Description:
fix wrong merge on user
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r845:daf5f8de8a3f - - 1 file changed: 4 inserted, 0 deleted
@@ -1,282 +1,286 | |||
|
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 | 25 | has_many :logins |
|
26 | 26 | |
|
27 | 27 | has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy |
|
28 | 28 | |
|
29 | 29 | belongs_to :site |
|
30 | 30 | belongs_to :country |
|
31 | 31 | |
|
32 | 32 | has_and_belongs_to_many :contests, -> { order(:name)} |
|
33 | 33 | |
|
34 | 34 | scope :activated_users, -> {where activated: true} |
|
35 | 35 | |
|
36 | 36 | validates_presence_of :login |
|
37 | 37 | validates_uniqueness_of :login |
|
38 | 38 | validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/ |
|
39 | 39 | validates_length_of :login, :within => 3..30 |
|
40 | 40 | |
|
41 | 41 | validates_presence_of :full_name |
|
42 | 42 | validates_length_of :full_name, :minimum => 1 |
|
43 | 43 | |
|
44 | 44 | validates_presence_of :password, :if => :password_required? |
|
45 | 45 | validates_length_of :password, :within => 4..50, :if => :password_required? |
|
46 | 46 | validates_confirmation_of :password, :if => :password_required? |
|
47 | 47 | |
|
48 | 48 | validates_format_of :email, |
|
49 | 49 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
50 | 50 | :if => :email_validation? |
|
51 | 51 | validate :uniqueness_of_email_from_activated_users, |
|
52 | 52 | :if => :email_validation? |
|
53 | 53 | validate :enough_time_interval_between_same_email_registrations, |
|
54 | 54 | :if => :email_validation? |
|
55 | 55 | |
|
56 | 56 | # these are for ytopc |
|
57 | 57 | # disable for now |
|
58 | 58 | #validates_presence_of :province |
|
59 | 59 | |
|
60 | 60 | attr_accessor :password |
|
61 | 61 | |
|
62 | 62 | before_save :encrypt_new_password |
|
63 | 63 | before_save :assign_default_site |
|
64 | 64 | before_save :assign_default_contest |
|
65 | 65 | |
|
66 | 66 | # this is for will_paginate |
|
67 | 67 | cattr_reader :per_page |
|
68 | 68 | @@per_page = 50 |
|
69 | 69 | |
|
70 | 70 | def self.authenticate(login, password) |
|
71 | 71 | user = find_by_login(login) |
|
72 | 72 | if user |
|
73 | 73 | return user if user.authenticated?(password) |
|
74 | 74 | if user.authenticated_by_cucas?(password) |
|
75 | 75 | user.password = password |
|
76 | 76 | user.save |
|
77 | 77 | return user |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | def authenticated?(password) |
|
84 | 84 | if self.activated |
|
85 | 85 | hashed_password == User.encrypt(password,self.salt) |
|
86 | 86 | else |
|
87 | 87 | false |
|
88 | 88 | end |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | + def login_with_name | |
|
92 | + "[#{login}] #{full_name}" | |
|
93 | + end | |
|
94 | + | |
|
91 | 95 | def authenticated_by_cucas?(password) |
|
92 | 96 | url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate') |
|
93 | 97 | appid = '41508763e340d5858c00f8c1a0f5a2bb' |
|
94 | 98 | appsecret ='d9cbb5863091dbe186fded85722a1e31' |
|
95 | 99 | post_args = { |
|
96 | 100 | 'appid' => appid, |
|
97 | 101 | 'appsecret' => appsecret, |
|
98 | 102 | 'username' => login, |
|
99 | 103 | 'password' => password |
|
100 | 104 | } |
|
101 | 105 | |
|
102 | 106 | #simple call |
|
103 | 107 | begin |
|
104 | 108 | http = Net::HTTP.new('www.cas.chula.ac.th', 443) |
|
105 | 109 | http.use_ssl = true |
|
106 | 110 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
|
107 | 111 | result = [ ] |
|
108 | 112 | http.start do |http| |
|
109 | 113 | req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate') |
|
110 | 114 | #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate') |
|
111 | 115 | #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate') |
|
112 | 116 | param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}" |
|
113 | 117 | resp = http.request(req,param) |
|
114 | 118 | result = JSON.parse resp.body |
|
115 | 119 | puts result |
|
116 | 120 | end |
|
117 | 121 | return true if result["type"] == "beanStudent" |
|
118 | 122 | rescue => e |
|
119 | 123 | puts e |
|
120 | 124 | puts e.message |
|
121 | 125 | return false |
|
122 | 126 | end |
|
123 | 127 | return false |
|
124 | 128 | end |
|
125 | 129 | |
|
126 | 130 | def admin? |
|
127 | 131 | has_role?('admin') |
|
128 | 132 | end |
|
129 | 133 | |
|
130 | 134 | def has_role?(role) |
|
131 | 135 | self.roles.where(name: role).count > 0 |
|
132 | 136 | end |
|
133 | 137 | |
|
134 | 138 | def email_for_editing |
|
135 | 139 | if self.email==nil |
|
136 | 140 | "(unknown)" |
|
137 | 141 | elsif self.email=='' |
|
138 | 142 | "(blank)" |
|
139 | 143 | else |
|
140 | 144 | self.email |
|
141 | 145 | end |
|
142 | 146 | end |
|
143 | 147 | |
|
144 | 148 | def email_for_editing=(e) |
|
145 | 149 | self.email=e |
|
146 | 150 | end |
|
147 | 151 | |
|
148 | 152 | def alias_for_editing |
|
149 | 153 | if self.alias==nil |
|
150 | 154 | "(unknown)" |
|
151 | 155 | elsif self.alias=='' |
|
152 | 156 | "(blank)" |
|
153 | 157 | else |
|
154 | 158 | self.alias |
|
155 | 159 | end |
|
156 | 160 | end |
|
157 | 161 | |
|
158 | 162 | def alias_for_editing=(e) |
|
159 | 163 | self.alias=e |
|
160 | 164 | end |
|
161 | 165 | |
|
162 | 166 | def activation_key |
|
163 | 167 | if self.hashed_password==nil |
|
164 | 168 | encrypt_new_password |
|
165 | 169 | end |
|
166 | 170 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
167 | 171 | end |
|
168 | 172 | |
|
169 | 173 | def verify_activation_key(key) |
|
170 | 174 | key == activation_key |
|
171 | 175 | end |
|
172 | 176 | |
|
173 | 177 | def self.random_password(length=5) |
|
174 | 178 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
175 | 179 | password = '' |
|
176 | 180 | length.times { password << chars[rand(chars.length - 1)] } |
|
177 | 181 | password |
|
178 | 182 | end |
|
179 | 183 | |
|
180 | 184 | def self.find_non_admin_with_prefix(prefix='') |
|
181 | 185 | users = User.all |
|
182 | 186 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
183 | 187 | end |
|
184 | 188 | |
|
185 | 189 | # Contest information |
|
186 | 190 | |
|
187 | 191 | def self.find_users_with_no_contest() |
|
188 | 192 | users = User.all |
|
189 | 193 | return users.find_all { |u| u.contests.length == 0 } |
|
190 | 194 | end |
|
191 | 195 | |
|
192 | 196 | |
|
193 | 197 | def contest_time_left |
|
194 | 198 | if GraderConfiguration.contest_mode? |
|
195 | 199 | return nil if site==nil |
|
196 | 200 | return site.time_left |
|
197 | 201 | elsif GraderConfiguration.indv_contest_mode? |
|
198 | 202 | time_limit = GraderConfiguration.contest_time_limit |
|
199 | 203 | if time_limit == nil |
|
200 | 204 | return nil |
|
201 | 205 | end |
|
202 | 206 | if contest_stat==nil or contest_stat.started_at==nil |
|
203 | 207 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
204 | 208 | else |
|
205 | 209 | finish_time = contest_stat.started_at + time_limit |
|
206 | 210 | current_time = Time.now.gmtime |
|
207 | 211 | if current_time > finish_time |
|
208 | 212 | return 0 |
|
209 | 213 | else |
|
210 | 214 | return finish_time - current_time |
|
211 | 215 | end |
|
212 | 216 | end |
|
213 | 217 | else |
|
214 | 218 | return nil |
|
215 | 219 | end |
|
216 | 220 | end |
|
217 | 221 | |
|
218 | 222 | def contest_finished? |
|
219 | 223 | if GraderConfiguration.contest_mode? |
|
220 | 224 | return false if site==nil |
|
221 | 225 | return site.finished? |
|
222 | 226 | elsif GraderConfiguration.indv_contest_mode? |
|
223 | 227 | return false if self.contest_stat==nil |
|
224 | 228 | return contest_time_left == 0 |
|
225 | 229 | else |
|
226 | 230 | return false |
|
227 | 231 | end |
|
228 | 232 | end |
|
229 | 233 | |
|
230 | 234 | def contest_started? |
|
231 | 235 | if GraderConfiguration.indv_contest_mode? |
|
232 | 236 | stat = self.contest_stat |
|
233 | 237 | return ((stat != nil) and (stat.started_at != nil)) |
|
234 | 238 | elsif GraderConfiguration.contest_mode? |
|
235 | 239 | return true if site==nil |
|
236 | 240 | return site.started |
|
237 | 241 | else |
|
238 | 242 | return true |
|
239 | 243 | end |
|
240 | 244 | end |
|
241 | 245 | |
|
242 | 246 | def update_start_time |
|
243 | 247 | stat = self.contest_stat |
|
244 | 248 | if stat.nil? or stat.started_at.nil? |
|
245 | 249 | stat ||= UserContestStat.new(:user => self) |
|
246 | 250 | stat.started_at = Time.now.gmtime |
|
247 | 251 | stat.save |
|
248 | 252 | end |
|
249 | 253 | end |
|
250 | 254 | |
|
251 | 255 | def problem_in_user_contests?(problem) |
|
252 | 256 | problem_contests = problem.contests.all |
|
253 | 257 | |
|
254 | 258 | if problem_contests.length == 0 # this is public contest |
|
255 | 259 | return true |
|
256 | 260 | end |
|
257 | 261 | |
|
258 | 262 | contests.each do |contest| |
|
259 | 263 | if problem_contests.find {|c| c.id == contest.id } |
|
260 | 264 | return true |
|
261 | 265 | end |
|
262 | 266 | end |
|
263 | 267 | return false |
|
264 | 268 | end |
|
265 | 269 | |
|
266 | 270 | def available_problems_group_by_contests |
|
267 | 271 | contest_problems = [] |
|
268 | 272 | pin = {} |
|
269 | 273 | contests.enabled.each do |contest| |
|
270 | 274 | available_problems = contest.problems.available |
|
271 | 275 | contest_problems << { |
|
272 | 276 | :contest => contest, |
|
273 | 277 | :problems => available_problems |
|
274 | 278 | } |
|
275 | 279 | available_problems.each {|p| pin[p.id] = true} |
|
276 | 280 | end |
|
277 | 281 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
278 | 282 | contest_problems << { |
|
279 | 283 | :contest => nil, |
|
280 | 284 | :problems => other_avaiable_problems |
|
281 | 285 | } |
|
282 | 286 | return contest_problems |
You need to be logged in to leave comments.
Login now