Module: Challah::AuthableUser::InstanceMethods

Defined in:
lib/challah/authable/user.rb

Overview

Instance methods to be included once authable_user is set up.

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(sym, *args, &block)

Allow dynamic checking for permissions

admin? is shorthand for:

def admin?
  has(:admin)
end


223
224
225
226
# File 'lib/challah/authable/user.rb', line 223

def method_missing(sym, *args, &block)
  return has(sym.to_s.gsub(/\?/, '')) if sym.to_s =~ /^[a-z_]*\?$/
  super(sym, *args, &block)
end

Instance Method Details

- (Boolean) active?

Returns true if this user is active, and should be able to log in. If the active column is false, the user will not be able to authenticate

Returns:

  • (Boolean)


61
62
63
# File 'lib/challah/authable/user.rb', line 61

def active?
  !!self.active
end

- (Object) authenticate(*args)

Generic authentication method. By default, this just checks to see if the password given matches this user. You can also pass in the first parameter as the method to use for a different type of authentication.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/challah/authable/user.rb', line 68

def authenticate(*args)
  return false unless active?
  
  if args.length > 1
    method = args.shift
    
    if respond_to?("authenticate_with_#{method}")
      return self.send("authenticate_with_#{method}", *args)
    end
    
    false              
  else
    authenticate_with_password(args[0])
  end
end

- (Object) authenticate_with_api_key(api_key)

Pass in an api_key, and if it matches this user account, return true.



85
86
87
# File 'lib/challah/authable/user.rb', line 85

def authenticate_with_api_key(api_key)
  self.api_key == api_key
end

- (Object) authenticate_with_password(plain_password)

Pass in a password, and if it matches this user's account, return true.



90
91
92
# File 'lib/challah/authable/user.rb', line 90

def authenticate_with_password(plain_password)
  ::Challah::Encrypter.compare(self.crypted_password, plain_password)
end

- (Object) before_save_password (protected)

called before_save on the User model, actually encrypts the password with a new generated salt



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/challah/authable/user.rb', line 230

def before_save_password
  if @password_updated and valid?
    self.crypted_password = ::Challah::Encrypter.encrypt(@password)

    @password_updated = false
    @password = nil
  end

  self.persistence_token = ::Challah::Random.token(125) if self.persistence_token.to_s.blank?
  self.api_key = ::Challah::Random.token(50) if self.api_key.to_s.blank?
end

- (Object) default_path

The default url where this user should be redirected to after logging in. Also can be used as the main link at the top of navigation.



96
97
98
# File 'lib/challah/authable/user.rb', line 96

def default_path
  role ? role.default_path : '/'
end

- (Object) failed_authentication!



100
101
102
# File 'lib/challah/authable/user.rb', line 100

def failed_authentication!
  self.increment!(:failed_auth_count)
end

- (Object) has(permission_key) Also known as: permission?

Returns true if this user has permission to the provided permission key



161
162
163
# File 'lib/challah/authable/user.rb', line 161

def has(permission_key)
  self.permission_keys.include?(permission_key.to_s)
end

- (Object) name

full name



105
106
107
# File 'lib/challah/authable/user.rb', line 105

def name
  "#{first_name} #{last_name}"
end

- (Object) password

Get the value of the current password, only can be used right after setting a new password.



110
111
112
# File 'lib/challah/authable/user.rb', line 110

def password
  @password
end

- (Object) password=(value)

Set a password for this user



115
116
117
118
119
120
121
122
123
# File 'lib/challah/authable/user.rb', line 115

def password=(value)
  if value.to_s.blank?
    @password = nil
    @password_updated = false
  else
    @password = value
    @password_updated = true
  end
end

- (Object) password_confirmation=(value)

Set the confirmation when changing a password



126
127
128
# File 'lib/challah/authable/user.rb', line 126

def password_confirmation=(value)
  @password_confirmation = value
end

- (Object) permission_keys

Returns the permission keys in an array for exactly what this user can access. This includes all role based permission keys, and any specifically given to this user through permissions_users



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/challah/authable/user.rb', line 131

