Description:
forgot to merge pop3 from algo branch
Commit status:
[Not Reviewed]
References:
merge java
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r403:845ea78912ad - - 1 file changed: 18 inserted, 0 deleted

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