Module: Workarea::Authentication

Extended by:
ActiveSupport::Concern
Included in:
AdminGuestBrowsing
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(cookies.signed[: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
# File 'app/controllers/workarea/authentication.rb', line 18

def (user)
  @current_user = user
  user.update_login!(request)
  touch_auth_cookie
  user
end

#logoutObject



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

def logout
  cookies.delete(:user_id)
  cookies.delete(:cache)
  cookies.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 (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., turbolinks: false
  false
end

#require_logoutObject



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.
    return false
  end
end

#require_password_changesObject



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_toObject



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


32
33
34
35
36
37
38
39
40
# File 'app/controllers/workarea/authentication.rb', line 32

def touch_auth_cookie
  return if current_user.blank?

  cookies.signed[:user_id] = { value: current_user.id, expires: auth_expiry }

  if current_user.admin?
    cookies[:cache] = { value: 'false', expires: auth_expiry }
  end
end