agents_controller.rb 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. # POST /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. if event_payload = params[:event]
  46. dummy_agent = Agent.build_for_type('ManualEventAgent', current_user, name: 'Dry-Runner')
  47. dummy_agent.readonly!
  48. event = dummy_agent.events.build(user: current_user, payload: event_payload)
  49. end
  50. results = agent.dry_run!(event)
  51. render json: {
  52. log: results[:log],
  53. events: Utils.pretty_print(results[:events], false),
  54. memory: Utils.pretty_print(results[:memory] || {}, false),
  55. }
  56. else
  57. render json: {
  58. log: [
  59. "#{pluralize(agent.errors.count, "error")} prohibited this Agent from being saved:",
  60. *agent.errors.full_messages
  61. ].join("\n- "),
  62. events: '',
  63. memory: '',
  64. }
  65. end
  66. end
  67. def type_details
  68. @agent = Agent.build_for_type(params[:type], current_user, {})
  69. initialize_presenter
  70. render json: {
  71. can_be_scheduled: @agent.can_be_scheduled?,
  72. default_schedule: @agent.default_schedule,
  73. can_receive_events: @agent.can_receive_events?,
  74. can_create_events: @agent.can_create_events?,
  75. can_control_other_agents: @agent.can_control_other_agents?,
  76. can_dry_run: @agent.can_dry_run?,
  77. options: @agent.default_options,
  78. description_html: @agent.html_description,
  79. oauthable: render_to_string(partial: 'oauth_dropdown', locals: { agent: @agent }),
  80. form_options: render_to_string(partial: 'options', locals: { agent: @agent })
  81. }
  82. end
  83. def event_descriptions
  84. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  85. agents.map(&:html_event_description).uniq.map { |desc|
  86. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  87. }
  88. }.flatten.join()
  89. render :json => { :description_html => html }
  90. end
  91. def remove_events
  92. @agent = current_user.agents.find(params[:id])
  93. @agent.events.delete_all
  94. respond_to do |format|
  95. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  96. format.json { head :ok }
  97. end
  98. end
  99. def propagate
  100. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  101. respond_to do |format|
  102. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  103. format.json { head :ok }
  104. end
  105. end
  106. def destroy_memory
  107. @agent = current_user.agents.find(params[:id])
  108. @agent.update!(memory: {})
  109. respond_to do |format|
  110. format.html { redirect_back "Memory erased for '#{@agent.name}'" }
  111. format.json { head :ok }
  112. end
  113. end
  114. def show
  115. @agent = current_user.agents.find(params[:id])
  116. respond_to do |format|
  117. format.html
  118. format.json { render json: @agent }
  119. end
  120. end
  121. def new
  122. agents = current_user.agents
  123. if id = params[:id]
  124. @agent = agents.build_clone(agents.find(id))
  125. else
  126. @agent = agents.build
  127. end
  128. initialize_presenter
  129. respond_to do |format|
  130. format.html
  131. format.json { render json: @agent }
  132. end
  133. end
  134. def edit
  135. @agent = current_user.agents.find(params[:id])
  136. initialize_presenter
  137. end
  138. def create
  139. build_agent
  140. respond_to do |format|
  141. if @agent.save
  142. format.html { redirect_back "'#{@agent.name}' was successfully created.", return: agents_path }
  143. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  144. else
  145. initialize_presenter
  146. format.html { render action: "new" }
  147. format.json { render json: @agent.errors, status: :unprocessable_entity }
  148. end
  149. end
  150. end
  151. def update
  152. @agent = current_user.agents.find(params[:id])
  153. respond_to do |format|
  154. if @agent.update_attributes(params[:agent])
  155. format.html { redirect_back "'#{@agent.name}' was successfully updated.", return: agents_path }
  156. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  157. else
  158. initialize_presenter
  159. format.html { render action: "edit" }
  160. format.json { render json: @agent.errors, status: :unprocessable_entity }
  161. end
  162. end
  163. end
  164. def leave_scenario
  165. @agent = current_user.agents.find(params[:id])
  166. @scenario = current_user.scenarios.find(params[:scenario_id])
  167. @agent.scenarios.destroy(@scenario)
  168. respond_to do |format|
  169. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  170. format.json { head :no_content }
  171. end
  172. end
  173. def destroy
  174. @agent = current_user.agents.find(params[:id])
  175. @agent.destroy
  176. respond_to do |format|
  177. format.html { redirect_back "'#{@agent.name}' deleted" }
  178. format.json { head :no_content }
  179. end
  180. end
  181. def validate
  182. build_agent
  183. if @agent.validate_option(params[:attribute])
  184. render text: 'ok'
  185. else
  186. render text: 'error', status: 403
  187. end
  188. end
  189. def complete
  190. build_agent
  191. render json: @agent.complete_option(params[:attribute])
  192. end
  193. protected
  194. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  195. def redirect_back(message, options = {})
  196. case ret = params[:return] || options[:return]
  197. when "show"
  198. if @agent && !@agent.destroyed?
  199. path = agent_path(@agent)
  200. end
  201. when /\A#{Regexp::escape scenarios_path}\/\d+\Z/, agents_path
  202. path = ret
  203. end
  204. if path
  205. redirect_to path, notice: message
  206. else
  207. super agents_path, notice: message
  208. end
  209. end
  210. def build_agent
  211. @agent = Agent.build_for_type(params[:agent].delete(:type),
  212. current_user,
  213. params[:agent])
  214. end
  215. def initialize_presenter
  216. if @agent.present? && @agent.is_form_configurable?
  217. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  218. end
  219. end
  220. end