Description:
modernize
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r574:6250748f34ac - - 1 file changed: 1 inserted, 1 deleted

@@ -50,331 +50,331
50 :if => :email_validation?
50 :if => :email_validation?
51
51
52 # these are for ytopc
52 # these are for ytopc
53 # disable for now
53 # disable for now
54 #validates_presence_of :province
54 #validates_presence_of :province
55
55
56 attr_accessor :password
56 attr_accessor :password
57
57
58 before_save :encrypt_new_password
58 before_save :encrypt_new_password
59 before_save :assign_default_site
59 before_save :assign_default_site
60 before_save :assign_default_contest
60 before_save :assign_default_contest
61
61
62 # this is for will_paginate
62 # this is for will_paginate
63 cattr_reader :per_page
63 cattr_reader :per_page
64 @@per_page = 50
64 @@per_page = 50
65
65
66 def self.authenticate(login, password)
66 def self.authenticate(login, password)
67 user = find_by_login(login)
67 user = find_by_login(login)
68 if user
68 if user
69 return user if user.authenticated?(password)
69 return user if user.authenticated?(password)
70 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
70 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
71 user.password = password
71 user.password = password
72 user.save
72 user.save
73 return user
73 return user
74 end
74 end
75 end
75 end
76 end
76 end
77
77
78 def authenticated?(password)
78 def authenticated?(password)
79 if self.activated
79 if self.activated
80 hashed_password == User.encrypt(password,self.salt)
80 hashed_password == User.encrypt(password,self.salt)
81 else
81 else
82 false
82 false
83 end
83 end
84 end
84 end
85
85
86 def authenticated_by_pop3?(password)
86 def authenticated_by_pop3?(password)
87 Net::POP3.enable_ssl
87 Net::POP3.enable_ssl
88 pop = Net::POP3.new('pops.it.chula.ac.th')
88 pop = Net::POP3.new('pops.it.chula.ac.th')
89 authen = true
89 authen = true
90 begin
90 begin
91 pop.start(login, password)
91 pop.start(login, password)
92 pop.finish
92 pop.finish
93 return true
93 return true
94 rescue
94 rescue
95 return false
95 return false
96 end
96 end
97 end
97 end
98
98
99 def authenticated_by_cucas?(password)
99 def authenticated_by_cucas?(password)
100 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
100 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
101 appid = '41508763e340d5858c00f8c1a0f5a2bb'
101 appid = '41508763e340d5858c00f8c1a0f5a2bb'
102 appsecret ='d9cbb5863091dbe186fded85722a1e31'
102 appsecret ='d9cbb5863091dbe186fded85722a1e31'
103 post_args = {
103 post_args = {
104 'appid' => appid,
104 'appid' => appid,
105 'appsecret' => appsecret,
105 'appsecret' => appsecret,
106 'username' => login,
106 'username' => login,
107 'password' => password
107 'password' => password
108 }
108 }
109
109
110 #simple call
110 #simple call
111 begin
111 begin
112 http = Net::HTTP.new('www.cas.chula.ac.th', 443)
112 http = Net::HTTP.new('www.cas.chula.ac.th', 443)
113 http.use_ssl = true
113 http.use_ssl = true
114 result = [ ]
114 result = [ ]
115 http.start do |http|
115 http.start do |http|
116 req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
116 req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
117 param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
117 param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
118 resp = http.request(req,param)
118 resp = http.request(req,param)
119 result = JSON.parse resp.body
119 result = JSON.parse resp.body
120 end
120 end
121 return true if result["type"] == "beanStudent"
121 return true if result["type"] == "beanStudent"
122 rescue
122 rescue
123 return false
123 return false
124 end
124 end
125 return false
125 return false
126 end
126 end
127
127
128 def admin?
128 def admin?
129 self.roles.detect {|r| r.name == 'admin' }
129 self.roles.detect {|r| r.name == 'admin' }
130 end
130 end
131
131
132 def email_for_editing
132 def email_for_editing
133 if self.email==nil
133 if self.email==nil
134 "(unknown)"
134 "(unknown)"
135 elsif self.email==''
135 elsif self.email==''
136 "(blank)"
136 "(blank)"
137 else
137 else
138 self.email
138 self.email
139 end
139 end
140 end
140 end
141
141
142 def email_for_editing=(e)
142 def email_for_editing=(e)
143 self.email=e
143 self.email=e
144 end
144 end
145
145
146 def alias_for_editing
146 def alias_for_editing
147 if self.alias==nil
147 if self.alias==nil
148 "(unknown)"
148 "(unknown)"
149 elsif self.alias==''
149 elsif self.alias==''
150 "(blank)"
150 "(blank)"
151 else
151 else
152 self.alias
152 self.alias
153 end
153 end
154 end
154 end
155
155
156 def alias_for_editing=(e)
156 def alias_for_editing=(e)
157 self.alias=e
157 self.alias=e
158 end
158 end
159
159
160 def activation_key
160 def activation_key
161 if self.hashed_password==nil
161 if self.hashed_password==nil
162 encrypt_new_password
162 encrypt_new_password
163 end
163 end
164 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
164 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
165 end
165 end
166
166
167 def verify_activation_key(key)
167 def verify_activation_key(key)
168 key == activation_key
168 key == activation_key
169 end
169 end
170
170
171 def self.random_password(length=5)
171 def self.random_password(length=5)
172 chars = 'abcdefghjkmnopqrstuvwxyz'
172 chars = 'abcdefghjkmnopqrstuvwxyz'
173 password = ''
173 password = ''
174 length.times { password << chars[rand(chars.length - 1)] }
174 length.times { password << chars[rand(chars.length - 1)] }
175 password
175 password
176 end
176 end
177
177
178 def self.find_non_admin_with_prefix(prefix='')
178 def self.find_non_admin_with_prefix(prefix='')
179 users = User.find(:all)
179 users = User.find(:all)
180 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
180 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
181 end
181 end
182
182
183 # Contest information
183 # Contest information
184
184
185 def self.find_users_with_no_contest()
185 def self.find_users_with_no_contest()
186 users = User.find(:all)
186 users = User.find(:all)
187 return users.find_all { |u| u.contests.length == 0 }
187 return users.find_all { |u| u.contests.length == 0 }
188 end
188 end
189
189
190
190
191 def contest_time_left
191 def contest_time_left
192 if GraderConfiguration.contest_mode?
192 if GraderConfiguration.contest_mode?
193 return nil if site==nil
193 return nil if site==nil
194 return site.time_left
194 return site.time_left
195 elsif GraderConfiguration.indv_contest_mode?
195 elsif GraderConfiguration.indv_contest_mode?
196 time_limit = GraderConfiguration.contest_time_limit
196 time_limit = GraderConfiguration.contest_time_limit
197 if time_limit == nil
197 if time_limit == nil
198 return nil
198 return nil
199 end
199 end
200 if contest_stat==nil or contest_stat.started_at==nil
200 if contest_stat==nil or contest_stat.started_at==nil
201 return (Time.now.gmtime + time_limit) - Time.now.gmtime
201 return (Time.now.gmtime + time_limit) - Time.now.gmtime
202 else
202 else
203 finish_time = contest_stat.started_at + time_limit
203 finish_time = contest_stat.started_at + time_limit
204 current_time = Time.now.gmtime
204 current_time = Time.now.gmtime
205 if current_time > finish_time
205 if current_time > finish_time
206 return 0
206 return 0
207 else
207 else
208 return finish_time - current_time
208 return finish_time - current_time
209 end
209 end
210 end
210 end
211 else
211 else
212 return nil
212 return nil
213 end
213 end
214 end
214 end
215
215
216 def contest_finished?
216 def contest_finished?
217 if GraderConfiguration.contest_mode?
217 if GraderConfiguration.contest_mode?
218 return false if site==nil
218 return false if site==nil
219 return site.finished?
219 return site.finished?
220 elsif GraderConfiguration.indv_contest_mode?
220 elsif GraderConfiguration.indv_contest_mode?
221 return false if self.contest_stat(true)==nil
221 return false if self.contest_stat(true)==nil
222 return contest_time_left == 0
222 return contest_time_left == 0
223 else
223 else
224 return false
224 return false
225 end
225 end
226 end
226 end
227
227
228 def contest_started?
228 def contest_started?
229 if GraderConfiguration.indv_contest_mode?
229 if GraderConfiguration.indv_contest_mode?
230 stat = self.contest_stat
230 stat = self.contest_stat
231 return ((stat != nil) and (stat.started_at != nil))
231 return ((stat != nil) and (stat.started_at != nil))
232 elsif GraderConfiguration.contest_mode?
232 elsif GraderConfiguration.contest_mode?
233 return true if site==nil
233 return true if site==nil
234 return site.started
234 return site.started
235 else
235 else
236 return true
236 return true
237 end
237 end
238 end
238 end
239
239
240 def update_start_time
240 def update_start_time
241 stat = self.contest_stat
241 stat = self.contest_stat
242 - if stat == nil or stat.started_at == nil
242 + if (stat.nil?) or (stat.started_at.nil?)
243 stat ||= UserContestStat.new(:user => self)
243 stat ||= UserContestStat.new(:user => self)
244 stat.started_at = Time.now.gmtime
244 stat.started_at = Time.now.gmtime
245 stat.save
245 stat.save
246 end
246 end
247 end
247 end
248
248
249 def problem_in_user_contests?(problem)
249 def problem_in_user_contests?(problem)
250 problem_contests = problem.contests.all
250 problem_contests = problem.contests.all
251
251
252 if problem_contests.length == 0 # this is public contest
252 if problem_contests.length == 0 # this is public contest
253 return true
253 return true
254 end
254 end
255
255
256 contests.each do |contest|
256 contests.each do |contest|
257 if problem_contests.find {|c| c.id == contest.id }
257 if problem_contests.find {|c| c.id == contest.id }
258 return true
258 return true
259 end
259 end
260 end
260 end
261 return false
261 return false
262 end
262 end
263
263
264 def available_problems_group_by_contests
264 def available_problems_group_by_contests
265 contest_problems = []
265 contest_problems = []
266 pin = {}
266 pin = {}
267 contests.enabled.each do |contest|
267 contests.enabled.each do |contest|
268 available_problems = contest.problems.available
268 available_problems = contest.problems.available
269 contest_problems << {
269 contest_problems << {
270 :contest => contest,
270 :contest => contest,
271 :problems => available_problems
271 :problems => available_problems
272 }
272 }
273 available_problems.each {|p| pin[p.id] = true}
273 available_problems.each {|p| pin[p.id] = true}
274 end
274 end
275 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
275 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
276 contest_problems << {
276 contest_problems << {
277 :contest => nil,
277 :contest => nil,
278 :problems => other_avaiable_problems
278 :problems => other_avaiable_problems
279 }
279 }
280 return contest_problems
280 return contest_problems
281 end
281 end
282
282
283 def available_problems
283 def available_problems
284 if not GraderConfiguration.multicontests?
284 if not GraderConfiguration.multicontests?
285 return Problem.find_available_problems
285 return Problem.find_available_problems
286 else
286 else
287 contest_problems = []
287 contest_problems = []
288 pin = {}
288 pin = {}
289 contests.enabled.each do |contest|
289 contests.enabled.each do |contest|
290 contest.problems.available.each do |problem|
290 contest.problems.available.each do |problem|
291 if not pin.has_key? problem.id
291 if not pin.has_key? problem.id
292 contest_problems << problem
292 contest_problems << problem
293 end
293 end
294 pin[problem.id] = true
294 pin[problem.id] = true
295 end
295 end
296 end
296 end
297 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
297 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
298 return contest_problems + other_avaiable_problems
298 return contest_problems + other_avaiable_problems
299 end
299 end
300 end
300 end
301
301
302 def can_view_problem?(problem)
302 def can_view_problem?(problem)
303 if not GraderConfiguration.multicontests?
303 if not GraderConfiguration.multicontests?
304 return problem.available
304 return problem.available
305 else
305 else
306 return problem_in_user_contests? problem
306 return problem_in_user_contests? problem
307 end
307 end
308 end
308 end
309
309
310 def self.clear_last_login
310 def self.clear_last_login
311 User.update_all(:last_ip => nil)
311 User.update_all(:last_ip => nil)
312 end
312 end
313
313
314 protected
314 protected
315 def encrypt_new_password
315 def encrypt_new_password
316 return if password.blank?
316 return if password.blank?
317 self.salt = (10+rand(90)).to_s
317 self.salt = (10+rand(90)).to_s
318 self.hashed_password = User.encrypt(self.password,self.salt)
318 self.hashed_password = User.encrypt(self.password,self.salt)
319 end
319 end
320
320
321 def assign_default_site
321 def assign_default_site
322 # have to catch error when migrating (because self.site is not available).
322 # have to catch error when migrating (because self.site is not available).
323 begin
323 begin
324 if self.site==nil
324 if self.site==nil
325 self.site = Site.find_by_name('default')
325 self.site = Site.find_by_name('default')
326 if self.site==nil
326 if self.site==nil
327 self.site = Site.find(1) # when 'default has be renamed'
327 self.site = Site.find(1) # when 'default has be renamed'
328 end
328 end
329 end
329 end
330 rescue
330 rescue
331 end
331 end
332 end
332 end
333
333
334 def assign_default_contest
334 def assign_default_contest
335 # have to catch error when migrating (because self.site is not available).
335 # have to catch error when migrating (because self.site is not available).
336 begin
336 begin
337 if self.contests.length == 0
337 if self.contests.length == 0
338 default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name'])
338 default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name'])
339 if default_contest
339 if default_contest
340 self.contests = [default_contest]
340 self.contests = [default_contest]
341 end
341 end
342 end
342 end
343 rescue
343 rescue
344 end
344 end
345 end
345 end
346
346
347 def password_required?
347 def password_required?
348 self.hashed_password.blank? || !self.password.blank?
348 self.hashed_password.blank? || !self.password.blank?
349 end
349 end
350
350
351 def self.encrypt(string,salt)
351 def self.encrypt(string,salt)
352 Digest::SHA1.hexdigest(salt + string)
352 Digest::SHA1.hexdigest(salt + string)
353 end
353 end
354
354
355 def uniqueness_of_email_from_activated_users
355 def uniqueness_of_email_from_activated_users
356 user = User.activated_users.find_by_email(self.email)
356 user = User.activated_users.find_by_email(self.email)
357 if user and (user.login != self.login)
357 if user and (user.login != self.login)
358 self.errors.add(:base,"Email has already been taken")
358 self.errors.add(:base,"Email has already been taken")
359 end
359 end
360 end
360 end
361
361
362 def enough_time_interval_between_same_email_registrations
362 def enough_time_interval_between_same_email_registrations
363 return if !self.new_record?
363 return if !self.new_record?
364 return if self.activated
364 return if self.activated
365 open_user = User.find_by_email(self.email,
365 open_user = User.find_by_email(self.email,
366 :order => 'created_at DESC')
366 :order => 'created_at DESC')
367 if open_user and open_user.created_at and
367 if open_user and open_user.created_at and
368 (open_user.created_at > Time.now.gmtime - 5.minutes)
368 (open_user.created_at > Time.now.gmtime - 5.minutes)
369 self.errors.add(:base,"There are already unactivated registrations with this e-mail address (please wait for 5 minutes)")
369 self.errors.add(:base,"There are already unactivated registrations with this e-mail address (please wait for 5 minutes)")
370 end
370 end
371 end
371 end
372
372
373 def email_validation?
373 def email_validation?
374 begin
374 begin
375 return VALIDATE_USER_EMAILS
375 return VALIDATE_USER_EMAILS
376 rescue
376 rescue
377 return false
377 return false
378 end
378 end
379 end
379 end
380 end
380 end
You need to be logged in to leave comments. Login now