-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.rb
121 lines (100 loc) · 3.58 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright © 2011, David McIntosh <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
require 'couchrest_extended_document'
require 'bcrypt'
require 'cgi'
require 'logger'
require 'image_size'
require 'fastimage_resize'
class User < CouchRest::ExtendedDocument
@@AVATAR_ATT_NAME = "avatar"
@@AVATAR_S_ATT_NAME = "avatar_s"
property :username
property :password
property :joined, DateTime
property :roles, [String]
property :groups, [String]
property :avatar_modified, Time
view_by :username
unique_id :id_string
# Used by CouchRest for naming new documents
def id_string
return "user_"+self.username
end
def self.for_username(username)
return by_username(:key => username).first
end
def check_password?(password)
logger = Logger.new(STDERR)
if self.password.nil? then
logger.warn "No hash available for user #{self.username}"
return false
end
bPass = BCrypt::Password.new(self.password)
return bPass == password
end
def create_password(password)
self.password=BCrypt::Password.create(password)
end
def displayname
# Ruby 1.9: self.username.encode :xml => :text
CGI.escapeHTML(self.username)
end
def avatar=(file)
if self.has_attachment? @@AVATAR_ATT_NAME then
self.delete_attachment @@AVATAR_ATT_NAME
end
self.create_attachment :file => file, :name => @@AVATAR_ATT_NAME
self.avatar_s=file
self.avatar_modified = Time.now
end
def avatar_s=(file)
# Resize the image to a maximum of 48 pixels wide or high
imageSize = ImageSize.new(File.new(file.path))
old_w = imageSize.width; old_h = imageSize.height
if (imageSize.width > imageSize.height) then
w = 48; h = old_h * ( 48.0 / old_w.to_f )
else
w = old_w * ( 48.0 / old_h.to_f ); h = 48
end
resized = Tempfile.new @@AVATAR_S_ATT_NAME
FastImage.resize(file.path, resized.path, w, h)
# Save the resized attachment, overwriting if one exists
if self.has_attachment? @@AVATAR_S_ATT_NAME then
self.delete_attachment @@AVATAR_S_ATT_NAME
end
self.create_attachment :file => resized, :name => @@AVATAR_S_ATT_NAME
end
def has_avatar?
self.has_attachment? @@AVATAR_ATT_NAME and
self.has_attachment? @@AVATAR_S_ATT_NAME
end
def avatar
self.read_attachment @@AVATAR_ATT_NAME
end
def avatar_s
self.read_attachment @@AVATAR_S_ATT_NAME
end
def has_role?(role)
return self.roles.include? role
end
def in_group?(val)
if val.instance_of? Array then
val.each do |group|
return true if self.groups.include? group
end
end
return self.groups.include? val
end
end