agents_controller.rb 5.6 KB

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