Nessuna descrizione http://j1x-huginn.herokuapp.com

agents_controller.rb 4.7KB

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