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

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

Instance Method Summary collapse

Instance Method Details

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#change_password(new_password) ⇒ Object

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

#encrypt(password) ⇒ Object

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

#encrypt_passwordObject

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

#forget_meObject

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

#password_required?Boolean

Returns:

  • (Boolean)


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

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

#refresh_tokenObject

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

#remember_meObject

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

#remember_me_for(time) ⇒ Object



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

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

#remember_me_until(time) ⇒ Object



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

#remember_token?Boolean

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