1
0

agents_controller.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. class AgentsController < ApplicationController
  2. include DotHelper
  3. include ActionView::Helpers::TextHelper
  4. include SortableTable
  5. def index
  6. set_table_sort sorts: %w[name last_check_at last_event_at last_receive_at], default: { name: :asc }
  7. @agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
  8. respond_to do |format|
  9. format.html
  10. format.json { render json: @agents }
  11. end
  12. end
  13. def handle_details_post
  14. @agent = current_user.agents.find(params[:id])
  15. if @agent.respond_to?(:handle_details_post)
  16. render :json => @agent.handle_details_post(params) || {}
  17. else
  18. @agent.error "#handle_details_post called on an instance of #{@agent.class} that does not define it."
  19. head 500
  20. end
  21. end
  22. def run
  23. @agent = current_user.agents.find(params[:id])
  24. Agent.async_check(@agent.id)
  25. respond_to do |format|
  26. format.html { redirect_back "Agent run queued for '#{@agent.name}'" }
  27. format.json { head :ok }
  28. end
  29. end
  30. def dry_run
  31. attrs = params[:agent] || {}
  32. if agent = current_user.agents.find_by(id: params[:id])
  33. # PUT /agents/:id/dry_run
  34. if attrs.present?
  35. type = agent.type
  36. agent = Agent.build_for_type(type, current_user, attrs)
  37. end
  38. else
  39. # POST /agents/dry_run
  40. type = attrs.delete(:type)
  41. agent = Agent.build_for_type(type, current_user, attrs)
  42. end
  43. agent.name ||= '(Untitled)'
  44. if agent.valid?
  45. results = agent.dry_run!
  46. render json: {
  47. log: results[:log],
  48. events: Utils.pretty_print(results[:events], false),
  49. memory: Utils.pretty_print(results[:memory] || {}, false),
  50. }
  51. else
  52. render json: {
  53. log: [
  54. "#{pluralize(agent.errors.count, "error")} prohibited this Agent from being saved:",
  55. *agent.errors.full_messages
  56. ].join("\n- "),
  57. events: '',
  58. memory: '',
  59. }
  60. end
  61. end
  62. def type_details
  63. @agent = Agent.build_for_type(params[:type], current_user, {})
  64. initialize_presenter
  65. render json: {
  66. can_be_scheduled: @agent.can_be_scheduled?,
  67. default_schedule: @agent.default_schedule,
  68. can_receive_events: @agent.can_receive_events?,
  69. can_create_events: @agent.can_create_events?,
  70. can_control_other_agents: @agent.can_control_other_agents?,
  71. can_dry_run: @agent.can_dry_run?,
  72. options: @agent.default_options,
  73. description_html: @agent.html_description,
  74. oauthable: render_to_string(partial: 'oauth_dropdown', locals: { agent: @agent }),
  75. form_options: render_to_string(partial: 'options', locals: { agent: @agent })
  76. }
  77. end
  78. def event_descriptions
  79. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  80. agents.map(&:html_event_description).uniq.map { |desc|
  81. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  82. }
  83. }.flatten.join()
  84. render :json => { :description_html => html }
  85. end
  86. def remove_events
  87. @agent = current_user.agents.find(params[:id])
  88. @agent.events.delete_all
  89. respond_to do |format|
  90. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  91. format.json { head :ok }
  92. end
  93. end
  94. def propagate
  95. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  96. respond_to do |format|
  97. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  98. format.json { head :ok }
  99. end
  100. end
  101. def destroy_memory
  102. @agent = current_user.agents.find(params[:id])
  103. @agent.update!(memory: {})
  104. respond_to do |format|
  105. format.html { redirect_back "Memory erased for '#{@agent.name}'" }
  106. format.json { head :ok }
  107. end
  108. end
  109. def show
  110. @agent = current_user.agents.find(params[:id])
  111. respond_to do |format|
  112. format.html
  113. format.json { render json: @agent }
  114. end
  115. end
  116. def new
  117. agents = current_user.agents
  118. if id = params[:id]
  119. @agent = agents.build_clone(agents.find(id))
  120. else
  121. @agent = agents.build
  122. end
  123. initialize_presenter
  124. respond_to do |format|
  125. format.html
  126. format.json { render json: @agent }
  127. end
  128. end
  129. def edit
  130. @agent = current_user.agents.find(params[:id])
  131. initialize_presenter
  132. end
  133. def create
  134. build_agent
  135. respond_to do |format|
  136. if @agent.save
  137. format.html { redirect_back "'#{@agent.name}' was successfully created." }
  138. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  139. else
  140. initialize_presenter
  141. format.html { render action: "new" }
  142. format.json { render json: @agent.errors, status: :unprocessable_entity }
  143. end
  144. end
  145. end
  146. def update
  147. @agent = current_user.agents.find(params[:id])
  148. respond_to do |format|
  149. if @agent.update_attributes(params[:agent])
  150. format.html { redirect_back "'#{@agent.name}' was successfully updated." }
  151. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  152. else
  153. initialize_presenter
  154. format.html { render action: "edit" }
  155. format.json { render json: @agent.errors, status: :unprocessable_entity }
  156. end
  157. end
  158. end
  159. def leave_scenario
  160. @agent = current_user.agents.find(params[:id])
  161. @scenario = current_user.scenarios.find(params[:scenario_id])
  162. @agent.scenarios.destroy(@scenario)
  163. respond_to do |format|
  164. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  165. format.json { head :no_content }
  166. end
  167. end
  168. def destroy
  169. @agent = current_user.agents.find(params[:id])
  170. @agent.destroy
  171. respond_to do |format|
  172. format.html { redirect_back "'#{@agent.name}' deleted" }
  173. format.json { head :no_content }
  174. end
  175. end
  176. def validate
  177. build_agent
  178. if @agent.validate_option(params[:attribute])
  179. render text: 'ok'
  180. else
  181. render text: 'error', status: 403
  182. end
  183. end
  184. def complete
  185. build_agent
  186. render json: @agent.complete_option(params[:attribute])
  187. end
  188. protected
  189. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  190. def redirect_back(message)
  191. if params[:return] == "show" && @agent && !@agent.destroyed?
  192. path = agent_path(@agent)
  193. elsif params[:return] =~ /\A#{Regexp::escape scenarios_path}\/\d+\Z/
  194. path = params[:return]
  195. else
  196. path = agents_path
  197. end
  198. redirect_to path, notice: message
  199. end
  200. def build_agent
  201. @agent = Agent.build_for_type(params[:agent].delete(:type),
  202. current_user,
  203. params[:agent])
  204. end
  205. def initialize_presenter
  206. if @agent.present? && @agent.is_form_configurable?
  207. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  208. end
  209. end
  210. end