agents_controller.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. render :json => {
  32. :can_be_scheduled => @agent.can_be_scheduled?,
  33. :default_schedule => @agent.default_schedule,
  34. :can_receive_events => @agent.can_receive_events?,
  35. :can_create_events => @agent.can_create_events?,
  36. :can_control_other_agents => @agent.can_control_other_agents?,
  37. :options => @agent.default_options,
  38. :description_html => @agent.html_description,
  39. :form => render_to_string(partial: 'oauth_dropdown', locals: { agent: @agent })
  40. }
  41. end
  42. def event_descriptions
  43. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  44. agents.map(&:html_event_description).uniq.map { |desc|
  45. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  46. }
  47. }.flatten.join()
  48. render :json => { :description_html => html }
  49. end
  50. def remove_events
  51. @agent = current_user.agents.find(params[:id])
  52. @agent.events.delete_all
  53. respond_to do |format|
  54. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  55. format.json { head :ok }
  56. end
  57. end
  58. def propagate
  59. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  60. respond_to do |format|
  61. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  62. format.json { head :ok }
  63. end
  64. end
  65. def show
  66. @agent = current_user.agents.find(params[:id])
  67. respond_to do |format|
  68. format.html
  69. format.json { render json: @agent }
  70. end
  71. end
  72. def new
  73. agents = current_user.agents
  74. if id = params[:id]
  75. @agent = agents.build_clone(agents.find(id))
  76. else
  77. @agent = agents.build
  78. end
  79. respond_to do |format|
  80. format.html
  81. format.json { render json: @agent }
  82. end
  83. end
  84. def edit
  85. @agent = current_user.agents.find(params[:id])
  86. end
  87. def create
  88. @agent = Agent.build_for_type(params[:agent].delete(:type),
  89. current_user,
  90. params[:agent])
  91. respond_to do |format|
  92. if @agent.save
  93. format.html { redirect_back "'#{@agent.name}' was successfully created." }
  94. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  95. else
  96. format.html { render action: "new" }
  97. format.json { render json: @agent.errors, status: :unprocessable_entity }
  98. end
  99. end
  100. end
  101. def update
  102. @agent = current_user.agents.find(params[:id])
  103. respond_to do |format|
  104. if @agent.update_attributes(params[:agent])
  105. format.html { redirect_back "'#{@agent.name}' was successfully updated." }
  106. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  107. else
  108. format.html { render action: "edit" }
  109. format.json { render json: @agent.errors, status: :unprocessable_entity }
  110. end
  111. end
  112. end
  113. def leave_scenario
  114. @agent = current_user.agents.find(params[:id])
  115. @scenario = current_user.scenarios.find(params[:scenario_id])
  116. @agent.scenarios.destroy(@scenario)
  117. respond_to do |format|
  118. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  119. format.json { head :no_content }
  120. end
  121. end
  122. def destroy
  123. @agent = current_user.agents.find(params[:id])
  124. @agent.destroy
  125. respond_to do |format|
  126. format.html { redirect_back "'#{@agent.name}' deleted" }
  127. format.json { head :no_content }
  128. end
  129. end
  130. protected
  131. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  132. def redirect_back(message)
  133. if params[:return] == "show" && @agent
  134. path = agent_path(@agent)
  135. elsif params[:return] =~ /\A#{Regexp::escape scenarios_path}\/\d+\Z/
  136. path = params[:return]
  137. else
  138. path = agents_path
  139. end
  140. redirect_to path, notice: message
  141. end
  142. end