Module: AuthenticatesRpi::InstanceMethods

Defined in:
lib/authenticates_rpi.rb

Overview

Instance methods that are included when we run authenticates_rpi in a controller (say, ApplicationController) We use instance methods because the access the session, which belongs to the ActionController instance.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Setup for when we get mixed into ActionController::Base



75
76
77
78
79
80
# File 'lib/authenticates_rpi.rb', line 75

def self.included(base)
  base.helper :all
  base.before_filter "set_up_accessor"
  base.helper_method :logged_in?, :admin_logged_in?, :go_to_login,
                     :current_user, :current_user_display_name
end

Instance Method Details

#admin_logged_in?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/authenticates_rpi.rb', line 91

def admin_logged_in?
  if session[:username].nil?
    #No current user
    false
  elsif admin_field.nil?
    #Site not configured for admin behavior
    logger.warn "AuthenticatesRpi checking for admin, " +
      "but no admin field is configured. "
    false
  else
    #Check the app-configured admin field
    if current_user.send admin_field
      true
    else
      false
    end
  end
end

#current_userObject

Method to return the object representing the logged in user, or false if there’s nobody logged in. For general use, and included as a helper. This should be the only method used for getting the current user.



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/authenticates_rpi.rb', line 114

def current_user
  if session[:username].nil?
    false
  else
    p = find_user_by_username(session[:username])
    if p.nil?
      raise "User "+session[:username]+" not found!"
    else
      p
    end
  end
end

#current_user_display_nameObject

Figures out a ‘display name’ for the user, in the following priority

  1. If there’s a fullname field defined, use that

  2. If there are first and last names, use them

  3. Just use the username, because its unique.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/authenticates_rpi.rb', line 131

def current_user_display_name
  user = current_user
  if fullname_field
    n = user.send(fullname_field)
    return n unless n.blank?
  end

  if firstname_field && lastname_field
    f = user.send(firstname_field)
    l = user.send(lastname_field)
    return f + " " + l unless f.blank? || l.blank?
  end

  return user.send(username_field)
end

#go_to_loginObject



147
148
149
150
151
152
153
154
# File 'lib/authenticates_rpi.rb', line 147

def 
  redirect_to :controller => 'sessions', :action => 'new'

  # Before we go, save the current (full) path.
  # This will allow us to get back to the requested page
  # once we've authenticated.
  session[:page_before_login] = request.request_uri
end

#logged_in?Boolean

Methods for interacting with session data

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'lib/authenticates_rpi.rb', line 83

def logged_in?
  if session[:username].nil?
    false
  else
    true
  end
end