Module: Workarea::Authentication

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/workarea/authentication.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



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(session[:user_id]) rescue nil
end

#logged_in?Boolean

Returns:

  • (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
24
# File 'app/controllers/workarea/authentication.rb', line 18

def (user)
  @current_user = user
  session[:user_id] = user.id.to_s
  user.update_login!(request)
  update_tracking!
  user
end

#logoutObject



26
27
28
29
30
31
# File 'app/controllers/workarea/authentication.rb', line 26

def logout
  reset_session
  cookies.delete(:cache)
  @current_user = nil
  update_tracking!
end

#redirect_back_or(default = root_path) ⇒ Object



100
101
102
103
104
# File 'app/controllers/workarea/authentication.rb', line 100

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



79
80
81
82
# File 'app/controllers/workarea/authentication.rb', line 79

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
55
# File 'app/controllers/workarea/authentication.rb', line 47

def (should_remember_location = true)
  return if logged_in?

  logout if current_user.present? # reset everything if invalid logged in request
  flash[:info] = t('workarea.authentication.login')
  remember_location if request.get? && should_remember_location
  redirect_to storefront., turbolinks: false
  false
end

#require_logoutObject



57
58
59
60
61
62
63
# File 'app/controllers/workarea/authentication.rb', line 57

def require_logout
  if logged_in?
    flash[:info] = t('workarea.authentication.logout')
    redirect_to storefront.
    return false
  end
end

#require_password_changesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/workarea/authentication.rb', line 65

def require_password_changes
  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_toObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/workarea/authentication.rb', line 84

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

TODO deprecated, remove in v3.6



34
# File 'app/controllers/workarea/authentication.rb', line 34

def touch_auth_cookie; end