agents_controller.rb 4.8KB

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