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

r846:4279bf26f98b - - 1 file changed: 78 inserted, 5 deleted

@@ -88,341 +88,414
88 end
88 end
89 end
89 end
90
90
91 def login_with_name
91 def login_with_name
92 "[#{login}] #{full_name}"
92 "[#{login}] #{full_name}"
93 end
93 end
94
94
95 def authenticated_by_cucas?(password)
95 def authenticated_by_cucas?(password)
96 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
96 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
97 appid = '41508763e340d5858c00f8c1a0f5a2bb'
97 appid = '41508763e340d5858c00f8c1a0f5a2bb'
98 appsecret ='d9cbb5863091dbe186fded85722a1e31'
98 appsecret ='d9cbb5863091dbe186fded85722a1e31'
99 post_args = {
99 post_args = {
100 'appid' => appid,
100 'appid' => appid,
101 'appsecret' => appsecret,
101 'appsecret' => appsecret,
102 'username' => login,
102 'username' => login,
103 'password' => password
103 'password' => password
104 }
104 }
105
105
106 #simple call
106 #simple call
107 begin
107 begin
108 http = Net::HTTP.new('www.cas.chula.ac.th', 443)
108 http = Net::HTTP.new('www.cas.chula.ac.th', 443)
109 http.use_ssl = true
109 http.use_ssl = true
110 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
110 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
111 result = [ ]
111 result = [ ]
112 http.start do |http|
112 http.start do |http|
113 req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
113 req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
114 #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate')
114 #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate')
115 #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate')
115 #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate')
116 param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
116 param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
117 resp = http.request(req,param)
117 resp = http.request(req,param)
118 result = JSON.parse resp.body
118 result = JSON.parse resp.body
119 puts result
119 puts result
120 end
120 end
121 return true if result["type"] == "beanStudent"
121 return true if result["type"] == "beanStudent"
122 rescue => e
122 rescue => e
123 puts e
123 puts e
124 puts e.message
124 puts e.message
125 return false
125 return false
126 end
126 end
127 return false
127 return false
128 end
128 end
129
129
130 def admin?
130 def admin?
131 has_role?('admin')
131 has_role?('admin')
132 end
132 end
133
133
134 def has_role?(role)
134 def has_role?(role)
135 self.roles.where(name: role).count > 0
135 self.roles.where(name: role).count > 0
136 end
136 end
137
137
138 def email_for_editing
138 def email_for_editing
139 if self.email==nil
139 if self.email==nil
140 "(unknown)"
140 "(unknown)"
141 elsif self.email==''
141 elsif self.email==''
142 "(blank)"
142 "(blank)"
143 else
143 else
144 self.email
144 self.email
145 end
145 end
146 end
146 end
147
147
148 def email_for_editing=(e)
148 def email_for_editing=(e)
149 self.email=e
149 self.email=e
150 end
150 end
151
151
152 def alias_for_editing
152 def alias_for_editing
153 if self.alias==nil
153 if self.alias==nil
154 "(unknown)"
154 "(unknown)"
155 elsif self.alias==''
155 elsif self.alias==''
156 "(blank)"
156 "(blank)"
157 else
157 else
158 self.alias
158 self.alias
159 end
159 end
160 end
160 end
161
161
162 def alias_for_editing=(e)
162 def alias_for_editing=(e)
163 self.alias=e
163 self.alias=e
164 end
164 end
165
165
166 def activation_key
166 def activation_key
167 if self.hashed_password==nil
167 if self.hashed_password==nil
168 encrypt_new_password
168 encrypt_new_password
169 end
169 end
170 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
170 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
171 end
171 end
172
172
173 def verify_activation_key(key)
173 def verify_activation_key(key)
174 key == activation_key
174 key == activation_key
175 end
175 end
176
176
177 def self.random_password(length=5)
177 def self.random_password(length=5)
178 chars = 'abcdefghjkmnopqrstuvwxyz'
178 chars = 'abcdefghjkmnopqrstuvwxyz'
179 password = ''
179 password = ''
180 length.times { password << chars[rand(chars.length - 1)] }
180 length.times { password << chars[rand(chars.length - 1)] }
181 password
181 password
182 end
182 end
183
183
184 - def self.find_non_admin_with_prefix(prefix='')
185 - users = User.all
186 - return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
187 - end
188 -
189 # Contest information
184 # Contest information
190
185
191 def self.find_users_with_no_contest()
186 def self.find_users_with_no_contest()
192 users = User.all
187 users = User.all
193 return users.find_all { |u| u.contests.length == 0 }
188 return users.find_all { |u| u.contests.length == 0 }
194 end
189 end
195
190
196
191
197 def contest_time_left
192 def contest_time_left
198 if GraderConfiguration.contest_mode?
193 if GraderConfiguration.contest_mode?
199 return nil if site==nil
194 return nil if site==nil
200 return site.time_left
195 return site.time_left
201 elsif GraderConfiguration.indv_contest_mode?
196 elsif GraderConfiguration.indv_contest_mode?
202 time_limit = GraderConfiguration.contest_time_limit
197 time_limit = GraderConfiguration.contest_time_limit
203 if time_limit == nil
198 if time_limit == nil
204 return nil
199 return nil
205 end
200 end
206 if contest_stat==nil or contest_stat.started_at==nil
201 if contest_stat==nil or contest_stat.started_at==nil
207 return (Time.now.gmtime + time_limit) - Time.now.gmtime
202 return (Time.now.gmtime + time_limit) - Time.now.gmtime
208 else
203 else
209 finish_time = contest_stat.started_at + time_limit
204 finish_time = contest_stat.started_at + time_limit
210 current_time = Time.now.gmtime
205 current_time = Time.now.gmtime
211 if current_time > finish_time
206 if current_time > finish_time
212 return 0
207 return 0
213 else
208 else
214 return finish_time - current_time
209 return finish_time - current_time
215 end
210 end
216 end
211 end
217 else
212 else
218 return nil
213 return nil
219 end
214 end
220 end
215 end
221
216
222 def contest_finished?
217 def contest_finished?
223 if GraderConfiguration.contest_mode?
218 if GraderConfiguration.contest_mode?
224 return false if site==nil
219 return false if site==nil
225 return site.finished?
220 return site.finished?
226 elsif GraderConfiguration.indv_contest_mode?
221 elsif GraderConfiguration.indv_contest_mode?
227 return false if self.contest_stat==nil
222 return false if self.contest_stat==nil
228 return contest_time_left == 0
223 return contest_time_left == 0
229 else
224 else
230 return false
225 return false
231 end
226 end
232 end
227 end
233
228
234 def contest_started?
229 def contest_started?
235 if GraderConfiguration.indv_contest_mode?
230 if GraderConfiguration.indv_contest_mode?
236 stat = self.contest_stat
231 stat = self.contest_stat
237 return ((stat != nil) and (stat.started_at != nil))
232 return ((stat != nil) and (stat.started_at != nil))
238 elsif GraderConfiguration.contest_mode?
233 elsif GraderConfiguration.contest_mode?
239 return true if site==nil
234 return true if site==nil
240 return site.started
235 return site.started
241 else
236 else
242 return true
237 return true
243 end
238 end
244 end
239 end
245
240
246 def update_start_time
241 def update_start_time
247 stat = self.contest_stat
242 stat = self.contest_stat
248 if stat.nil? or stat.started_at.nil?
243 if stat.nil? or stat.started_at.nil?
249 stat ||= UserContestStat.new(:user => self)
244 stat ||= UserContestStat.new(:user => self)
250 stat.started_at = Time.now.gmtime
245 stat.started_at = Time.now.gmtime
251 stat.save
246 stat.save
252 end
247 end
253 end
248 end
254
249
255 def problem_in_user_contests?(problem)
250 def problem_in_user_contests?(problem)
256 problem_contests = problem.contests.all
251 problem_contests = problem.contests.all
257
252
258 if problem_contests.length == 0 # this is public contest
253 if problem_contests.length == 0 # this is public contest
259 return true
254 return true
260 end
255 end
261
256
262 contests.each do |contest|
257 contests.each do |contest|
263 if problem_contests.find {|c| c.id == contest.id }
258 if problem_contests.find {|c| c.id == contest.id }
264 return true
259 return true
265 end
260 end
266 end
261 end
267 return false
262 return false
268 end
263 end
269
264
270 def available_problems_group_by_contests
265 def available_problems_group_by_contests
271 contest_problems = []
266 contest_problems = []
272 pin = {}
267 pin = {}
273 contests.enabled.each do |contest|
268 contests.enabled.each do |contest|
274 available_problems = contest.problems.available
269 available_problems = contest.problems.available
275 contest_problems << {
270 contest_problems << {
276 :contest => contest,
271 :contest => contest,
277 :problems => available_problems
272 :problems => available_problems
278 }
273 }
279 available_problems.each {|p| pin[p.id] = true}
274 available_problems.each {|p| pin[p.id] = true}
280 end
275 end
281 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
276 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
282 contest_problems << {
277 contest_problems << {
283 :contest => nil,
278 :contest => nil,
284 :problems => other_avaiable_problems
279 :problems => other_avaiable_problems
285 }
280 }
286 return contest_problems
281 return contest_problems
287 end
282 end
288
283
289 def solve_all_available_problems?
284 def solve_all_available_problems?
290 available_problems.each do |p|
285 available_problems.each do |p|
291 u = self
286 u = self
292 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
287 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
293 return false if !p or !sub or sub.points < p.full_score
288 return false if !p or !sub or sub.points < p.full_score
294 end
289 end
295 return true
290 return true
296 end
291 end
297
292
298 #get a list of available problem
293 #get a list of available problem
299 def available_problems
294 def available_problems
300 # first, we check if this is normal mode
295 # first, we check if this is normal mode
301 if not GraderConfiguration.multicontests?
296 if not GraderConfiguration.multicontests?
302
297
303 #if this is a normal mode
298 #if this is a normal mode
304 #we show problem based on problem_group, if the config said so
299 #we show problem based on problem_group, if the config said so
305 if GraderConfiguration.use_problem_group?
300 if GraderConfiguration.use_problem_group?
306 return available_problems_in_group
301 return available_problems_in_group
307 else
302 else
308 return Problem.available_problems
303 return Problem.available_problems
309 end
304 end
310 else
305 else
311 #this is multi contest mode
306 #this is multi contest mode
312 contest_problems = []
307 contest_problems = []
313 pin = {}
308 pin = {}
314 contests.enabled.each do |contest|
309 contests.enabled.each do |contest|
315 contest.problems.available.each do |problem|
310 contest.problems.available.each do |problem|
316 if not pin.has_key? problem.id
311 if not pin.has_key? problem.id
317 contest_problems << problem
312 contest_problems << problem
318 end
313 end
319 pin[problem.id] = true
314 pin[problem.id] = true
320 end
315 end
321 end
316 end
322 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
317 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
323 return contest_problems + other_avaiable_problems
318 return contest_problems + other_avaiable_problems
324 end
319 end
325 end
320 end
326
321
327 # new feature, get list of available problem in all enabled group that the user belongs to
322 # new feature, get list of available problem in all enabled group that the user belongs to
328 def available_problems_in_group
323 def available_problems_in_group
329 problem = []
324 problem = []
330 self.groups.where(enabled: true).each do |group|
325 self.groups.where(enabled: true).each do |group|
331 group.problems.where(available: true).each { |p| problem << p }
326 group.problems.where(available: true).each { |p| problem << p }
332 end
327 end
333 problem.uniq!
328 problem.uniq!
334 if problem
329 if problem
335 problem.sort! do |a,b|
330 problem.sort! do |a,b|
336 case
331 case
337 when a.date_added < b.date_added
332 when a.date_added < b.date_added
338 1
333 1
339 when a.date_added > b.date_added
334 when a.date_added > b.date_added
340 -1
335 -1
341 else
336 else
342 a.name <=> b.name
337 a.name <=> b.name
343 end
338 end
344 end
339 end
345 return problem
340 return problem
346 else
341 else
347 return []
342 return []
348 end
343 end
349 end
344 end
350
345
351 #check if the user has the right to view that problem
346 #check if the user has the right to view that problem
352 #this also consider group based problem policy
347 #this also consider group based problem policy
353 def can_view_problem?(problem)
348 def can_view_problem?(problem)
354 return true if admin?
349 return true if admin?
355 return available_problems.include? problem
350 return available_problems.include? problem
356 end
351 end
357
352
358 def self.clear_last_login
353 def self.clear_last_login
359 User.update_all(:last_ip => nil)
354 User.update_all(:last_ip => nil)
360 end
355 end
361
356
357 + #create multiple user, one per lines of input
358 + def self.create_from_list(lines)
359 + error_logins = []
360 + first_error = nil
361 + created_users = []
362 +
363 + lines.split("\n").each do |line|
364 + #split with large limit, this will cause consecutive ',' to be result in a blank
365 + items = line.chomp.split(',',1000)
366 + if items.length>=2
367 + login = items[0]
368 + full_name = items[1]
369 + remark =''
370 + user_alias = ''
371 +
372 + added_random_password = false
373 + added_password = false
374 +
375 + #given password?
376 + if items.length >= 3
377 + if items[2].chomp(" ").length > 0
378 + password = items[2].chomp(" ")
379 + added_password = true
380 + end
381 + else
382 + password = random_password
383 + added_random_password=true;
384 + end
385 +
386 + #given alias?
387 + if items.length>= 4 and items[3].chomp(" ").length > 0;
388 + user_alias = items[3].chomp(" ")
389 + else
390 + user_alias = login
391 + end
392 +
393 + #given remark?
394 + has_remark = false
395 + if items.length>=5
396 + remark = items[4].strip;
397 + has_remark = true
398 + end
399 +
400 + user = User.find_by_login(login)
401 + if (user)
402 + user.full_name = full_name
403 + user.remark = remark if has_remark
404 + user.password = password if added_password || added_random_password
405 + else
406 + #create a random password if none are given
407 + password = random_password unless password
408 + user = User.new({:login => login,
409 + :full_name => full_name,
410 + :password => password,
411 + :password_confirmation => password,
412 + :alias => user_alias,
413 + :remark => remark})
414 + end
415 + user.activated = true
416 +
417 + if user.save
418 + created_users << user
419 + else
420 + error_logins << "'#{login}'"
421 + first_error = user.errors.full_messages.to_sentence unless first_error
422 + end
423 + end
424 + end
425 +
426 + return {error_logins: error_logins, first_error: first_error, created_users: created_users}
427 +
428 + end
429 +
430 + def self.find_non_admin_with_prefix(prefix='')
431 + users = User.all
432 + return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
433 + end
434 +
362 protected
435 protected
363 def encrypt_new_password
436 def encrypt_new_password
364 return if password.blank?
437 return if password.blank?
365 self.salt = (10+rand(90)).to_s
438 self.salt = (10+rand(90)).to_s
366 self.hashed_password = User.encrypt(self.password,self.salt)
439 self.hashed_password = User.encrypt(self.password,self.salt)
367 end
440 end
368
441
369 def assign_default_site
442 def assign_default_site
370 # have to catch error when migrating (because self.site is not available).
443 # have to catch error when migrating (because self.site is not available).
371 begin
444 begin
372 if self.site==nil
445 if self.site==nil
373 self.site = Site.find_by_name('default')
446 self.site = Site.find_by_name('default')
374 if self.site==nil
447 if self.site==nil
375 self.site = Site.find(1) # when 'default has be renamed'
448 self.site = Site.find(1) # when 'default has be renamed'
376 end
449 end
377 end
450 end
378 rescue
451 rescue
379 end
452 end
380 end
453 end
381
454
382 def assign_default_contest
455 def assign_default_contest
383 # have to catch error when migrating (because self.site is not available).
456 # have to catch error when migrating (because self.site is not available).
384 begin
457 begin
385 if self.contests.length == 0
458 if self.contests.length == 0
386 default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name'])
459 default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name'])
387 if default_contest
460 if default_contest
388 self.contests = [default_contest]
461 self.contests = [default_contest]
389 end
462 end
390 end
463 end
391 rescue
464 rescue
392 end
465 end
393 end
466 end
394
467
395 def password_required?
468 def password_required?
396 self.hashed_password.blank? || !self.password.blank?
469 self.hashed_password.blank? || !self.password.blank?
397 end
470 end
398
471
399 def self.encrypt(string,salt)
472 def self.encrypt(string,salt)
400 Digest::SHA1.hexdigest(salt + string)
473 Digest::SHA1.hexdigest(salt + string)
401 end
474 end
402
475
403 def uniqueness_of_email_from_activated_users
476 def uniqueness_of_email_from_activated_users
404 user = User.activated_users.find_by_email(self.email)
477 user = User.activated_users.find_by_email(self.email)
405 if user and (user.login != self.login)
478 if user and (user.login != self.login)
406 self.errors.add(:base,"Email has already been taken")
479 self.errors.add(:base,"Email has already been taken")
407 end
480 end
408 end
481 end
409
482
410 def enough_time_interval_between_same_email_registrations
483 def enough_time_interval_between_same_email_registrations
411 return if !self.new_record?
484 return if !self.new_record?
412 return if self.activated
485 return if self.activated
413 open_user = User.find_by_email(self.email,
486 open_user = User.find_by_email(self.email,
414 :order => 'created_at DESC')
487 :order => 'created_at DESC')
415 if open_user and open_user.created_at and
488 if open_user and open_user.created_at and
416 (open_user.created_at > Time.now.gmtime - 5.minutes)
489 (open_user.created_at > Time.now.gmtime - 5.minutes)
417 self.errors.add(:base,"There are already unactivated registrations with this e-mail address (please wait for 5 minutes)")
490 self.errors.add(:base,"There are already unactivated registrations with this e-mail address (please wait for 5 minutes)")
418 end
491 end
419 end
492 end
420
493
421 def email_validation?
494 def email_validation?
422 begin
495 begin
423 return VALIDATE_USER_EMAILS
496 return VALIDATE_USER_EMAILS
424 rescue
497 rescue
425 return false
498 return false
426 end
499 end
427 end
500 end
428 end
501 end
You need to be logged in to leave comments. Login now