Module: Workarea::Authentication
- Extended by:
- ActiveSupport::Concern
- Included in:
- AdminGuestBrowsing
- Defined in:
- app/controllers/workarea/authentication.rb
Instance Method Summary collapse
- #current_user ⇒ Object
- #logged_in? ⇒ Boolean
- #login(user) ⇒ Object
- #logout ⇒ Object
- #redirect_back_or(default = root_path) ⇒ Object
- #remember_location(value = request.fullpath) ⇒ Object
- #require_login(should_remember_location = true) ⇒ Object
- #require_logout ⇒ Object
- #require_password_changes ⇒ Object
- #return_to ⇒ Object
- #touch_auth_cookie ⇒ Object (also: #keep_auth_alive)
Instance Method Details
#current_user ⇒ Object
13 14 15 16 |
# File 'app/controllers/workarea/authentication.rb', line 13 def current_user return @current_user if defined?(@current_user) @current_user = User.find(.signed[:user_id]) rescue nil end |
#logged_in? ⇒ Boolean
43 44 45 |
# File 'app/controllers/workarea/authentication.rb', line 43 def logged_in? current_user.present? && current_user.valid_logged_in_request?(request) end |
#login(user) ⇒ Object
18 19 20 21 22 23 |
# File 'app/controllers/workarea/authentication.rb', line 18 def login(user) @current_user = user user.update_login!(request) user end |
#logout ⇒ Object
25 26 27 28 29 30 |
# File 'app/controllers/workarea/authentication.rb', line 25 def logout .delete(:user_id) .delete(:cache) .delete(:completed_order) @current_user = nil end |
#redirect_back_or(default = root_path) ⇒ Object
101 102 103 104 105 |
# File 'app/controllers/workarea/authentication.rb', line 101 def redirect_back_or(default = root_path) remembered = return_to.presence || session[:return_to].presence session.delete(:return_to) redirect_to remembered || default end |
#remember_location(value = request.fullpath) ⇒ Object
80 81 82 83 |
# File 'app/controllers/workarea/authentication.rb', line 80 def remember_location(value = request.fullpath) url = URI.parse(return_to.presence || value).request_uri session[:return_to] = url[0..Workarea.config.return_to_url_max_length] end |
#require_login(should_remember_location = true) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'app/controllers/workarea/authentication.rb', line 47 def require_login(should_remember_location = true) return if logged_in? flash[:info] = t('workarea.authentication.login') remember_location if request.get? && should_remember_location redirect_to storefront.login_path, turbolinks: false false end |
#require_logout ⇒ Object
56 57 58 59 60 61 62 |
# File 'app/controllers/workarea/authentication.rb', line 56 def require_logout if logged_in? flash[:info] = t('workarea.authentication.logout') redirect_to storefront.login_path return false end end |
#require_password_changes ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/controllers/workarea/authentication.rb', line 64 def require_password_changes return unless logged_in? if current_user.force_password_change? flash[:warning] = t('workarea.authentication.password_expired') if request.xhr? head :unauthorized else redirect_to storefront.change_password_path end return false end end |
#return_to ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/controllers/workarea/authentication.rb', line 85 def return_to return nil unless params[:return_to].present? if params[:return_to].respond_to?(:to_h) url_for(params[:return_to].to_h.merge(only_path: true)) else uri = URI.parse(params[:return_to]) if uri.fragment.present? "#{uri.request_uri}##{uri.fragment}" else uri.request_uri end end end |
#touch_auth_cookie ⇒ Object Also known as: keep_auth_alive
32 33 34 35 36 37 38 39 40 |
# File 'app/controllers/workarea/authentication.rb', line 32 def return if current_user.blank? .signed[:user_id] = { value: current_user.id, expires: auth_expiry } if current_user.admin? [:cache] = { value: 'false', expires: auth_expiry } end end |