agents_controller.rb 4.6KB

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