agents_controller.rb 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 show
  99. @agent = current_user.agents.find(params[:id])
  100. respond_to do |format|
  101. format.html
  102. format.json { render json: @agent }
  103. end
  104. end
  105. def new
  106. agents = current_user.agents
  107. if id = params[:id]
  108. @agent = agents.build_clone(agents.find(id))
  109. else
  110. @agent = agents.build
  111. end
  112. initialize_presenter
  113. respond_to do |format|
  114. format.html
  115. format.json { render json: @agent }
  116. end
  117. end
  118. def edit
  119. @agent = current_user.agents.find(params[:id])
  120. initialize_presenter
  121. end
  122. def create
  123. build_agent
  124. respond_to do |format|
  125. if @agent.save
  126. format.html { redirect_back "'#{@agent.name}' was successfully created." }
  127. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  128. else
  129. initialize_presenter
  130. format.html { render action: "new" }
  131. format.json { render json: @agent.errors, status: :unprocessable_entity }
  132. end
  133. end
  134. end
  135. def update
  136. @agent = current_user.agents.find(params[:id])
  137. respond_to do |format|
  138. if @agent.update_attributes(params[:agent])
  139. format.html { redirect_back "'#{@agent.name}' was successfully updated." }
  140. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  141. else
  142. initialize_presenter
  143. format.html { render action: "edit" }
  144. format.json { render json: @agent.errors, status: :unprocessable_entity }
  145. end
  146. end
  147. end
  148. def leave_scenario
  149. @agent = current_user.agents.find(params[:id])
  150. @scenario = current_user.scenarios.find(params[:scenario_id])
  151. @agent.scenarios.destroy(@scenario)
  152. respond_to do |format|
  153. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  154. format.json { head :no_content }
  155. end
  156. end
  157. def destroy
  158. @agent = current_user.agents.find(params[:id])
  159. @agent.destroy
  160. respond_to do |format|
  161. format.html { redirect_back "'#{@agent.name}' deleted" }
  162. format.json { head :no_content }
  163. end
  164. end
  165. def validate
  166. build_agent
  167. if @agent.validate_option(params[:attribute])
  168. render text: 'ok'
  169. else
  170. render text: 'error', status: 403
  171. end
  172. end
  173. def complete
  174. build_agent
  175. render json: @agent.complete_option(params[:attribute])
  176. end
  177. protected
  178. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  179. def redirect_back(message)
  180. if params[:return] == "show" && @agent && !@agent.destroyed?
  181. path = agent_path(@agent)
  182. elsif params[:return] =~ /\A#{Regexp::escape scenarios_path}\/\d+\Z/
  183. path = params[:return]
  184. else
  185. path = agents_path
  186. end
  187. redirect_to path, notice: message
  188. end
  189. def build_agent
  190. @agent = Agent.build_for_type(params[:agent].delete(:type),
  191. current_user,
  192. params[:agent])
  193. end
  194. def initialize_presenter
  195. if @agent.present? && @agent.is_form_configurable?
  196. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  197. end
  198. end
  199. end