application_controller.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. def redirect_back(fallback_path, *args)
  7. redirect_to :back, *args
  8. rescue ActionController::RedirectBackError
  9. redirect_to fallback_path, *args
  10. end
  11. protected
  12. def configure_permitted_parameters
  13. devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me, :invitation_code) }
  14. devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
  15. devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
  16. end
  17. def authenticate_admin!
  18. redirect_to(root_path, alert: 'Admin access required to view that page.') unless current_user && current_user.admin?
  19. end
  20. def upgrade_warning
  21. return unless current_user
  22. twitter_oauth_check
  23. basecamp_auth_check
  24. end
  25. def filtered_agent_return_link(options = {})
  26. case ret = params[:return].presence || options[:return]
  27. when "show"
  28. if @agent && !@agent.destroyed?
  29. agent_path(@agent)
  30. else
  31. agents_path
  32. end
  33. when /\A#{(Regexp::escape scenarios_path)}/, /\A#{(Regexp::escape agents_path)}/, /\A#{(Regexp::escape events_path)}/
  34. ret
  35. end
  36. end
  37. helper_method :filtered_agent_return_link
  38. private
  39. def twitter_oauth_check
  40. unless Devise.omniauth_providers.include?(:twitter)
  41. if @twitter_agent = current_user.agents.where("type like 'Agents::Twitter%'").first
  42. @twitter_oauth_key = @twitter_agent.options['consumer_key'].presence || @twitter_agent.credential('twitter_consumer_key')
  43. @twitter_oauth_secret = @twitter_agent.options['consumer_secret'].presence || @twitter_agent.credential('twitter_consumer_secret')
  44. end
  45. end
  46. end
  47. def basecamp_auth_check
  48. unless Devise.omniauth_providers.include?(:'37signals')
  49. @basecamp_agent = current_user.agents.where(type: 'Agents::BasecampAgent').first
  50. end
  51. end
  52. end