Description:
cucas
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r833:913658e11037 - - 1 file changed: 43 inserted, 0 deleted
@@ -1,374 +1,417 | |||
|
1 | 1 | require 'digest/sha1' |
|
2 | 2 | require 'net/pop' |
|
3 | 3 | require 'net/https' |
|
4 | 4 | require 'net/http' |
|
5 | 5 | require 'json' |
|
6 | 6 | |
|
7 | 7 | class User < ActiveRecord::Base |
|
8 | 8 | |
|
9 | 9 | has_and_belongs_to_many :roles |
|
10 | 10 | |
|
11 | 11 | #has_and_belongs_to_many :groups |
|
12 | 12 | has_many :groups_users, class_name: 'GroupUser' |
|
13 | 13 | has_many :groups, :through => :groups_users |
|
14 | 14 | |
|
15 | 15 | has_many :test_requests, -> {order(submitted_at: :desc)} |
|
16 | 16 | |
|
17 | 17 | has_many :messages, -> { order(created_at: :desc) }, |
|
18 | 18 | :class_name => "Message", |
|
19 | 19 | :foreign_key => "sender_id" |
|
20 | 20 | |
|
21 | 21 | has_many :replied_messages, -> { order(created_at: :desc) }, |
|
22 | 22 | :class_name => "Message", |
|
23 | 23 | :foreign_key => "receiver_id" |
|
24 | 24 | |
|
25 | + has_many :logins | |
|
26 | + | |
|
25 | 27 | has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy |
|
26 | 28 | |
|
27 | 29 | belongs_to :site |
|
28 | 30 | belongs_to :country |
|
29 | 31 | |
|
30 | 32 | has_and_belongs_to_many :contests, -> { order(:name)} |
|
31 | 33 | |
|
32 | 34 | scope :activated_users, -> {where activated: true} |
|
33 | 35 | |
|
34 | 36 | validates_presence_of :login |
|
35 | 37 | validates_uniqueness_of :login |
|
36 | 38 | validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/ |
|
37 | 39 | validates_length_of :login, :within => 3..30 |
|
38 | 40 | |
|
39 | 41 | validates_presence_of :full_name |
|
40 | 42 | validates_length_of :full_name, :minimum => 1 |
|
41 | 43 | |
|
42 | 44 | validates_presence_of :password, :if => :password_required? |
|
43 | 45 | validates_length_of :password, :within => 4..50, :if => :password_required? |
|
44 | 46 | validates_confirmation_of :password, :if => :password_required? |
|
45 | 47 | |
|
46 | 48 | validates_format_of :email, |
|
47 | 49 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
48 | 50 | :if => :email_validation? |
|
49 | 51 | validate :uniqueness_of_email_from_activated_users, |
|
50 | 52 | :if => :email_validation? |
|
51 | 53 | validate :enough_time_interval_between_same_email_registrations, |
|
52 | 54 | :if => :email_validation? |
|
53 | 55 | |
|
54 | 56 | # these are for ytopc |
|
55 | 57 | # disable for now |
|
56 | 58 | #validates_presence_of :province |
|
57 | 59 | |
|
58 | 60 | attr_accessor :password |
|
59 | 61 | |
|
60 | 62 | before_save :encrypt_new_password |
|
61 | 63 | before_save :assign_default_site |
|
62 | 64 | before_save :assign_default_contest |
|
63 | 65 | |
|
64 | 66 | # this is for will_paginate |
|
65 | 67 | cattr_reader :per_page |
|
66 | 68 | @@per_page = 50 |
|
67 | 69 | |
|
68 | 70 | def self.authenticate(login, password) |
|
69 | 71 | user = find_by_login(login) |
|
70 | 72 | if user |
|
71 | 73 | return user if user.authenticated?(password) |
|
74 | + if user.authenticated_by_cucas?(password) | |
|
75 | + user.password = password | |
|
76 | + user.save | |
|
77 | + return user | |
|
72 | 78 | end |
|
73 | 79 | end |
|
80 | + end | |
|
81 | + | |
|
74 | 82 | |
|
75 | 83 | def authenticated?(password) |
|
76 | 84 | if self.activated |
|
77 | 85 | hashed_password == User.encrypt(password,self.salt) |
|
78 | 86 | else |
|
79 | 87 | false |
|
80 | 88 | end |
|
81 | 89 | end |
|
82 | 90 | |
|
91 | + def authenticated_by_cucas?(password) | |
|
92 | + url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate') | |
|
93 | + appid = '41508763e340d5858c00f8c1a0f5a2bb' | |
|
94 | + appsecret ='d9cbb5863091dbe186fded85722a1e31' | |
|
95 | + post_args = { | |
|
96 | + 'appid' => appid, | |
|
97 | + 'appsecret' => appsecret, | |
|
98 | + 'username' => login, | |
|
99 | + 'password' => password | |
|
100 | + } | |
|
101 | + | |
|
102 | + #simple call | |
|
103 | + begin | |
|
104 | + http = Net::HTTP.new('www.cas.chula.ac.th', 443) | |
|
105 | + http.use_ssl = true | |
|
106 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
|
107 | + result = [ ] | |
|
108 | + http.start do |http| | |
|
109 | + req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate') | |
|
110 | + #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate') | |
|
111 | + #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate') | |
|
112 | + param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}" | |
|
113 | + resp = http.request(req,param) | |
|
114 | + result = JSON.parse resp.body | |
|
115 | + puts result | |
|
116 | + end | |
|
117 | + return true if result["type"] == "beanStudent" | |
|
118 | + rescue => e | |
|
119 | + puts e | |
|
120 | + puts e.message | |
|
121 | + return false | |
|
122 | + end | |
|
123 | + return false | |
|
124 | + end | |
|
125 | + | |
|
83 | 126 | def admin? |
|
84 | 127 | self.roles.where(name: 'admin').count > 0 |
|
85 | 128 | end |
|
86 | 129 | |
|
87 | 130 | def email_for_editing |
|
88 | 131 | if self.email==nil |
|
89 | 132 | "(unknown)" |
|
90 | 133 | elsif self.email=='' |
|
91 | 134 | "(blank)" |
|
92 | 135 | else |
|
93 | 136 | self.email |
|
94 | 137 | end |
|
95 | 138 | end |
|
96 | 139 | |
|
97 | 140 | def email_for_editing=(e) |
|
98 | 141 | self.email=e |
|
99 | 142 | end |
|
100 | 143 | |
|
101 | 144 | def alias_for_editing |
|
102 | 145 | if self.alias==nil |
|
103 | 146 | "(unknown)" |
|
104 | 147 | elsif self.alias=='' |
|
105 | 148 | "(blank)" |
|
106 | 149 | else |
|
107 | 150 | self.alias |
|
108 | 151 | end |
|
109 | 152 | end |
|
110 | 153 | |
|
111 | 154 | def alias_for_editing=(e) |
|
112 | 155 | self.alias=e |
|
113 | 156 | end |
|
114 | 157 | |
|
115 | 158 | def activation_key |
|
116 | 159 | if self.hashed_password==nil |
|
117 | 160 | encrypt_new_password |
|
118 | 161 | end |
|
119 | 162 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
120 | 163 | end |
|
121 | 164 | |
|
122 | 165 | def verify_activation_key(key) |
|
123 | 166 | key == activation_key |
|
124 | 167 | end |
|
125 | 168 | |
|
126 | 169 | def self.random_password(length=5) |
|
127 | 170 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
128 | 171 | password = '' |
|
129 | 172 | length.times { password << chars[rand(chars.length - 1)] } |
|
130 | 173 | password |
|
131 | 174 | end |
|
132 | 175 | |
|
133 | 176 | def self.find_non_admin_with_prefix(prefix='') |
|
134 | 177 | users = User.all |
|
135 | 178 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
136 | 179 | end |
|
137 | 180 | |
|
138 | 181 | # Contest information |
|
139 | 182 | |
|
140 | 183 | def self.find_users_with_no_contest() |
|
141 | 184 | users = User.all |
|
142 | 185 | return users.find_all { |u| u.contests.length == 0 } |
|
143 | 186 | end |
|
144 | 187 | |
|
145 | 188 | |
|
146 | 189 | def contest_time_left |
|
147 | 190 | if GraderConfiguration.contest_mode? |
|
148 | 191 | return nil if site==nil |
|
149 | 192 | return site.time_left |
|
150 | 193 | elsif GraderConfiguration.indv_contest_mode? |
|
151 | 194 | time_limit = GraderConfiguration.contest_time_limit |
|
152 | 195 | if time_limit == nil |
|
153 | 196 | return nil |
|
154 | 197 | end |
|
155 | 198 | if contest_stat==nil or contest_stat.started_at==nil |
|
156 | 199 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
157 | 200 | else |
|
158 | 201 | finish_time = contest_stat.started_at + time_limit |
|
159 | 202 | current_time = Time.now.gmtime |
|
160 | 203 | if current_time > finish_time |
|
161 | 204 | return 0 |
|
162 | 205 | else |
|
163 | 206 | return finish_time - current_time |
|
164 | 207 | end |
|
165 | 208 | end |
|
166 | 209 | else |
|
167 | 210 | return nil |
|
168 | 211 | end |
|
169 | 212 | end |
|
170 | 213 | |
|
171 | 214 | def contest_finished? |
|
172 | 215 | if GraderConfiguration.contest_mode? |
|
173 | 216 | return false if site==nil |
|
174 | 217 | return site.finished? |
|
175 | 218 | elsif GraderConfiguration.indv_contest_mode? |
|
176 | 219 | return false if self.contest_stat==nil |
|
177 | 220 | return contest_time_left == 0 |
|
178 | 221 | else |
|
179 | 222 | return false |
|
180 | 223 | end |
|
181 | 224 | end |
|
182 | 225 | |
|
183 | 226 | def contest_started? |
|
184 | 227 | if GraderConfiguration.indv_contest_mode? |
|
185 | 228 | stat = self.contest_stat |
|
186 | 229 | return ((stat != nil) and (stat.started_at != nil)) |
|
187 | 230 | elsif GraderConfiguration.contest_mode? |
|
188 | 231 | return true if site==nil |
|
189 | 232 | return site.started |
|
190 | 233 | else |
|
191 | 234 | return true |
|
192 | 235 | end |
|
193 | 236 | end |
|
194 | 237 | |
|
195 | 238 | def update_start_time |
|
196 | 239 | stat = self.contest_stat |
|
197 | 240 | if stat.nil? or stat.started_at.nil? |
|
198 | 241 | stat ||= UserContestStat.new(:user => self) |
|
199 | 242 | stat.started_at = Time.now.gmtime |
|
200 | 243 | stat.save |
|
201 | 244 | end |
|
202 | 245 | end |
|
203 | 246 | |
|
204 | 247 | def problem_in_user_contests?(problem) |
|
205 | 248 | problem_contests = problem.contests.all |
|
206 | 249 | |
|
207 | 250 | if problem_contests.length == 0 # this is public contest |
|
208 | 251 | return true |
|
209 | 252 | end |
|
210 | 253 | |
|
211 | 254 | contests.each do |contest| |
|
212 | 255 | if problem_contests.find {|c| c.id == contest.id } |
|
213 | 256 | return true |
|
214 | 257 | end |
|
215 | 258 | end |
|
216 | 259 | return false |
|
217 | 260 | end |
|
218 | 261 | |
|
219 | 262 | def available_problems_group_by_contests |
|
220 | 263 | contest_problems = [] |
|
221 | 264 | pin = {} |
|
222 | 265 | contests.enabled.each do |contest| |
|
223 | 266 | available_problems = contest.problems.available |
|
224 | 267 | contest_problems << { |
|
225 | 268 | :contest => contest, |
|
226 | 269 | :problems => available_problems |
|
227 | 270 | } |
|
228 | 271 | available_problems.each {|p| pin[p.id] = true} |
|
229 | 272 | end |
|
230 | 273 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
231 | 274 | contest_problems << { |
|
232 | 275 | :contest => nil, |
|
233 | 276 | :problems => other_avaiable_problems |
|
234 | 277 | } |
|
235 | 278 | return contest_problems |
|
236 | 279 | end |
|
237 | 280 | |
|
238 | 281 | def solve_all_available_problems? |
|
239 | 282 | available_problems.each do |p| |
|
240 | 283 | u = self |
|
241 | 284 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
242 | 285 | return false if !p or !sub or sub.points < p.full_score |
|
243 | 286 | end |
|
244 | 287 | return true |
|
245 | 288 | end |
|
246 | 289 | |
|
247 | 290 | #get a list of available problem |
|
248 | 291 | def available_problems |
|
249 | 292 | # first, we check if this is normal mode |
|
250 | 293 | if not GraderConfiguration.multicontests? |
|
251 | 294 | |
|
252 | 295 | #if this is a normal mode |
|
253 | 296 | #we show problem based on problem_group, if the config said so |
|
254 | 297 | if GraderConfiguration.use_problem_group? |
|
255 | 298 | return available_problems_in_group |
|
256 | 299 | else |
|
257 | 300 | return Problem.available_problems |
|
258 | 301 | end |
|
259 | 302 | else |
|
260 | 303 | #this is multi contest mode |
|
261 | 304 | contest_problems = [] |
|
262 | 305 | pin = {} |
|
263 | 306 | contests.enabled.each do |contest| |
|
264 | 307 | contest.problems.available.each do |problem| |
|
265 | 308 | if not pin.has_key? problem.id |
|
266 | 309 | contest_problems << problem |
|
267 | 310 | end |
|
268 | 311 | pin[problem.id] = true |
|
269 | 312 | end |
|
270 | 313 | end |
|
271 | 314 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
272 | 315 | return contest_problems + other_avaiable_problems |
|
273 | 316 | end |
|
274 | 317 | end |
|
275 | 318 | |
|
276 | 319 | def available_problems_in_group |
|
277 | 320 | problem = [] |
|
278 | 321 | self.groups.each do |group| |
|
279 | 322 | group.problems.where(available: true).each { |p| problem << p } |
|
280 | 323 | end |
|
281 | 324 | problem.uniq! |
|
282 | 325 | if problem |
|
283 | 326 | problem.sort! do |a,b| |
|
284 | 327 | case |
|
285 | 328 | when a.date_added < b.date_added |
|
286 | 329 | 1 |
|
287 | 330 | when a.date_added > b.date_added |
|
288 | 331 | -1 |
|
289 | 332 | else |
|
290 | 333 | a.name <=> b.name |
|
291 | 334 | end |
|
292 | 335 | end |
|
293 | 336 | return problem |
|
294 | 337 | else |
|
295 | 338 | return [] |
|
296 | 339 | end |
|
297 | 340 | end |
|
298 | 341 | |
|
299 | 342 | def can_view_problem?(problem) |
|
300 | 343 | return true if admin? |
|
301 | 344 | return available_problems.include? problem |
|
302 | 345 | end |
|
303 | 346 | |
|
304 | 347 | def self.clear_last_login |
|
305 | 348 | User.update_all(:last_ip => nil) |
|
306 | 349 | end |
|
307 | 350 | |
|
308 | 351 | protected |
|
309 | 352 | def encrypt_new_password |
|
310 | 353 | return if password.blank? |
|
311 | 354 | self.salt = (10+rand(90)).to_s |
|
312 | 355 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
313 | 356 | end |
|
314 | 357 | |
|
315 | 358 | def assign_default_site |
|
316 | 359 | # have to catch error when migrating (because self.site is not available). |
|
317 | 360 | begin |
|
318 | 361 | if self.site==nil |
|
319 | 362 | self.site = Site.find_by_name('default') |
|
320 | 363 | if self.site==nil |
|
321 | 364 | self.site = Site.find(1) # when 'default has be renamed' |
|
322 | 365 | end |
|
323 | 366 | end |
|
324 | 367 | rescue |
|
325 | 368 | end |
|
326 | 369 | end |
|
327 | 370 | |
|
328 | 371 | def assign_default_contest |
|
329 | 372 | # have to catch error when migrating (because self.site is not available). |
|
330 | 373 | begin |
|
331 | 374 | if self.contests.length == 0 |
|
332 | 375 | default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name']) |
|
333 | 376 | if default_contest |
|
334 | 377 | self.contests = [default_contest] |
|
335 | 378 | end |
|
336 | 379 | end |
|
337 | 380 | rescue |
|
338 | 381 | end |
|
339 | 382 | end |
|
340 | 383 | |
|
341 | 384 | def password_required? |
|
342 | 385 | self.hashed_password.blank? || !self.password.blank? |
|
343 | 386 | end |
|
344 | 387 | |
|
345 | 388 | def self.encrypt(string,salt) |
|
346 | 389 | Digest::SHA1.hexdigest(salt + string) |
|
347 | 390 | end |
|
348 | 391 | |
|
349 | 392 | def uniqueness_of_email_from_activated_users |
|
350 | 393 | user = User.activated_users.find_by_email(self.email) |
|
351 | 394 | if user and (user.login != self.login) |
|
352 | 395 | self.errors.add(:base,"Email has already been taken") |
|
353 | 396 | end |
|
354 | 397 | end |
|
355 | 398 | |
|
356 | 399 | def enough_time_interval_between_same_email_registrations |
|
357 | 400 | return if !self.new_record? |
|
358 | 401 | return if self.activated |
|
359 | 402 | open_user = User.find_by_email(self.email, |
|
360 | 403 | :order => 'created_at DESC') |
|
361 | 404 | if open_user and open_user.created_at and |
|
362 | 405 | (open_user.created_at > Time.now.gmtime - 5.minutes) |
|
363 | 406 | self.errors.add(:base,"There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") |
|
364 | 407 | end |
|
365 | 408 | end |
|
366 | 409 | |
|
367 | 410 | def email_validation? |
|
368 | 411 | begin |
|
369 | 412 | return VALIDATE_USER_EMAILS |
|
370 | 413 | rescue |
|
371 | 414 | return false |
|
372 | 415 | end |
|
373 | 416 | end |
|
374 | 417 | end |
You need to be logged in to leave comments.
Login now