application_controller.rb 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class ApplicationController < ActionController::Base
  2. protect_from_forgery
  3. before_action :authenticate_user!
  4. before_action :configure_permitted_parameters, if: :devise_controller?
  5. helper :all
  6. rescue_from 'ActiveRecord::SubclassNotFound' do
  7. @undefined_agent_types = current_user.undefined_agent_types
  8. render template: 'application/undefined_agents'
  9. end
  10. def redirect_back(fallback_path, **args)
  11. super(fallback_location: fallback_path, **args)
  12. end
  13. protected
  14. def configure_permitted_parameters
  15. devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email, :password, :password_confirmation, :remember_me, :invitation_code])
  16. devise_parameter_sanitizer.permit(:sign_in, keys: [:login, :username, :email, :password, :remember_me])
  17. devise_parameter_sanitizer.permit(:account_update, keys: [:username, :email, :password, :password_confirmation, :current_password])
  18. end
  19. def authenticate_admin!
  20. redirect_to(root_path, alert: 'Admin access required to view that page.') unless current_user && current_user.admin?
  21. end
  22. def upgrade_warning
  23. return unless current_user
  24. twitter_oauth_check
  25. basecamp_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 agent_params
  55. return {} unless params[:agent]
  56. @agent_params ||= begin
  57. options = params[:agent].delete(:options) if params[:agent][:options].present?
  58. params[:agent].permit(:memory, :name, :type, :schedule, :disabled, :keep_events_for, :propagate_immediately, :drop_pending_events, :service_id,
  59. source_ids: [], receiver_ids: [], scenario_ids: [], controller_ids: [], control_target_ids: []).tap do |agent_params|
  60. agent_params[:options] = options if options
  61. agent_params[:options].permit! if agent_params[:options].respond_to?(:permit!)
  62. end
  63. end
  64. end
  65. end