application_controller.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class ApplicationController < ActionController::Base
  2. before_action :authenticate_user!
  3. before_action :configure_permitted_parameters, if: :devise_controller?
  4. helper :all
  5. rescue_from 'ActiveRecord::SubclassNotFound' do
  6. @undefined_agent_types = current_user.undefined_agent_types
  7. render template: 'application/undefined_agents'
  8. end
  9. def redirect_back(fallback_path, **args)
  10. super(fallback_location: fallback_path, **args)
  11. end
  12. protected
  13. def configure_permitted_parameters
  14. devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email, :password, :password_confirmation, :remember_me, :invitation_code])
  15. devise_parameter_sanitizer.permit(:sign_in, keys: [:login, :username, :email, :password, :remember_me])
  16. devise_parameter_sanitizer.permit(:account_update, keys: [:username, :email, :password, :password_confirmation, :current_password])
  17. end
  18. def authenticate_admin!
  19. redirect_to(root_path, alert: 'Admin access required to view that page.') unless current_user && current_user.admin?
  20. end
  21. def upgrade_warning
  22. return unless current_user
  23. twitter_oauth_check
  24. basecamp_auth_check
  25. outdated_docker_image_namespace_check
  26. outdated_google_auth_check
  27. end
  28. def filtered_agent_return_link(options = {})
  29. case ret = params[:return].presence || options[:return]
  30. when "show"
  31. if @agent && !@agent.destroyed?
  32. agent_path(@agent)
  33. else
  34. agents_path
  35. end
  36. when /\A#{(Regexp::escape scenarios_path)}/, /\A#{(Regexp::escape agents_path)}/, /\A#{(Regexp::escape events_path)}/
  37. ret
  38. end
  39. end
  40. helper_method :filtered_agent_return_link
  41. private
  42. def twitter_oauth_check
  43. unless Devise.omniauth_providers.include?(:twitter)
  44. if @twitter_agent = current_user.agents.where("type like 'Agents::Twitter%'").first
  45. @twitter_oauth_key = @twitter_agent.options['consumer_key'].presence || @twitter_agent.credential('twitter_consumer_key')
  46. @twitter_oauth_secret = @twitter_agent.options['consumer_secret'].presence || @twitter_agent.credential('twitter_consumer_secret')
  47. end
  48. end
  49. end
  50. def basecamp_auth_check
  51. unless Devise.omniauth_providers.include?(:'37signals')
  52. @basecamp_agent = current_user.agents.where(type: 'Agents::BasecampAgent').first
  53. end
  54. end
  55. def outdated_docker_image_namespace_check
  56. @outdated_docker_image_namespace = ENV['OUTDATED_DOCKER_IMAGE_NAMESPACE'] == 'true'
  57. end
  58. def outdated_google_auth_check
  59. @outdated_google_cal_agents = current_user.agents.of_type('Agents::GoogleCalendarPublishAgent').select do |agent|
  60. agent.options['google']['key_secret'].present?
  61. end
  62. end
  63. def agent_params
  64. return {} unless params[:agent]
  65. @agent_params ||= begin
  66. params[:agent].permit([:memory, :name, :type, :schedule, :disabled, :keep_events_for, :propagate_immediately, :drop_pending_events, :service_id,
  67. source_ids: [], receiver_ids: [], scenario_ids: [], controller_ids: [], control_target_ids: []] + agent_params_options)
  68. end
  69. end
  70. private
  71. def agent_params_options
  72. if params[:agent].fetch(:options, '').kind_of?(ActionController::Parameters)
  73. [options: {}]
  74. else
  75. [:options]
  76. end
  77. end
  78. end