1
0

scenarios_controller.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. :source_url => @scenario.public? && export_scenario_url(@scenario),
  44. :agents => @scenario.agents)
  45. response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'
  46. render :json => JSON.pretty_generate(@exporter.as_json)
  47. end
  48. def edit
  49. @scenario = current_user.scenarios.find(params[:id])
  50. respond_to do |format|
  51. format.html
  52. format.json { render json: @scenario }
  53. end
  54. end
  55. def create
  56. @scenario = current_user.scenarios.build(params[:scenario])
  57. respond_to do |format|
  58. if @scenario.save
  59. format.html { redirect_to @scenario, notice: 'This Scenario was successfully created.' }
  60. format.json { render json: @scenario, status: :created, location: @scenario }
  61. else
  62. format.html { render action: "new" }
  63. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  64. end
  65. end
  66. end
  67. def update
  68. @scenario = current_user.scenarios.find(params[:id])
  69. respond_to do |format|
  70. if @scenario.update_attributes(params[:scenario])
  71. format.html { redirect_to @scenario, notice: 'This Scenario was successfully updated.' }
  72. format.json { head :no_content }
  73. else
  74. format.html { render action: "edit" }
  75. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  76. end
  77. end
  78. end
  79. def destroy
  80. @scenario = current_user.scenarios.find(params[:id])
  81. @scenario.destroy
  82. respond_to do |format|
  83. format.html { redirect_to scenarios_path }
  84. format.json { head :no_content }
  85. end
  86. end
  87. end