Description:
remove authenticate by pop3 and cucas
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r485:b530a9de9711 - - 1 file changed: 0 inserted, 47 deleted

@@ -1,319 +1,272
1 require 'digest/sha1'
1 require 'digest/sha1'
2 require 'net/pop'
2 require 'net/pop'
3 require 'net/https'
3 require 'net/https'
4 require 'net/http'
4 require 'net/http'
5 require 'json'
5 require 'json'
6
6
7 class User < ActiveRecord::Base
7 class User < ActiveRecord::Base
8
8
9 has_and_belongs_to_many :roles
9 has_and_belongs_to_many :roles
10
10
11 has_many :test_requests, :order => "submitted_at DESC"
11 has_many :test_requests, :order => "submitted_at DESC"
12
12
13 has_many :messages,
13 has_many :messages,
14 :class_name => "Message",
14 :class_name => "Message",
15 :foreign_key => "sender_id",
15 :foreign_key => "sender_id",
16 :order => 'created_at DESC'
16 :order => 'created_at DESC'
17
17
18 has_many :replied_messages,
18 has_many :replied_messages,
19 :class_name => "Message",
19 :class_name => "Message",
20 :foreign_key => "receiver_id",
20 :foreign_key => "receiver_id",
21 :order => 'created_at DESC'
21 :order => 'created_at DESC'
22
22
23 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
23 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
24
24
25 belongs_to :site
25 belongs_to :site
26 belongs_to :country
26 belongs_to :country
27
27
28 has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
28 has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
29
29
30 scope :activated_users, :conditions => {:activated => true}
30 scope :activated_users, :conditions => {:activated => true}
31
31
32 validates_presence_of :login
32 validates_presence_of :login
33 validates_uniqueness_of :login
33 validates_uniqueness_of :login
34 validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/
34 validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/
35 validates_length_of :login, :within => 3..30
35 validates_length_of :login, :within => 3..30
36
36
37 validates_presence_of :full_name
37 validates_presence_of :full_name
38 validates_length_of :full_name, :minimum => 1
38 validates_length_of :full_name, :minimum => 1
39
39
40 validates_presence_of :password, :if => :password_required?
40 validates_presence_of :password, :if => :password_required?
41 validates_length_of :password, :within => 4..20, :if => :password_required?
41 validates_length_of :password, :within => 4..20, :if => :password_required?
42 validates_confirmation_of :password, :if => :password_required?
42 validates_confirmation_of :password, :if => :password_required?
43
43
44 validates_format_of :email,
44 validates_format_of :email,
45 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
45 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
46 :if => :email_validation?
46 :if => :email_validation?
47 validate :uniqueness_of_email_from_activated_users,
47 validate :uniqueness_of_email_from_activated_users,
48 :if => :email_validation?
48 :if => :email_validation?
49 validate :enough_time_interval_between_same_email_registrations,
49 validate :enough_time_interval_between_same_email_registrations,
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)
71 - user.password = password
72 - user.save
73 - return user
74 - end
75 end
70 end
76 end
71 end
77
72
78 def authenticated?(password)
73 def authenticated?(password)
79 if self.activated
74 if self.activated
80 hashed_password == User.encrypt(password,self.salt)
75 hashed_password == User.encrypt(password,self.salt)
81 else
76 else
82 false
77 false
83 end
78 end
84 end
79 end
85
80
86 - def authenticated_by_pop3?(password)
87 - Net::POP3.enable_ssl
88 - pop = Net::POP3.new('pops.it.chula.ac.th')
89 - authen = true
90 - begin
91 - pop.start(login, password)
92 - pop.finish
93 - return true
94 - rescue
95 - return false
96 - end
97 - end
98 -
99 - def authenticated_by_cucas?(password)
100 - url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
101 - appid = '41508763e340d5858c00f8c1a0f5a2bb'
102 - appsecret ='d9cbb5863091dbe186fded85722a1e31'
103 - post_args = {
104 - 'appid' => appid,
105 - 'appsecret' => appsecret,
106 - 'username' => login,
107 - 'password' => password
108 - }
109 -
110 - #simple call
111 - begin
112 - http = Net::HTTP.new('www.cas.chula.ac.th', 443)
113 - http.use_ssl = true
114 - result = [ ]
115 - http.start do |http|
116 - req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
117 - param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
118 - resp = http.request(req,param)
119 - result = JSON.parse resp.body
120 - end
121 - return true if result["type"] == "beanStudent"
122 - rescue
123 - return false
124 - end
125 - return false
126 - end
127 -
128 def admin?
81 def admin?
129 self.roles.detect {|r| r.name == 'admin' }
82 self.roles.detect {|r| r.name == 'admin' }
130 end
83 end
131
84
132 def email_for_editing
85 def email_for_editing
133 if self.email==nil
86 if self.email==nil
134 "(unknown)"
87 "(unknown)"
135 elsif self.email==''
88 elsif self.email==''
136 "(blank)"
89 "(blank)"
137 else
90 else
138 self.email
91 self.email
139 end
92 end
140 end
93 end
141
94
142 def email_for_editing=(e)
95 def email_for_editing=(e)
143 self.email=e
96 self.email=e
144 end
97 end
145
98
146 def alias_for_editing
99 def alias_for_editing
147 if self.alias==nil
100 if self.alias==nil
148 "(unknown)"
101 "(unknown)"
149 elsif self.alias==''
102 elsif self.alias==''
150 "(blank)"
103 "(blank)"
151 else
104 else
152 self.alias
105 self.alias
153 end
106 end
154 end
107 end
155
108
156 def alias_for_editing=(e)
109 def alias_for_editing=(e)
157 self.alias=e
110 self.alias=e
158 end
111 end
159
112
160 def activation_key
113 def activation_key
161 if self.hashed_password==nil
114 if self.hashed_password==nil
162 encrypt_new_password
115 encrypt_new_password
163 end
116 end
164 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
117 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
165 end
118 end
166
119
167 def verify_activation_key(key)
120 def verify_activation_key(key)
168 key == activation_key
121 key == activation_key
169 end
122 end
170
123
171 def self.random_password(length=5)
124 def self.random_password(length=5)
172 chars = 'abcdefghjkmnopqrstuvwxyz'
125 chars = 'abcdefghjkmnopqrstuvwxyz'
173 password = ''
126 password = ''
174 length.times { password << chars[rand(chars.length - 1)] }
127 length.times { password << chars[rand(chars.length - 1)] }
175 password
128 password
176 end
129 end
177
130
178 def self.find_non_admin_with_prefix(prefix='')
131 def self.find_non_admin_with_prefix(prefix='')
179 users = User.find(:all)
132 users = User.find(:all)
180 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
133 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
181 end
134 end
182
135
183 # Contest information
136 # Contest information
184
137
185 def self.find_users_with_no_contest()
138 def self.find_users_with_no_contest()
186 users = User.find(:all)
139 users = User.find(:all)
187 return users.find_all { |u| u.contests.length == 0 }
140 return users.find_all { |u| u.contests.length == 0 }
188 end
141 end
189
142
190
143
191 def contest_time_left
144 def contest_time_left
192 if GraderConfiguration.contest_mode?
145 if GraderConfiguration.contest_mode?
193 return nil if site==nil
146 return nil if site==nil
194 return site.time_left
147 return site.time_left
195 elsif GraderConfiguration.indv_contest_mode?
148 elsif GraderConfiguration.indv_contest_mode?
196 time_limit = GraderConfiguration.contest_time_limit
149 time_limit = GraderConfiguration.contest_time_limit
197 if time_limit == nil
150 if time_limit == nil
198 return nil
151 return nil
199 end
152 end
200 if contest_stat==nil or contest_stat.started_at==nil
153 if contest_stat==nil or contest_stat.started_at==nil
201 return (Time.now.gmtime + time_limit) - Time.now.gmtime
154 return (Time.now.gmtime + time_limit) - Time.now.gmtime
202 else
155 else
203 finish_time = contest_stat.started_at + time_limit
156 finish_time = contest_stat.started_at + time_limit
204 current_time = Time.now.gmtime
157 current_time = Time.now.gmtime
205 if current_time > finish_time
158 if current_time > finish_time
206 return 0
159 return 0
207 else
160 else
208 return finish_time - current_time
161 return finish_time - current_time
209 end
162 end
210 end
163 end
211 else
164 else
212 return nil
165 return nil
213 end
166 end
214 end
167 end
215
168
216 def contest_finished?
169 def contest_finished?
217 if GraderConfiguration.contest_mode?
170 if GraderConfiguration.contest_mode?
218 return false if site==nil
171 return false if site==nil
219 return site.finished?
172 return site.finished?
220 elsif GraderConfiguration.indv_contest_mode?
173 elsif GraderConfiguration.indv_contest_mode?
221 return false if self.contest_stat(true)==nil
174 return false if self.contest_stat(true)==nil
222 return contest_time_left == 0
175 return contest_time_left == 0
223 else
176 else
224 return false
177 return false
225 end
178 end
226 end
179 end
227
180
228 def contest_started?
181 def contest_started?
229 if GraderConfiguration.indv_contest_mode?
182 if GraderConfiguration.indv_contest_mode?
230 stat = self.contest_stat
183 stat = self.contest_stat
231 return ((stat != nil) and (stat.started_at != nil))
184 return ((stat != nil) and (stat.started_at != nil))
232 elsif GraderConfiguration.contest_mode?
185 elsif GraderConfiguration.contest_mode?
233 return true if site==nil
186 return true if site==nil
234 return site.started
187 return site.started
235 else
188 else
236 return true
189 return true
237 end
190 end
238 end
191 end
239
192
240 def update_start_time
193 def update_start_time
241 stat = self.contest_stat
194 stat = self.contest_stat
242 if stat == nil or stat.started_at == nil
195 if stat == nil or stat.started_at == nil
243 stat ||= UserContestStat.new(:user => self)
196 stat ||= UserContestStat.new(:user => self)
244 stat.started_at = Time.now.gmtime
197 stat.started_at = Time.now.gmtime
245 stat.save
198 stat.save
246 end
199 end
247 end
200 end
248
201
249 def problem_in_user_contests?(problem)
202 def problem_in_user_contests?(problem)
250 problem_contests = problem.contests.all
203 problem_contests = problem.contests.all
251
204
252 if problem_contests.length == 0 # this is public contest
205 if problem_contests.length == 0 # this is public contest
253 return true
206 return true
254 end
207 end
255
208
256 contests.each do |contest|
209 contests.each do |contest|
257 if problem_contests.find {|c| c.id == contest.id }
210 if problem_contests.find {|c| c.id == contest.id }
258 return true
211 return true
259 end
212 end
260 end
213 end
261 return false
214 return false
262 end
215 end
263
216
264 def available_problems_group_by_contests
217 def available_problems_group_by_contests
265 contest_problems = []
218 contest_problems = []
266 pin = {}
219 pin = {}
267 contests.enabled.each do |contest|
220 contests.enabled.each do |contest|
268 available_problems = contest.problems.available
221 available_problems = contest.problems.available
269 contest_problems << {
222 contest_problems << {
270 :contest => contest,
223 :contest => contest,
271 :problems => available_problems
224 :problems => available_problems
272 }
225 }
273 available_problems.each {|p| pin[p.id] = true}
226 available_problems.each {|p| pin[p.id] = true}
274 end
227 end
275 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
228 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
276 contest_problems << {
229 contest_problems << {
277 :contest => nil,
230 :contest => nil,
278 :problems => other_avaiable_problems
231 :problems => other_avaiable_problems
279 }
232 }
280 return contest_problems
233 return contest_problems
281 end
234 end
282
235
283 def available_problems
236 def available_problems
284 if not GraderConfiguration.multicontests?
237 if not GraderConfiguration.multicontests?
285 return Problem.find_available_problems
238 return Problem.find_available_problems
286 else
239 else
287 contest_problems = []
240 contest_problems = []
288 pin = {}
241 pin = {}
289 contests.enabled.each do |contest|
242 contests.enabled.each do |contest|
290 contest.problems.available.each do |problem|
243 contest.problems.available.each do |problem|
291 if not pin.has_key? problem.id
244 if not pin.has_key? problem.id
292 contest_problems << problem
245 contest_problems << problem
293 end
246 end
294 pin[problem.id] = true
247 pin[problem.id] = true
295 end
248 end
296 end
249 end
297 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
250 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
298 return contest_problems + other_avaiable_problems
251 return contest_problems + other_avaiable_problems
299 end
252 end
300 end
253 end
301
254
302 def can_view_problem?(problem)
255 def can_view_problem?(problem)
303 if not GraderConfiguration.multicontests?
256 if not GraderConfiguration.multicontests?
304 return problem.available
257 return problem.available
305 else
258 else
306 return problem_in_user_contests? problem
259 return problem_in_user_contests? problem
307 end
260 end
308 end
261 end
309
262
310 protected
263 protected
311 def encrypt_new_password
264 def encrypt_new_password
312 return if password.blank?
265 return if password.blank?
313 self.salt = (10+rand(90)).to_s
266 self.salt = (10+rand(90)).to_s
314 self.hashed_password = User.encrypt(self.password,self.salt)
267 self.hashed_password = User.encrypt(self.password,self.salt)
315 end
268 end
316
269
317 def assign_default_site
270 def assign_default_site
318 # have to catch error when migrating (because self.site is not available).
271 # have to catch error when migrating (because self.site is not available).
319 begin
272 begin
You need to be logged in to leave comments. Login now