Keine Beschreibung http://j1x-huginn.herokuapp.com

agents_controller.rb 4.6KB

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