Class: User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- User
- Includes:
- AASM
- Defined in:
- vendor/plugins/authentication/app/models/user.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Virtual attribute for the unencrypted password.
Class Method Summary collapse
-
.authenticate(login, password) ⇒ Object
Authenticates a user by their login name and unencrypted password.
-
.encrypt(password, salt) ⇒ Object
Encrypts some data with the salt.
Instance Method Summary collapse
- #authenticated?(password) ⇒ Boolean
- #authorized_plugins ⇒ Object
- #create_reset_code ⇒ Object
- #delete_reset_code ⇒ Object
-
#encrypt(password) ⇒ Object
Encrypts the password with the user salt.
- #forget_me ⇒ Object
- #plugins=(plugin_titles) ⇒ Object
-
#recently_activated? ⇒ Boolean
Returns true if the user has just been activated.
- #recently_reset? ⇒ Boolean
-
#remember_me ⇒ Object
These create and unset the fields required for remembering users between browser closes.
- #remember_me_for(time) ⇒ Object
- #remember_me_until(time) ⇒ Object
- #remember_token? ⇒ Boolean
- #ui_deletable?(current_user = self) ⇒ Boolean
Instance Attribute Details
#password ⇒ Object
Virtual attribute for the unencrypted password
26 27 28 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 26 def password @password end |
Class Method Details
.authenticate(login, password) ⇒ Object
Authenticates a user by their login name and unencrypted password. Returns the user or nil.
47 48 49 50 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 47 def self.authenticate(login, password) u = find_in_state :first, :active, :conditions => {:login => login} # need to get the salt u && u.authenticated?(password) ? u : nil end |
.encrypt(password, salt) ⇒ Object
Encrypts some data with the salt.
53 54 55 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 53 def self.encrypt(password, salt) Digest::SHA1.hexdigest("--#{salt}--#{password}--") end |
Instance Method Details
#authenticated?(password) ⇒ Boolean
62 63 64 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 62 def authenticated?(password) crypted_password == encrypt(password) end |
#authorized_plugins ⇒ Object
76 77 78 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 76 def self.plugins.collect {|p| p.title} | Refinery::Plugins.always_allowed.titles end |
#create_reset_code ⇒ Object
114 115 116 117 118 119 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 114 def create_reset_code @reset = true code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ) self.attributes = {:reset_code => code[0..6]} save(false) end |
#delete_reset_code ⇒ Object
125 126 127 128 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 125 def delete_reset_code self.attributes = {:reset_code => nil} save(false) end |
#encrypt(password) ⇒ Object
Encrypts the password with the user salt
58 59 60 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 58 def encrypt(password) self.class.encrypt(password, salt) end |
#forget_me ⇒ Object
99 100 101 102 103 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 99 def forget_me self.remember_token_expires_at = nil self.remember_token = nil save(false) end |
#plugins=(plugin_titles) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 66 def plugins=(plugin_titles) unless self.new_record? # don't add plugins when the user_id is NULL. self.plugins.delete_all plugin_titles.each do |plugin_title| self.plugins.find_or_create_by_title(plugin_title) if plugin_title.is_a?(String) end end end |
#recently_activated? ⇒ Boolean
Returns true if the user has just been activated.
106 107 108 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 106 def recently_activated? @activated end |
#recently_reset? ⇒ Boolean
121 122 123 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 121 def recently_reset? @reset end |
#remember_me ⇒ Object
These create and unset the fields required for remembering users between browser closes
85 86 87 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 85 def remember_me remember_me_for 2.weeks end |
#remember_me_for(time) ⇒ Object
89 90 91 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 89 def remember_me_for(time) remember_me_until time.from_now.utc end |
#remember_me_until(time) ⇒ Object
93 94 95 96 97 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 93 def remember_me_until(time) self.remember_token_expires_at = time self.remember_token = encrypt("#{email}--#{remember_token_expires_at}") save(false) end |
#remember_token? ⇒ Boolean
80 81 82 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 80 def remember_token? remember_token_expires_at && Time.now.utc < remember_token_expires_at end |
#ui_deletable?(current_user = self) ⇒ Boolean
110 111 112 |
# File 'vendor/plugins/authentication/app/models/user.rb', line 110 def ui_deletable?(current_user = self) !self.superuser and User.count > 1 and (current_user.nil? or self.id != current_user.id) end |