agents_controller.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. @agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])
  129. initialize_presenter
  130. respond_to do |format|
  131. format.html
  132. format.json { render json: @agent }
  133. end
  134. end
  135. def edit
  136. @agent = current_user.agents.find(params[:id])
  137. initialize_presenter
  138. end
  139. def create
  140. build_agent
  141. respond_to do |format|
  142. if @agent.save
  143. format.html { redirect_back "'#{@agent.name}' was successfully created.", return: agents_path }
  144. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  145. else
  146. initialize_presenter
  147. format.html { render action: "new" }
  148. format.json { render json: @agent.errors, status: :unprocessable_entity }
  149. end
  150. end
  151. end
  152. def update
  153. @agent = current_user.agents.find(params[:id])
  154. respond_to do |format|
  155. if @agent.update_attributes(params[:agent])
  156. format.html { redirect_back "'#{@agent.name}' was successfully updated.", return: agents_path }
  157. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  158. else
  159. initialize_presenter
  160. format.html { render action: "edit" }
  161. format.json { render json: @agent.errors, status: :unprocessable_entity }
  162. end
  163. end
  164. end
  165. def leave_scenario
  166. @agent = current_user.agents.find(params[:id])
  167. @scenario = current_user.scenarios.find(params[:scenario_id])
  168. @agent.scenarios.destroy(@scenario)
  169. respond_to do |format|
  170. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  171. format.json { head :no_content }
  172. end
  173. end
  174. def destroy
  175. @agent = current_user.agents.find(params[:id])
  176. @agent.destroy
  177. respond_to do |format|
  178. format.html { redirect_back "'#{@agent.name}' deleted" }
  179. format.json { head :no_content }
  180. end
  181. end
  182. def validate
  183. build_agent
  184. if @agent.validate_option(params[:attribute])
  185. render text: 'ok'
  186. else
  187. render text: 'error', status: 403
  188. end
  189. end
  190. def complete
  191. build_agent
  192. render json: @agent.complete_option(params[:attribute])
  193. end
  194. protected
  195. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  196. def redirect_back(message, options = {})
  197. case ret = params[:return] || options[:return]
  198. when "show"
  199. if @agent && !@agent.destroyed?
  200. path = agent_path(@agent)
  201. else
  202. path = agents_path
  203. end
  204. when /\A#{Regexp::escape scenarios_path}\/\d+\z/, agents_path
  205. path = ret
  206. end
  207. if path
  208. redirect_to path, notice: message
  209. else
  210. super agents_path, notice: message
  211. end
  212. end
  213. def build_agent
  214. @agent = Agent.build_for_type(params[:agent].delete(:type),
  215. current_user,
  216. params[:agent])
  217. end
  218. def initialize_presenter
  219. if @agent.present? && @agent.is_form_configurable?
  220. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  221. end
  222. end
  223. end