-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
219 lines (187 loc) · 5.5 KB
/
models.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
class Professor
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
belongs_to :school
belongs_to :department
has n, :mannerism
has n, :board
end
class Category
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :mannerism
end
class Department
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :professor
end
class Student
include DataMapper::Resource
property :id, Serial
property :first_name, String, :required => true, :messages => { :presence => 'A First Name is required.' }
property :last_name, String, :required => true, :messages => { :presence => 'A Last Name address is required.' }
property :email, String, :required => true, :unique => true,
# line commented out for personal testing
#:format => :email_address,
:messages => {
:presence => 'An email address is required.',
:is_unique => 'That school name is already taken.',
:format => 'Please make sure you entered your email address as `[email protected]`.'
}
property :pwhash, String, :required => true
property :mobileauth, String
property :permissions, String, :accessor => :protected, :required => true, :default => 'standard'
property :regtime, Time, :required => true
property :item_enabled,Boolean, :default => false, :required => true
belongs_to :school, :required => false
has n, :board
# before :save, :categorize
before :save, :set_school
def password=(pass)
self.pwhash = Student.encrypt(pass, self.email.to_s)
self.regtime = Time.now
end
def self.encrypt(pass, salt)
totalpword = pass + salt
Digest::SHA1.hexdigest(totalpword)
end
def self.auth(login, pass)
u = Student.first(:email => login)
return nil if(u.nil? || !u.item_enabled)
return u if Student.encrypt(pass, login) == u.pwhash
end
# a more secure auth method where the client has pre hashed the password, Ideally we'd like to do this all the time
def self.sauth(login, passhash)
u = Student.first(:email => login, :pwhash => passhash)
pp u
pp login
pp passhash
return nil if(u.nil? || !u.item_enabled)
pp "Apparently I didn't return nil..."
return u
end
def regcode
Digest::SHA1.hexdigest(self.email + self.pwhash)
end
def available?
# Was the account registered more than 3 days ago AND has it NOT been validated
((Time.now.to_i - self.regtime.to_i)/60/60/24 > 3 && !self.item_enabled)
end
def validate!(code, time_in_seconds)
if(self.item_enabled && time_in_seconds.to_i == self.regtime.to_i && code == self.regcode)
return true
end
if(time_in_seconds.to_i == self.regtime.to_i && code == self.regcode && !self.available?)
pp "User #{self.email} has been validated..."
self.item_enabled = true
self.save
else
return false
end
return self.item_enabled
end
def superadmin?
self.permissions == "superadmin"
end
def superadmin!
self.permissions = "superadmin"
end
def admin?
(!(self.permissions =~ /^(super)?admin$/).nil? && !self.permissions.nil?)
end
def admin!
self.permissions = "admin"
end
def supermod?
(!(self.permissions =~ /^((super)?(admin)?|supermod)$/).nil? && !self.permissions.nil?)
end
def supermod!
self.permissions = "supermod"
end
def mod?
(!(self.permissions =~ /^((super)?(admin|mod))$/).nil? && !self.permissions.nil?)
end
def mod!
self.permissions = "mod"
end
def standard!
self.permissions = "standard"
end
def get_permissions
if(!self.permissions.nil?)
self.permissions
else
'standard'
end
end
def url_safe_email
CGI::escape(self.email)
end
def set_school
if(self.email)
ext = self.email.scan(/^.*@(.*)$/)[0]
pp ext
self.school = School.first(:emailext => ext)
end
end
end
class School
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true, :unique => true,
:messages => {
:presence => 'A school name is required.',
:is_unique => 'That school name is already taken.'
}
property :short, String, :required => true
property :emailext, String, :required => true, :unique => true,
:messages => {
:presence => 'You must enter an email extension for this school',
:is_unique => "That school name is already taken."
}
property :item_enabled, Boolean, :default => false, :required => true
has n, :student
has n, :professor
def url_safe_name
CGI::escape(self.name)
end
end
class Board
include DataMapper::Resource
property :id, Serial
belongs_to :student
belongs_to :professor
has n, :board_link
has n, :mannerism, :through => :board_link
def generate
mannerisms = Mannerism.all(:professor => professor)
if(self.mannerism.count != 25 && mannerisms.count >= 25)
shuffled = mannerisms.shuffle[0..24]
pp shuffled
pp shuffled.class
self.mannerism = shuffled
self.save
return true
else
return nil
end
end
end
class Mannerism
include DataMapper::Resource
property :id, Serial
property :text, Text
belongs_to :professor
belongs_to :category
has n, :board_link
has n, :board, :through => :board_link
end
class BoardLink
include DataMapper::Resource
belongs_to :board, :key => true
belongs_to :mannerism, :key => true
end