application_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_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 basecamp_auth_check
  50. unless Devise.omniauth_providers.include?(:'37signals')
  51. @basecamp_agent = current_user.agents.where(type: 'Agents::BasecampAgent').first
  52. end
  53. end
  54. def outdated_google_auth_check
  55. @outdated_google_cal_agents = current_user.agents.of_type('Agents::GoogleCalendarPublishAgent').select do |agent|
  56. agent.options['google']['key_secret'].present?
  57. end
  58. end
  59. def agent_params
  60. return {} unless params[:agent]
  61. @agent_params ||= begin
  62. params[:agent].permit([:memory, :name, :type, :schedule, :disabled, :keep_events_for, :propagate_immediately, :drop_pending_events, :service_id,
  63. source_ids: [], receiver_ids: [], scenario_ids: [], controller_ids: [], control_target_ids: []] + agent_params_options)
  64. end
  65. end
  66. private
  67. def agent_params_options
  68. if params[:agent].fetch(:options, '').kind_of?(ActionController::Parameters)
  69. [options: {}]
  70. else
  71. [:options]
  72. end
  73. end
  74. end