agents_controller.rb 6.7KB

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