Class: User
- Inherits:
-
Object
- Object
- User
- Defined in:
- lib/shortener/server/user.rb
Constant Summary collapse
- ATTRS =
[:id, :name, :email]
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
- .authenticate!(username, password) ⇒ Object
- .available?(username) ⇒ Boolean
- .get_by_id(id) ⇒ Object
- .get_by_token(token) ⇒ Object
- .get_by_username(username) ⇒ Object
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(attrs = {}) ⇒ User
constructor
A new instance of User.
- #reset_token ⇒ Object
- #save ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ User
Returns a new instance of User.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/shortener/server/user.rb', line 42 def initialize(attrs = {}) ATTRS.each do |attr| self.instance_variable_set(:"@#{attr}", attrs[attr.to_s]) end @salt = attrs['salt'] || get_rand @token = attrs['token'] || get_rand @username = attrs['username'] @password = User.hash_password(attrs['password'], @salt) if attrs['password'] @password = attrs['prehashed_password'] if attrs['prehashed_password'] end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/shortener/server/user.rb', line 11 def password @password end |
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
11 12 13 |
# File 'lib/shortener/server/user.rb', line 11 def salt @salt end |
#token ⇒ Object
Returns the value of attribute token.
11 12 13 |
# File 'lib/shortener/server/user.rb', line 11 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/shortener/server/user.rb', line 11 def username @username end |
Class Method Details
.authenticate!(username, password) ⇒ Object
13 14 15 16 17 |
# File 'lib/shortener/server/user.rb', line 13 def self.authenticate!(username, password) user = get_by_username(username) return false unless user ((hash_password(password, user.salt)) == user.password) ? user : false end |
.available?(username) ⇒ Boolean
38 39 40 |
# File 'lib/shortener/server/user.rb', line 38 def self.available?(username) $redis.get("username.to.id:#{username.downcase}").nil? end |
.get_by_id(id) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/shortener/server/user.rb', line 19 def self.get_by_id(id) attrs = $redis.hgetall("user:#{id}") return nil if attrs.empty? attrs['prehashed_password'] = attrs.delete('password') u = User.new(attrs) end |
.get_by_token(token) ⇒ Object
32 33 34 35 36 |
# File 'lib/shortener/server/user.rb', line 32 def self.get_by_token(token) _id = $redis.get("auth:#{token}") return nil unless _id get_by_id(_id) end |
.get_by_username(username) ⇒ Object
26 27 28 29 30 |
# File 'lib/shortener/server/user.rb', line 26 def self.get_by_username(username) _id = $redis.get("username.to.id:#{username.downcase}") return nil unless _id get_by_id(_id) end |
Instance Method Details
#delete ⇒ Object
99 100 101 102 103 104 |
# File 'lib/shortener/server/user.rb', line 99 def delete $redis.del("username.to.id:#{username.downcase}") $redis.del("user:#{id}") $redis.del("auth:#{token}") true end |
#reset_token ⇒ Object
71 72 73 |
# File 'lib/shortener/server/user.rb', line 71 def reset_token self.token = get_rand end |
#save ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/shortener/server/user.rb', line 75 def save self.id = $redis.incr("users.count") unless id $redis.hmset("user:#{id}", "id", id, "name", name, "username", username, "salt", salt, "password", password, "email", email, "token", token) if @prev_username puts "deleting prev_username #{@prev_username}" $redis.del("username.to.id:#{@prev_username.downcase}") @prev_username = nil end if @prev_token $redis.del("auth:#{@prev_token}") @prev_token = nil end $redis.set("username.to.id:#{username.downcase}", id) $redis.set("auth:#{token}", id) return id, nil end |
#to_json ⇒ Object
106 107 108 |
# File 'lib/shortener/server/user.rb', line 106 def to_json {id: id, username: username, name: name, email: email, token: token}.to_json end |