forked from sklise/sinatra-warden-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
46 lines (37 loc) · 1.15 KB
/
model.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
40
41
42
43
44
45
46
DataMapper.setup(:default, "sqlite://#{Dir.pwd}/db.sqlite")
class User
include DataMapper::Resource
include BCrypt
property :id, Serial, key: true
property :username, String, length: 128
property :password, BCryptHash
def authenticate(attempted_password)
# The BCrypt class, which `self.password` is an instance of, has `==` defined to compare a
# test plain text string to the encrypted string and converts `attempted_password` to a BCrypt
# for the comparison.
#
# But don't take my word for it, check out the source: https://github.com/codahale/bcrypt-ruby/blob/master/lib/bcrypt/password.rb#L64-L67
if self.password == attempted_password
true
else
false
end
end
def self.create_first_user
if User.count == 0
@user = User.create(username: "admin")
@user.password = "admin"
@user.save
end
end
def self.get_first_user
self.create_first_user
User.first(id: 1)
end
end
# Tell DataMapper the models are done being defined
DataMapper.finalize
# Update the database to match the properties of User.
DataMapper.auto_upgrade!
# Create a test User
User.create_first_user