Description:
report only enabled user
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r550:6d57d5eff450 - - 1 file changed: 1 inserted, 1 deleted

@@ -1,529 +1,529
1 1 require 'csv'
2 2
3 3 class UserAdminController < ApplicationController
4 4
5 5 include MailHelperMethods
6 6
7 7 before_filter :admin_authorization
8 8
9 9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 10 verify :method => :post, :only => [ :destroy,
11 11 :create, :create_from_list,
12 12 :update,
13 13 :manage_contest,
14 14 :bulk_mail
15 15 ],
16 16 :redirect_to => { :action => :list }
17 17
18 18 def index
19 19 list
20 20 render :action => 'list'
21 21 end
22 22
23 23 def list
24 24 @user_count = User.count
25 25 if params[:page] == 'all'
26 26 @users = User.all
27 27 @paginated = false
28 28 else
29 29 @users = User.paginate :page => params[:page]
30 30 @paginated = true
31 31 end
32 32 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
33 33 @contests = Contest.enabled
34 34 end
35 35
36 36 def active
37 37 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
38 38 @users = []
39 39 sessions.each do |session|
40 40 if session.data[:user_id]
41 41 @users << User.find(session.data[:user_id])
42 42 end
43 43 end
44 44 end
45 45
46 46 def show
47 47 @user = User.find(params[:id])
48 48 end
49 49
50 50 def new
51 51 @user = User.new
52 52 end
53 53
54 54 def create
55 55 @user = User.new(params[:user])
56 56 @user.activated = true
57 57 if @user.save
58 58 flash[:notice] = 'User was successfully created.'
59 59 redirect_to :action => 'list'
60 60 else
61 61 render :action => 'new'
62 62 end
63 63 end
64 64
65 65 def clear_last_ip
66 66 @user = User.find(params[:id])
67 67 @user.last_ip = nil
68 68 @user.save
69 69 redirect_to action: 'list', page: params[:page]
70 70 end
71 71
72 72 def create_from_list
73 73 lines = params[:user_list]
74 74
75 75 note = []
76 76
77 77 lines.split("\n").each do |line|
78 78 items = line.chomp.split(',')
79 79 if items.length>=2
80 80 login = items[0]
81 81 full_name = items[1]
82 82
83 83 added_random_password = false
84 84 if items.length>=3
85 85 password = items[2].chomp(" ")
86 86 user_alias = (items.length>=4) ? items[3] : login
87 87 else
88 88 password = random_password
89 89 user_alias = (items.length>=4) ? items[3] : login
90 90 added_random_password = true
91 91 end
92 92
93 93 user = User.find_by_login(login)
94 94 if (user)
95 95 user.full_name = full_name
96 96 user.password = password
97 97 else
98 98 user = User.new({:login => login,
99 99 :full_name => full_name,
100 100 :password => password,
101 101 :password_confirmation => password,
102 102 :alias => user_alias})
103 103 end
104 104 user.activated = true
105 105 user.save
106 106
107 107 if added_random_password
108 108 note << "'#{login}' (+)"
109 109 else
110 110 note << login
111 111 end
112 112 end
113 113 end
114 114 flash[:notice] = 'User(s) ' + note.join(', ') +
115 115 ' were successfully created. ' +
116 116 '( (+) - created with random passwords.)'
117 117 redirect_to :action => 'list'
118 118 end
119 119
120 120 def edit
121 121 @user = User.find(params[:id])
122 122 end
123 123
124 124 def update
125 125 @user = User.find(params[:id])
126 126 if @user.update_attributes(params[:user])
127 127 flash[:notice] = 'User was successfully updated.'
128 128 redirect_to :action => 'show', :id => @user
129 129 else
130 130 render :action => 'edit'
131 131 end
132 132 end
133 133
134 134 def destroy
135 135 User.find(params[:id]).destroy
136 136 redirect_to :action => 'list'
137 137 end
138 138
139 139 def user_stat
140 140 if params[:commit] == 'download csv'
141 141 @problems = Problem.all
142 142 else
143 143 @problems = Problem.find_available_problems
144 144 end
145 - @users = User.find(:all, :include => [:contests, :contest_stat])
145 + @users = User.includes(:contests, :contest_stat).where(enabled: true) #find(:all, :include => [:contests, :contest_stat]).where(enabled: true)
146 146 @scorearray = Array.new
147 147 @users.each do |u|
148 148 ustat = Array.new
149 149 ustat[0] = u
150 150 @problems.each do |p|
151 151 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
152 152 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
153 153 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
154 154 else
155 155 ustat << [0,false]
156 156 end
157 157 end
158 158 @scorearray << ustat
159 159 end
160 160 if params[:commit] == 'download csv' then
161 161 csv = gen_csv_from_scorearray(@scorearray,@problems)
162 162 send_data csv, filename: 'last_score.csv'
163 163 else
164 164 render template: 'user_admin/user_stat'
165 165 end
166 166 end
167 167
168 168 def user_stat_max
169 169 if params[:commit] == 'download csv'
170 170 @problems = Problem.all
171 171 else
172 172 @problems = Problem.find_available_problems
173 173 end
174 174 @users = User.find(:all, :include => [:contests, :contest_stat])
175 175 @scorearray = Array.new
176 176 #set up range from param
177 177 since_id = params.fetch(:since_id, 0).to_i
178 178 until_id = params.fetch(:until_id, 0).to_i
179 179 @users.each do |u|
180 180 ustat = Array.new
181 181 ustat[0] = u
182 182 @problems.each do |p|
183 183 max_points = 0
184 184 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
185 185 max_points = sub.points if sub and sub.points and (sub.points > max_points)
186 186 end
187 187 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
188 188 end
189 189 @scorearray << ustat
190 190 end
191 191
192 192 if params[:commit] == 'download csv' then
193 193 csv = gen_csv_from_scorearray(@scorearray,@problems)
194 194 send_data csv, filename: 'max_score.csv'
195 195 else
196 196 render template: 'user_admin/user_stat'
197 197 end
198 198 end
199 199
200 200 def import
201 201 if params[:file]==''
202 202 flash[:notice] = 'Error importing no file'
203 203 redirect_to :action => 'list' and return
204 204 end
205 205 import_from_file(params[:file])
206 206 end
207 207
208 208 def random_all_passwords
209 209 users = User.find(:all)
210 210 @prefix = params[:prefix] || ''
211 211 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
212 212 @changed = false
213 213 if request.request_method == 'POST'
214 214 @non_admin_users.each do |user|
215 215 password = random_password
216 216 user.password = password
217 217 user.password_confirmation = password
218 218 user.save
219 219 end
220 220 @changed = true
221 221 end
222 222 end
223 223
224 224 # contest management
225 225
226 226 def contests
227 227 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
228 228 @contests = Contest.enabled
229 229 end
230 230
231 231 def assign_from_list
232 232 contest_id = params[:users_contest_id]
233 233 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
234 234 contest = Contest.find(params[:new_contest][:id])
235 235 if !contest
236 236 flash[:notice] = 'Error: no contest'
237 237 redirect_to :action => 'contests', :id =>contest_id
238 238 end
239 239
240 240 note = []
241 241 users.each do |u|
242 242 u.contests = [contest]
243 243 note << u.login
244 244 end
245 245 flash[:notice] = 'User(s) ' + note.join(', ') +
246 246 " were successfully reassigned to #{contest.title}."
247 247 redirect_to :action => 'contests', :id =>contest.id
248 248 end
249 249
250 250 def add_to_contest
251 251 user = User.find(params[:id])
252 252 contest = Contest.find(params[:contest_id])
253 253 if user and contest
254 254 user.contests << contest
255 255 end
256 256 redirect_to :action => 'list'
257 257 end
258 258
259 259 def remove_from_contest
260 260 user = User.find(params[:id])
261 261 contest = Contest.find(params[:contest_id])
262 262 if user and contest
263 263 user.contests.delete(contest)
264 264 end
265 265 redirect_to :action => 'list'
266 266 end
267 267
268 268 def contest_management
269 269 end
270 270
271 271 def manage_contest
272 272 contest = Contest.find(params[:contest][:id])
273 273 if !contest
274 274 flash[:notice] = 'You did not choose the contest.'
275 275 redirect_to :action => 'contest_management' and return
276 276 end
277 277
278 278 operation = params[:operation]
279 279
280 280 if not ['add','remove','assign'].include? operation
281 281 flash[:notice] = 'You did not choose the operation to perform.'
282 282 redirect_to :action => 'contest_management' and return
283 283 end
284 284
285 285 lines = params[:login_list]
286 286 if !lines or lines.blank?
287 287 flash[:notice] = 'You entered an empty list.'
288 288 redirect_to :action => 'contest_management' and return
289 289 end
290 290
291 291 note = []
292 292 users = []
293 293 lines.split("\n").each do |line|
294 294 user = User.find_by_login(line.chomp)
295 295 if user
296 296 if operation=='add'
297 297 if ! user.contests.include? contest
298 298 user.contests << contest
299 299 end
300 300 elsif operation=='remove'
301 301 user.contests.delete(contest)
302 302 else
303 303 user.contests = [contest]
304 304 end
305 305
306 306 if params[:reset_timer]
307 307 user.contest_stat.forced_logout = true
308 308 user.contest_stat.reset_timer_and_save
309 309 end
310 310
311 311 if params[:notification_emails]
312 312 send_contest_update_notification_email(user, contest)
313 313 end
314 314
315 315 note << user.login
316 316 users << user
317 317 end
318 318 end
319 319
320 320 if params[:reset_timer]
321 321 logout_users(users)
322 322 end
323 323
324 324 flash[:notice] = 'User(s) ' + note.join(', ') +
325 325 ' were successfully modified. '
326 326 redirect_to :action => 'contest_management'
327 327 end
328 328
329 329 # admin management
330 330
331 331 def admin
332 332 @admins = User.find(:all).find_all {|user| user.admin? }
333 333 end
334 334
335 335 def grant_admin
336 336 login = params[:login]
337 337 user = User.find_by_login(login)
338 338 if user!=nil
339 339 admin_role = Role.find_by_name('admin')
340 340 user.roles << admin_role
341 341 else
342 342 flash[:notice] = 'Unknown user'
343 343 end
344 344 flash[:notice] = 'User added as admins'
345 345 redirect_to :action => 'admin'
346 346 end
347 347
348 348 def revoke_admin
349 349 user = User.find(params[:id])
350 350 if user==nil
351 351 flash[:notice] = 'Unknown user'
352 352 redirect_to :action => 'admin' and return
353 353 elsif user.login == 'root'
354 354 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
355 355 redirect_to :action => 'admin' and return
356 356 end
357 357
358 358 admin_role = Role.find_by_name('admin')
359 359 user.roles.delete(admin_role)
360 360 flash[:notice] = 'User permission revoked'
361 361 redirect_to :action => 'admin'
362 362 end
363 363
364 364 # mass mailing
365 365
366 366 def mass_mailing
367 367 end
368 368
369 369 def bulk_mail
370 370 lines = params[:login_list]
371 371 if !lines or lines.blank?
372 372 flash[:notice] = 'You entered an empty list.'
373 373 redirect_to :action => 'mass_mailing' and return
374 374 end
375 375
376 376 mail_subject = params[:subject]
377 377 if !mail_subject or mail_subject.blank?
378 378 flash[:notice] = 'You entered an empty mail subject.'
379 379 redirect_to :action => 'mass_mailing' and return
380 380 end
381 381
382 382 mail_body = params[:email_body]
383 383 if !mail_body or mail_body.blank?
384 384 flash[:notice] = 'You entered an empty mail body.'
385 385 redirect_to :action => 'mass_mailing' and return
386 386 end
387 387
388 388 note = []
389 389 users = []
390 390 lines.split("\n").each do |line|
391 391 user = User.find_by_login(line.chomp)
392 392 if user
393 393 send_mail(user.email, mail_subject, mail_body)
394 394 note << user.login
395 395 end
396 396 end
397 397
398 398 flash[:notice] = 'User(s) ' + note.join(', ') +
399 399 ' were successfully modified. '
400 400 redirect_to :action => 'mass_mailing'
401 401 end
402 402
403 403 protected
404 404
405 405 def random_password(length=5)
406 406 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
407 407 newpass = ""
408 408 length.times { newpass << chars[rand(chars.size-1)] }
409 409 return newpass
410 410 end
411 411
412 412 def import_from_file(f)
413 413 data_hash = YAML.load(f)
414 414 @import_log = ""
415 415
416 416 country_data = data_hash[:countries]
417 417 site_data = data_hash[:sites]
418 418 user_data = data_hash[:users]
419 419
420 420 # import country
421 421 countries = {}
422 422 country_data.each_pair do |id,country|
423 423 c = Country.find_by_name(country[:name])
424 424 if c!=nil
425 425 countries[id] = c
426 426 @import_log << "Found #{country[:name]}\n"
427 427 else
428 428 countries[id] = Country.new(:name => country[:name])
429 429 countries[id].save
430 430 @import_log << "Created #{country[:name]}\n"
431 431 end
432 432 end
433 433
434 434 # import sites
435 435 sites = {}
436 436 site_data.each_pair do |id,site|
437 437 s = Site.find_by_name(site[:name])
438 438 if s!=nil
439 439 @import_log << "Found #{site[:name]}\n"
440 440 else
441 441 s = Site.new(:name => site[:name])
442 442 @import_log << "Created #{site[:name]}\n"
443 443 end
444 444 s.password = site[:password]
445 445 s.country = countries[site[:country_id]]
446 446 s.save
447 447 sites[id] = s
448 448 end
449 449
450 450 # import users
451 451 user_data.each_pair do |id,user|
452 452 u = User.find_by_login(user[:login])
453 453 if u!=nil
454 454 @import_log << "Found #{user[:login]}\n"
455 455 else
456 456 u = User.new(:login => user[:login])
457 457 @import_log << "Created #{user[:login]}\n"
458 458 end
459 459 u.full_name = user[:name]
460 460 u.password = user[:password]
461 461 u.country = countries[user[:country_id]]
462 462 u.site = sites[user[:site_id]]
463 463 u.activated = true
464 464 u.email = "empty-#{u.login}@none.com"
465 465 if not u.save
466 466 @import_log << "Errors\n"
467 467 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
468 468 end
469 469 end
470 470
471 471 end
472 472
473 473 def logout_users(users)
474 474 users.each do |user|
475 475 contest_stat = user.contest_stat(true)
476 476 if contest_stat and !contest_stat.forced_logout
477 477 contest_stat.forced_logout = true
478 478 contest_stat.save
479 479 end
480 480 end
481 481 end
482 482
483 483 def send_contest_update_notification_email(user, contest)
484 484 contest_title_name = GraderConfiguration['contest.name']
485 485 contest_name = contest.name
486 486 mail_subject = t('contest.notification.email_subject', {
487 487 :contest_title_name => contest_title_name,
488 488 :contest_name => contest_name })
489 489 mail_body = t('contest.notification.email_body', {
490 490 :full_name => user.full_name,
491 491 :contest_title_name => contest_title_name,
492 492 :contest_name => contest.name,
493 493 })
494 494
495 495 logger.info mail_body
496 496 send_mail(user.email, mail_subject, mail_body)
497 497 end
498 498
499 499 def find_contest_and_user_from_contest_id(id)
500 500 if id!='none'
501 501 @contest = Contest.find(id)
502 502 else
503 503 @contest = nil
504 504 end
505 505 if @contest
506 506 @users = @contest.users
507 507 else
508 508 @users = User.find_users_with_no_contest
509 509 end
510 510 return [@contest, @users]
511 511 end
512 512
513 513 def gen_csv_from_scorearray(scorearray,problem)
514 514 CSV.generate do |csv|
515 515 #add header
516 516 header = ['User','Name', 'Activated?', 'Logged in', 'Contest']
517 517 problem.each { |p| header << p.name }
518 518 header += ['Total','Passed']
519 519 csv << header
520 520 #add data
521 521 scorearray.each do |sc|
522 522 total = num_passed = 0
523 523 row = Array.new
524 524 sc.each_index do |i|
525 525 if i == 0
526 526 row << sc[i].login
527 527 row << sc[i].full_name
528 528 row << sc[i].activated
529 529 row << (sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no')
You need to be logged in to leave comments. Login now