Module: Hobo::Model::UserBase

Defined in:
lib/hobo/model/user_base.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

AUTHENTICATION_FIELDS =
[:salt, :crypted_password, :remember_token, :remember_token_expires_at]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_user_modelObject



9
10
11
# File 'lib/hobo/model/user_base.rb', line 9

def self.default_user_model
  @user_models.first._?.constantize
end

.included(base) ⇒ Object

Extend the base class with AuthenticatedUser functionality This includes:

  • plaintext password during login and encrypted password in the database

  • plaintext password validation

  • login token for rembering a login during multiple browser sessions



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hobo/model/user_base.rb', line 20

def self.included(base)
  @user_models << base.name

  base.extend(ClassMethods)

  base.class_eval do

    fields do
      crypted_password          :string, :limit => 40
      salt                      :string, :limit => 40
      remember_token            :string
      remember_token_expires_at :datetime
    end

    validates_confirmation_of :password,              :if => :new_password_required?
    validate :validate_password
    validate :validate_current_password_when_changing_password

    # Virtual attributes for setting and changing the password
    # note that :password_confirmation= is also defined by
    # validates_confirmation_of, so this line must follow any
    # validates_confirmation_of statements.
    # https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/530
    attr_accessor :current_password, :password, :password_confirmation, :type => :password

    before_save :encrypt_password, :downcase_email
    after_save :stash_current_password

    never_show *AUTHENTICATION_FIELDS

    attr_protected *AUTHENTICATION_FIELDS


  end
end

Instance Method Details

#account_active?Boolean

Returns:



102
103
104
# File 'lib/hobo/model/user_base.rb', line 102

def 
  !self.class.has_lifecycle? || lifecycle.active_state?
end

#authenticated?(password) ⇒ Boolean

Check if the encrypted passwords match

Returns:



112
113
114
# File 'lib/hobo/model/user_base.rb', line 112

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

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



107
108
109
# File 'lib/hobo/model/user_base.rb', line 107

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

#forget_meObject

Expire the login token, resulting in a forced login next time.



129
130
131
132
133
# File 'lib/hobo/model/user_base.rb', line 129

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save(:validate => :false)
end

#guest?Boolean

Returns:



135
136
137
# File 'lib/hobo/model/user_base.rb', line 135

def guest?
  false
end

#remember_meObject

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



122
123
124
125
126
# File 'lib/hobo/model/user_base.rb', line 122

def remember_me
  self.remember_token_expires_at = 2.weeks.from_now.utc
  self.remember_token            = encrypt("#{}--#{remember_token_expires_at}")
  save(:validate => false)
end

#remember_token?Boolean

Do we still need to remember the login token, or has it expired?

Returns:



117
118
119
# File 'lib/hobo/model/user_base.rb', line 117

def remember_token?
  remember_token_expires_at && Time.now.utc < remember_token_expires_at
end

#signed_up?Boolean

Returns:



139
140
141
# File 'lib/hobo/model/user_base.rb', line 139

def signed_up?
  true
end