1
0

scenarios_controller.rb 3.9 KB

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