Module: Cms::Authentication::Model::InstanceMethods

Defined in:
lib/cms/authentication/model.rb

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) authenticated?(password)

Returns:

  • (Boolean)


60
61
62
# File 'lib/cms/authentication/model.rb', line 60

def authenticated?(password)
  crypted_password == encrypt(password)
end

- (Object) change_password(new_password)

Method to make it easy to change a user's password from the console, not used in the app



51
52
53
# File 'lib/cms/authentication/model.rb', line 51

def change_password(new_password)
  update_attributes(:password => new_password, :password_confirmation => new_password)
end

- (Object) encrypt(password)

Encrypts the password with the user salt



56
57
58
# File 'lib/cms/authentication/model.rb', line 56

def encrypt(password)
  self.class.password_digest(password, salt)
end

- (Object) encrypt_password

before filter



65
66
67
68
69
# File 'lib/cms/authentication/model.rb', line 65

def encrypt_password
  return if password.blank?
  self.salt = self.class.make_token if new_record?
  self.crypted_password = encrypt(password)
end

- (Object) forget_me

Deletes the server-side record of the authentication token. The client-side (browser cookie) and server-side (this remember_token) must always be deleted together.



108
109
110
111
112
# File 'lib/cms/authentication/model.rb', line 108

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save
end

- (Boolean) password_required?

Returns:

  • (Boolean)


71
72
73
# File 'lib/cms/authentication/model.rb', line 71

def password_required?
  crypted_password.blank? || !password.blank?
end

- (Object) refresh_token

refresh token (keeping same expires_at) if it exists



96
97
98
99
100
101
# File 'lib/cms/authentication/model.rb', line 96

def refresh_token
  if remember_token?
    self.remember_token = self.class.make_token 
    save      
  end
end

- (Object) remember_me

These create and unset the fields required for remembering users between browser closes



81
82
83
# File 'lib/cms/authentication/model.rb', line 81

def remember_me
  remember_me_for 2.weeks
end

- (Object) remember_me_for(time)



85
86
87
# File 'lib/cms/authentication/model.rb', line 85

def remember_me_for(time)
  remember_me_until time.from_now.utc
end

- (Object) remember_me_until(time)



89
90
91
92
93
# File 'lib/cms/authentication/model.rb', line 89

def remember_me_until(time)
  self.remember_token_expires_at = time
  self.remember_token            = self.class.make_token
  save
end

- (Boolean) remember_token?

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/cms/authentication/model.rb', line 75

def remember_token?
  (!remember_token.blank?) && 
    remember_token_expires_at && (Time.now.utc < remember_token_expires_at.utc)
end