application_controller.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, allow_other_host: false, **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. outdated_docker_registry_check
  25. outdated_google_auth_check
  26. end
  27. def filtered_agent_return_link(options = {})
  28. case ret = params[:return].presence || options[:return]
  29. when "show"
  30. if @agent && !@agent.destroyed?
  31. agent_path(@agent)
  32. else
  33. agents_path
  34. end
  35. when /\A#{(Regexp::escape scenarios_path)}/, /\A#{(Regexp::escape agents_path)}/, /\A#{(Regexp::escape events_path)}/
  36. ret
  37. end
  38. end
  39. helper_method :filtered_agent_return_link
  40. private
  41. def twitter_oauth_check
  42. unless Devise.omniauth_providers.include?(:twitter)
  43. if @twitter_agent = current_user.agents.where("type like 'Agents::Twitter%'").first
  44. @twitter_oauth_key = @twitter_agent.options['consumer_key'].presence || @twitter_agent.credential('twitter_consumer_key')
  45. @twitter_oauth_secret = @twitter_agent.options['consumer_secret'].presence || @twitter_agent.credential('twitter_consumer_secret')
  46. end
  47. end
  48. end
  49. def outdated_docker_registry_check
  50. @outdated_docker_registry = ENV['OUTDATED_DOCKER_REGISTRY'] == 'true'
  51. end
  52. def outdated_google_auth_check
  53. @outdated_google_cal_agents = current_user.agents.of_type('Agents::GoogleCalendarPublishAgent').select do |agent|
  54. agent.options['google']['key_secret'].present?
  55. end
  56. end
  57. def agent_params
  58. return {} unless params[:agent]
  59. @agent_params ||= begin
  60. params[:agent].permit([:memory, :name, :type, :schedule, :disabled, :keep_events_for, :propagate_immediately, :drop_pending_events, :service_id,
  61. source_ids: [], receiver_ids: [], scenario_ids: [], controller_ids: [], control_target_ids: []] + agent_params_options)
  62. end
  63. end
  64. private
  65. def agent_params_options
  66. if params[:agent].fetch(:options, '').kind_of?(ActionController::Parameters)
  67. [options: {}]
  68. else
  69. [:options]
  70. end
  71. end
  72. end