Module: Incline::Extensions::Session::Common

Defined in:
lib/incline/extensions/session.rb

Overview

Contains the methods common to both controllers and views.

Instance Method Summary collapse

Instance Method Details

#current_userObject

Gets the currently logged in user.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/incline/extensions/session.rb', line 34

def current_user
  @current_user ||=
      if (user_id = session[:user_id])
        Incline::User.find_by(id: user_id)
      elsif (cookies&.respond_to?(:signed)) &&
          (user_id = cookies.signed[user_id_cookie]) &&
          (user = Incline::User.find_by(id: user_id)) &&
          (user.authenticated?(:remember, cookies[user_token_cookie]))
         user if respond_to?(:log_in)
        user
      else
        nil
      end ||Incline::User::anonymous
end

#current_user?(user) ⇒ Boolean

Is the specified user the current user?

Returns:

  • (Boolean)


51
52
53
# File 'lib/incline/extensions/session.rb', line 51

def current_user?(user)
  current_user == user
end

#logged_in?Boolean

Is a user logged in?

Returns:

  • (Boolean)


57
58
59
# File 'lib/incline/extensions/session.rb', line 57

def logged_in?
  !current_user.anonymous?
end

#system_admin?Boolean

Is the current user a system administrator?

Returns:

  • (Boolean)


63
64
65
# File 'lib/incline/extensions/session.rb', line 63

def system_admin?
  logged_in? && current_user.system_admin? && current_user.enabled? && current_user.activated?
end

Gets the name of the cookie containing the user ID for the current user.

This gets set when the user selects the “Remember me” checkbox when logging in.



20
21
22
# File 'lib/incline/extensions/session.rb', line 20

def user_id_cookie
  @user_id_cookie ||= Rails.application.cookie_name(:user_id)
end

Gets the name of the cookie containing the remember token for the current user.

This gets set when the user selects the “Remember me” checkbox when logging in.



28
29
30
# File 'lib/incline/extensions/session.rb', line 28

def user_token_cookie
  @user_token_cookie ||= Rails.application.cookie_name(:user_token)
end