Aucune description http://j1x-huginn.herokuapp.com

scenarios_controller.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. class ScenariosController < ApplicationController
  2. include SortableTable
  3. skip_before_action :authenticate_user!, only: :export
  4. def index
  5. set_table_sort sorts: %w[name public], default: { name: :asc }
  6. @scenarios = current_user.scenarios.reorder(table_sort).page(params[:page])
  7. respond_to do |format|
  8. format.html
  9. format.json { render json: @scenarios }
  10. end
  11. end
  12. def new
  13. @scenario = current_user.scenarios.build
  14. respond_to do |format|
  15. format.html
  16. format.json { render json: @scenario }
  17. end
  18. end
  19. def show
  20. @scenario = current_user.scenarios.find(params[:id])
  21. set_table_sort sorts: %w[name last_check_at last_event_at last_receive_at], default: { name: :asc }
  22. @agents = @scenario.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
  23. respond_to do |format|
  24. format.html
  25. format.json { render json: @scenario }
  26. end
  27. end
  28. def share
  29. @scenario = current_user.scenarios.find(params[:id])
  30. respond_to do |format|
  31. format.html
  32. format.json { render json: @scenario }
  33. end
  34. end
  35. def export
  36. @scenario = Scenario.find(params[:id])
  37. raise ActiveRecord::RecordNotFound unless @scenario.public? || (current_user && current_user.id == @scenario.user_id)
  38. @exporter = AgentsExporter.new(name: @scenario.name,
  39. description: @scenario.description,
  40. guid: @scenario.guid,
  41. tag_fg_color: @scenario.tag_fg_color,
  42. tag_bg_color: @scenario.tag_bg_color,
  43. icon: @scenario.icon,
  44. source_url: @scenario.public? && export_scenario_url(@scenario),
  45. agents: @scenario.agents)
  46. response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'
  47. render :json => JSON.pretty_generate(@exporter.as_json)
  48. end
  49. def edit
  50. @scenario = current_user.scenarios.find(params[:id])
  51. respond_to do |format|
  52. format.html
  53. format.json { render json: @scenario }
  54. end
  55. end
  56. def create
  57. @scenario = current_user.scenarios.build(scenario_params)
  58. respond_to do |format|
  59. if @scenario.save
  60. format.html { redirect_to @scenario, notice: 'This Scenario was successfully created.' }
  61. format.json { render json: @scenario, status: :created, location: @scenario }
  62. else
  63. format.html { render action: "new" }
  64. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  65. end
  66. end
  67. end
  68. def update
  69. @scenario = current_user.scenarios.find(params[:id])
  70. respond_to do |format|
  71. if @scenario.update_attributes(scenario_params)
  72. format.html { redirect_to @scenario, notice: 'This Scenario was successfully updated.' }
  73. format.json { head :no_content }
  74. else
  75. format.html { render action: "edit" }
  76. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  77. end
  78. end
  79. end
  80. def enable_or_disable_all_agents
  81. @scenario = current_user.scenarios.find(params[:id])
  82. @scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true')
  83. respond_to do |format|
  84. format.html { redirect_to @scenario, notice: 'The agents in this scenario have been successfully updated.' }
  85. format.json { head :no_content }
  86. end
  87. end
  88. def destroy
  89. @scenario = current_user.scenarios.find(params[:id])
  90. @scenario.destroy_with_mode(params[:mode])
  91. respond_to do |format|
  92. format.html { redirect_to scenarios_path }
  93. format.json { head :no_content }
  94. end
  95. end
  96. private
  97. def scenario_params
  98. params.require(:scenario).permit(:name, :description, :public, :source_url,
  99. :tag_fg_color, :tag_bg_color, :icon, agent_ids: [])
  100. end
  101. end