def permission_keys
  return @permission_keys if @permission_keys
  
  role_keys = if role(true)
    role_key = "#{role.cache_key}/permissions"
    
    keys = Rails.cache.fetch(role_key) do
      role.permission_keys.clone
    end
    
    Rails.cache.write(role_key, keys)          
    keys
  else
    []
  end
  
  user_key = "#{self.cache_key}/permissions"
  
  user_keys = Rails.cache.fetch(user_key) do
    user_permission_keys.clone
  end
  
  user_keys = [] unless user_keys
    
  Rails.cache.write(user_key, keys) unless new_record?

  @permission_keys = (role_keys + user_keys).uniq            
end

- (Object) permission_keys=(value)

Set the permission keys that this role can access



167
168
169
170
171
172
# File 'lib/challah/authable/user.rb', line 167

def permission_keys=(value)
  Rails.cache.delete("#{self.cache_key}/permissions")
  
  @permission_keys = value
  @permission_keys
end

- (Object) role_id=(value)

When a role is set, reset the permission_keys



175
176
177
178
179
180
# File 'lib/challah/authable/user.rb', line 175

def role_id=(value)
  @permission_keys = nil
  @user_permission_keys = nil
    
  self[:role_id] = value
end

- (Object) save_permission_keys (protected)

Saves any updated permission keys to the database for this user. Any permission keys that are specifically given to this user and are also in the user's role will be removed. So, the only permission keys added here will be those in addition to the user's role.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/challah/authable/user.rb', line 246

def save_permission_keys
  if @permission_keys and Array === @permission_keys
    self.permission_users(true).clear

    @permission_keys = @permission_keys.uniq - self.role.permission_keys

    @permission_keys.each do |key|
      permission = ::Permission[key]

      if permission
        self.permission_users.create(:permission_id => permission.id, :user_id => self.id)
      end
    end

    @permission_keys = nil
    @user_permission_keys = nil

    self.permissions(true).collect(&:key)
  end
end

- (Object) small_name

shortened name, just includes the first name and last initial



183
184
185
# File 'lib/challah/authable/user.rb', line 183

def small_name
  "#{first_name.to_s.titleize} #{last_name.to_s.first.upcase}."
end

- (Object) successful_authentication!(ip_address = nil)

Called when a Session validation is successful, and this user has been authenticated.



189
190
191
192
# File 'lib/challah/authable/user.rb', line 189

def successful_authentication!(ip_address = nil)
  self.update_attributes(:last_session_at => Time.now, :last_session_ip => ip_address)
  self.increment!(:session_count, 1)
end

- (Object) update_account_attributes(attributes_to_update = {})

Update a user's own account. This differsfrom User#update_attributes because it won't let a user update their own role and other protected elements.

All attributes on the user model can be updated, except for the ones listed below.



198
199
200
201
202
# File 'lib/challah/authable/user.rb', line 198

def (attributes_to_update = {})
  protected_attributes = self.class.protected_attributes.clone.flatten          
  attributes_to_update.keys.each { |key| attributes_to_update.delete(key) if protected_attributes.include?(key.to_s) }        
  self.update_attributes(attributes_to_update)
end

- (Object) user_permission_keys

Returns the permission keys used by this specific user, does not include any role-based permissions.



205
206
207
# File 'lib/challah/authable/user.rb', line 205

def user_permission_keys
  new_record? ? [] : self.permissions(true).collect(&:key)
end

- (Boolean) valid_session?

Is this user valid and ready for a user session?

Override this method if you need to check for a particular configuration on each page request.

Returns:

  • (Boolean)


212
213
214
# File 'lib/challah/authable/user.rb', line 212

def valid_session?
  self.active?
end

- (Object) validate_new_password (protected)

validation call for new passwords, make sure the password is confirmed, and is >= 4 characters



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/challah/authable/user.rb', line 268

def validate_new_password
  if new_record? and self.read_attribute(:crypted_password).to_s.blank? and !@password_updated
    errors.add :password, :blank
  elsif @password_updated
    if @password.to_s.size < 4
      errors.add :password, :invalid_password
    elsif @password.to_s != @password_confirmation.to_s
      errors.add :password, :no_match_password
    end
  end
end