forked from nedludd/sinatra-authentication
/
datamapper_user.rb
39 lines (31 loc) · 1.05 KB
/
datamapper_user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class DmUser
include DataMapper::Resource
property :id, Serial
property :email, String, :length => (5..40), :unique => true, :format => :email_address
property :hashed_password, String
property :salt, String
property :created_at, DateTime
property :permission_level, Integer, :default => 1
attr_accessor :password, :password_confirmation
#protected equievelant? :protected => true doesn't exist in dm 0.10.0
#protected :id, :salt
#doesn't behave correctly, I'm not even sure why I did this.
validates_presence_of :password_confirmation, :unless => Proc.new { |t| t.hashed_password }
validates_presence_of :password, :unless => Proc.new { |t| t.hashed_password }
validates_confirmation_of :password
def password=(pass)
@password = pass
self.salt = User.random_string(10) if !self.salt
self.hashed_password = User.encrypt(@password, self.salt)
end
def admin?
self.permission_level == -1 || self.id == 1
end
def site_admin?
self.id == 1
end
protected
def method_missing(m, *args)
return false
end
end