Class: Skyline::User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Skyline::User
- Defined in:
- app/models/skyline/user.rb
Instance Attribute Summary collapse
-
#current_password ⇒ Object
This must be set to change the password (by the user himself).
-
#editing_myself ⇒ Object
This can be set by the admin to force checking of the current password etc.
-
#force_password ⇒ Object
This can be set by the admin to change the PW.
Class Method Summary collapse
-
.authenticate(email, password) ⇒ Object
Authenticates a user with email and password.
- .encrypt(pw) ⇒ Object
- .extract_valid_email_address(email) ⇒ Object
- .per_page ⇒ Object
Instance Method Summary collapse
-
#allow?(right_or_class, suffix = nil) ⇒ Boolean
Check if a user has a specific right.
-
#correct_password?(password) ⇒ Boolean
Check if this users password matches.
-
#destroy_without_callbacks ⇒ Object
Don’t really destroy the object, just set the is_destroyed? flag.
-
#display_name ⇒ Object
Display the name or e-mailaddress of the user for display purposes.
-
#force_password!(pw) ⇒ Object
Forcefully set a password without any validations Directly saves the object.
-
#generate_new_password! ⇒ Object
Generate a new password, set it and return it.
- #viewable_roles ⇒ Object
Instance Attribute Details
#current_password ⇒ Object
This must be set to change the password (by the user himself)
10 11 12 |
# File 'app/models/skyline/user.rb', line 10 def current_password @current_password end |
#editing_myself ⇒ Object
This can be set by the admin to force checking of the current password etc
17 18 19 |
# File 'app/models/skyline/user.rb', line 17 def editing_myself @editing_myself end |
#force_password ⇒ Object
This can be set by the admin to change the PW
13 14 15 |
# File 'app/models/skyline/user.rb', line 13 def force_password @force_password end |
Class Method Details
.authenticate(email, password) ⇒ Object
Authenticates a user with email and password
Returns
- User
-
The user if authentication passed
–
50 51 52 53 |
# File 'app/models/skyline/user.rb', line 50 def authenticate(email,password) user = self.first(:conditions => {:email => email.to_s.downcase, :password => encrypt(password.to_s), :is_destroyed => false}) user && user || false end |
.encrypt(pw) ⇒ Object
55 56 57 |
# File 'app/models/skyline/user.rb', line 55 def encrypt(pw) Digest::SHA1.hexdigest(pw) end |
.extract_valid_email_address(email) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/skyline/user.rb', line 59 def extract_valid_email_address(email) if email.kind_of? TMail::Address return email.address else begin address = TMail::Address.parse(email.to_s) rescue return false end end if address && address.respond_to?(:domain) && address.domain return address.address else return false end end |
.per_page ⇒ Object
76 |
# File 'app/models/skyline/user.rb', line 76 def per_page; 30; end |
Instance Method Details
#allow?(right_or_class, suffix = nil) ⇒ Boolean
Check if a user has a specific right
Parameters
- right_or_class<String,Symbol,~right_prefix>
-
Can be a string or a symbol to check a specific right, or you can pass an object that responds to #right_prefix, right_prefix must return a string
- suffix<String,Symbol>,
-
A suffix to append to the right, mostly usefull in combination with an object
Returns
- <Boolean>
-
true if the right exists in one of the roles fo the user
–
92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/skyline/user.rb', line 92 def allow?(right_or_class,suffix=nil) if right_or_class.respond_to?(:right_prefix) right_or_class = right_or_class.right_prefix end right = [right_or_class,suffix].compact.map(&:to_s).join("_") @rights ||= self.rights.map{|r| r.name }.uniq @rights.include?(right) end |
#correct_password?(password) ⇒ Boolean
Check if this users password matches.
104 105 106 107 108 109 110 |
# File 'app/models/skyline/user.rb', line 104 def correct_password?(password) if self.password_changed? self.password == password.to_s else self.password == self.class.encrypt(password.to_s) end end |
#destroy_without_callbacks ⇒ Object
Don’t really destroy the object, just set the is_destroyed? flag.
148 149 150 151 152 153 154 |
# File 'app/models/skyline/user.rb', line 148 def destroy_without_callbacks unless new_record? self.update_attributes(:is_destroyed => true) end freeze end |
#display_name ⇒ Object
Display the name or e-mailaddress of the user for display purposes.
141 142 143 144 |
# File 'app/models/skyline/user.rb', line 141 def display_name return self.name self.name.present? ? self.name : self.email end |
#force_password!(pw) ⇒ Object
Forcefully set a password without any validations Directly saves the object. –
130 131 132 133 |
# File 'app/models/skyline/user.rb', line 130 def force_password!(pw) self.update_attribute(:password, pw) pw end |
#generate_new_password! ⇒ Object
Generate a new password, set it and return it.
Returns
- String
-
The newly set password
–
122 123 124 125 |
# File 'app/models/skyline/user.rb', line 122 def generate_new_password! pw = SimplePassword.new(8).to_s self.force_password!(pw) end |