Description:
add options for bulk manage add user to group (not finished)
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r671:52ce712479a1 - - 2 files changed: 17 inserted, 0 deleted

@@ -135,192 +135,193
135 135 redirect_to :action => 'show', :id => @user
136 136 else
137 137 render :action => 'edit'
138 138 end
139 139 end
140 140
141 141 def destroy
142 142 User.find(params[:id]).destroy
143 143 redirect_to :action => 'index'
144 144 end
145 145
146 146 def user_stat
147 147 if params[:commit] == 'download csv'
148 148 @problems = Problem.all
149 149 else
150 150 @problems = Problem.available_problems
151 151 end
152 152 @users = User.includes(:contests, :contest_stat).where(enabled: true)
153 153 @scorearray = Array.new
154 154 @users.each do |u|
155 155 ustat = Array.new
156 156 ustat[0] = u
157 157 @problems.each do |p|
158 158 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
159 159 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
160 160 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
161 161 else
162 162 ustat << [0,false]
163 163 end
164 164 end
165 165 @scorearray << ustat
166 166 end
167 167 if params[:commit] == 'download csv' then
168 168 csv = gen_csv_from_scorearray(@scorearray,@problems)
169 169 send_data csv, filename: 'last_score.csv'
170 170 else
171 171 render template: 'user_admin/user_stat'
172 172 end
173 173 end
174 174
175 175 def user_stat_max
176 176 if params[:commit] == 'download csv'
177 177 @problems = Problem.all
178 178 else
179 179 @problems = Problem.available_problems
180 180 end
181 181 @users = User.includes(:contests).includes(:contest_stat).all
182 182 @scorearray = Array.new
183 183 #set up range from param
184 184 since_id = params.fetch(:since_id, 0).to_i
185 185 until_id = params.fetch(:until_id, 0).to_i
186 186 @users.each do |u|
187 187 ustat = Array.new
188 188 ustat[0] = u
189 189 @problems.each do |p|
190 190 max_points = 0
191 191 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
192 192 max_points = sub.points if sub and sub.points and (sub.points > max_points)
193 193 end
194 194 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
195 195 end
196 196 @scorearray << ustat
197 197 end
198 198
199 199 if params[:commit] == 'download csv' then
200 200 csv = gen_csv_from_scorearray(@scorearray,@problems)
201 201 send_data csv, filename: 'max_score.csv'
202 202 else
203 203 render template: 'user_admin/user_stat'
204 204 end
205 205 end
206 206
207 207 def import
208 208 if params[:file]==''
209 209 flash[:notice] = 'Error importing no file'
210 210 redirect_to :action => 'index' and return
211 211 end
212 212 import_from_file(params[:file])
213 213 end
214 214
215 215 def random_all_passwords
216 216 users = User.all
217 217 @prefix = params[:prefix] || ''
218 218 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
219 219 @changed = false
220 220 if request.request_method == 'POST'
221 221 @non_admin_users.each do |user|
222 222 password = random_password
223 223 user.password = password
224 224 user.password_confirmation = password
225 225 user.save
226 226 end
227 227 @changed = true
228 228 end
229 229 end
230 230
231 +
231 232 # contest management
232 233
233 234 def contests
234 235 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
235 236 @contests = Contest.enabled
236 237 end
237 238
238 239 def assign_from_list
239 240 contest_id = params[:users_contest_id]
240 241 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
241 242 contest = Contest.find(params[:new_contest][:id])
242 243 if !contest
243 244 flash[:notice] = 'Error: no contest'
244 245 redirect_to :action => 'contests', :id =>contest_id
245 246 end
246 247
247 248 note = []
248 249 users.each do |u|
249 250 u.contests = [contest]
250 251 note << u.login
251 252 end
252 253 flash[:notice] = 'User(s) ' + note.join(', ') +
253 254 " were successfully reassigned to #{contest.title}."
254 255 redirect_to :action => 'contests', :id =>contest.id
255 256 end
256 257
257 258 def add_to_contest
258 259 user = User.find(params[:id])
259 260 contest = Contest.find(params[:contest_id])
260 261 if user and contest
261 262 user.contests << contest
262 263 end
263 264 redirect_to :action => 'index'
264 265 end
265 266
266 267 def remove_from_contest
267 268 user = User.find(params[:id])
268 269 contest = Contest.find(params[:contest_id])
269 270 if user and contest
270 271 user.contests.delete(contest)
271 272 end
272 273 redirect_to :action => 'index'
273 274 end
274 275
275 276 def contest_management
276 277 end
277 278
278 279 def manage_contest
279 280 contest = Contest.find(params[:contest][:id])
280 281 if !contest
281 282 flash[:notice] = 'You did not choose the contest.'
282 283 redirect_to :action => 'contest_management' and return
283 284 end
284 285
285 286 operation = params[:operation]
286 287
287 288 if not ['add','remove','assign'].include? operation
288 289 flash[:notice] = 'You did not choose the operation to perform.'
289 290 redirect_to :action => 'contest_management' and return
290 291 end
291 292
292 293 lines = params[:login_list]
293 294 if !lines or lines.blank?
294 295 flash[:notice] = 'You entered an empty list.'
295 296 redirect_to :action => 'contest_management' and return
296 297 end
297 298
298 299 note = []
299 300 users = []
300 301 lines.split("\n").each do |line|
301 302 user = User.find_by_login(line.chomp)
302 303 if user
303 304 if operation=='add'
304 305 if ! user.contests.include? contest
305 306 user.contests << contest
306 307 end
307 308 elsif operation=='remove'
308 309 user.contests.delete(contest)
309 310 else
310 311 user.contests = [contest]
311 312 end
312 313
313 314 if params[:reset_timer]
314 315 user.contest_stat.forced_logout = true
315 316 user.contest_stat.reset_timer_and_save
316 317 end
317 318
318 319 if params[:notification_emails]
319 320 send_contest_update_notification_email(user, contest)
320 321 end
321 322
322 323 note << user.login
323 324 users << user
324 325 end
325 326 end
326 327
@@ -330,206 +331,213
330 331
331 332 flash[:notice] = 'User(s) ' + note.join(', ') +
332 333 ' were successfully modified. '
333 334 redirect_to :action => 'contest_management'
334 335 end
335 336
336 337 # admin management
337 338
338 339 def admin
339 340 @admins = User.all.find_all {|user| user.admin? }
340 341 end
341 342
342 343 def grant_admin
343 344 login = params[:login]
344 345 user = User.find_by_login(login)
345 346 if user!=nil
346 347 admin_role = Role.find_by_name('admin')
347 348 user.roles << admin_role
348 349 else
349 350 flash[:notice] = 'Unknown user'
350 351 end
351 352 flash[:notice] = 'User added as admins'
352 353 redirect_to :action => 'admin'
353 354 end
354 355
355 356 def revoke_admin
356 357 user = User.find(params[:id])
357 358 if user==nil
358 359 flash[:notice] = 'Unknown user'
359 360 redirect_to :action => 'admin' and return
360 361 elsif user.login == 'root'
361 362 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
362 363 redirect_to :action => 'admin' and return
363 364 end
364 365
365 366 admin_role = Role.find_by_name('admin')
366 367 user.roles.delete(admin_role)
367 368 flash[:notice] = 'User permission revoked'
368 369 redirect_to :action => 'admin'
369 370 end
370 371
371 372 # mass mailing
372 373
373 374 def mass_mailing
374 375 end
375 376
376 377 def bulk_mail
377 378 lines = params[:login_list]
378 379 if !lines or lines.blank?
379 380 flash[:notice] = 'You entered an empty list.'
380 381 redirect_to :action => 'mass_mailing' and return
381 382 end
382 383
383 384 mail_subject = params[:subject]
384 385 if !mail_subject or mail_subject.blank?
385 386 flash[:notice] = 'You entered an empty mail subject.'
386 387 redirect_to :action => 'mass_mailing' and return
387 388 end
388 389
389 390 mail_body = params[:email_body]
390 391 if !mail_body or mail_body.blank?
391 392 flash[:notice] = 'You entered an empty mail body.'
392 393 redirect_to :action => 'mass_mailing' and return
393 394 end
394 395
395 396 note = []
396 397 users = []
397 398 lines.split("\n").each do |line|
398 399 user = User.find_by_login(line.chomp)
399 400 if user
400 401 send_mail(user.email, mail_subject, mail_body)
401 402 note << user.login
402 403 end
403 404 end
404 405
405 406 flash[:notice] = 'User(s) ' + note.join(', ') +
406 407 ' were successfully modified. '
407 408 redirect_to :action => 'mass_mailing'
408 409 end
409 410
410 411 #bulk manage
411 412 def bulk_manage
412 413
413 414 begin
414 415 @users = User.where('login REGEXP ?',params[:regex]) if params[:regex]
415 416 @users.count if @users #i don't know why I have to call count, but if I won't exception is not raised
416 417 rescue Exception
417 418 flash[:error] = 'Regular Expression is malformed'
418 419 @users = nil
419 420 end
420 421
421 422 if params[:commit]
422 423 @action = {}
423 424 @action[:set_enable] = params[:enabled]
424 425 @action[:enabled] = params[:enable] == "1"
425 426 @action[:gen_password] = params[:gen_password]
427 + @action[:add_group] = params[:add_group]
428 + @action[:group_name] = params[:group_name]
426 429 end
427 430
428 431 if params[:commit] == "Perform"
429 432 if @action[:set_enable]
430 433 @users.update_all(enabled: @action[:enabled])
431 434 end
432 435 if @action[:gen_password]
433 436 @users.each do |u|
434 437 password = random_password
435 438 u.password = password
436 439 u.password_confirmation = password
437 440 u.save
438 441 end
439 442 end
443 + if @action[:add_group]
444 + @uses.each do |u|
445 +
446 + end
447 + end
440 448 end
441 449 end
442 450
443 451 protected
444 452
445 453 def random_password(length=5)
446 454 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
447 455 newpass = ""
448 456 length.times { newpass << chars[rand(chars.size-1)] }
449 457 return newpass
450 458 end
451 459
452 460 def import_from_file(f)
453 461 data_hash = YAML.load(f)
454 462 @import_log = ""
455 463
456 464 country_data = data_hash[:countries]
457 465 site_data = data_hash[:sites]
458 466 user_data = data_hash[:users]
459 467
460 468 # import country
461 469 countries = {}
462 470 country_data.each_pair do |id,country|
463 471 c = Country.find_by_name(country[:name])
464 472 if c!=nil
465 473 countries[id] = c
466 474 @import_log << "Found #{country[:name]}\n"
467 475 else
468 476 countries[id] = Country.new(:name => country[:name])
469 477 countries[id].save
470 478 @import_log << "Created #{country[:name]}\n"
471 479 end
472 480 end
473 481
474 482 # import sites
475 483 sites = {}
476 484 site_data.each_pair do |id,site|
477 485 s = Site.find_by_name(site[:name])
478 486 if s!=nil
479 487 @import_log << "Found #{site[:name]}\n"
480 488 else
481 489 s = Site.new(:name => site[:name])
482 490 @import_log << "Created #{site[:name]}\n"
483 491 end
484 492 s.password = site[:password]
485 493 s.country = countries[site[:country_id]]
486 494 s.save
487 495 sites[id] = s
488 496 end
489 497
490 498 # import users
491 499 user_data.each_pair do |id,user|
492 500 u = User.find_by_login(user[:login])
493 501 if u!=nil
494 502 @import_log << "Found #{user[:login]}\n"
495 503 else
496 504 u = User.new(:login => user[:login])
497 505 @import_log << "Created #{user[:login]}\n"
498 506 end
499 507 u.full_name = user[:name]
500 508 u.password = user[:password]
501 509 u.country = countries[user[:country_id]]
502 510 u.site = sites[user[:site_id]]
503 511 u.activated = true
504 512 u.email = "empty-#{u.login}@none.com"
505 513 if not u.save
506 514 @import_log << "Errors\n"
507 515 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
508 516 end
509 517 end
510 518
511 519 end
512 520
513 521 def logout_users(users)
514 522 users.each do |user|
515 523 contest_stat = user.contest_stat(true)
516 524 if contest_stat and !contest_stat.forced_logout
517 525 contest_stat.forced_logout = true
518 526 contest_stat.save
519 527 end
520 528 end
521 529 end
522 530
523 531 def send_contest_update_notification_email(user, contest)
524 532 contest_title_name = GraderConfiguration['contest.name']
525 533 contest_name = contest.name
526 534 mail_subject = t('contest.notification.email_subject', {
527 535 :contest_title_name => contest_title_name,
528 536 :contest_name => contest_name })
529 537 mail_body = t('contest.notification.email_body', {
530 538 :full_name => user.full_name,
531 539 :contest_title_name => contest_title_name,
532 540 :contest_name => contest.name,
533 541 })
534 542
535 543 logger.info mail_body
@@ -1,77 +1,86
1 1 %h1 Bulk Manage User
2 2
3 3 = form_tag bulk_manage_user_admin_path
4 4 .row
5 5 .col-md-6
6 6 .panel.panel-primary
7 7 .panel-title.panel-heading
8 8 Filter User
9 9 .panel-body
10 10 Filtering users whose login match the following MySQL regex
11 11 .form-group
12 12 = label_tag "regex", 'Regex Pattern'
13 13 = text_field_tag "regex", params[:regex], class: 'form-control'
14 14 %p
15 15 Example
16 16 %ul
17 17 %li
18 18 %code root
19 19 matches every user whose login contains "root"
20 20 %li
21 21 %code ^56
22 22 matches every user whose login starts with "56"
23 23 %li
24 24 %code 21$
25 25 matches every user whose login ends with "21"
26 26 .col-md-6
27 27 .panel.panel-primary
28 28 .panel-title.panel-heading
29 29 Action
30 30 .panel-body
31 31 .row.form-group
32 32 .col-md-6
33 33 %label.checkbox-inline
34 34 = check_box_tag "enabled", true, params[:enabled]
35 35 Change "Enabled" to
36 36 .col-md-3
37 37 %label.radio-inline
38 38 = radio_button_tag "enable", 1, params[:enable] == '1', id: 'enable-yes'
39 39 Yes
40 40 .col-md-3
41 41 %label.radio-inline
42 42 = radio_button_tag "enable", 0, params[:enable] == '0', id: 'enable-no'
43 43 No
44 44 .row.form-group
45 45 .col-md-6
46 46 %label.checkbox-inline
47 47 = check_box_tag "gen_password", true, params[:gen_password]
48 48 Generate new random password
49 + .row.form-group
50 + .col-md-4
51 + %label.checkbox-inline
52 + = check_box_tag "add_group", true, params[:add_group]
53 + Add users to group
54 + %label.col-md-3.control-label.text-right Group name
55 + .col-md-5
56 + = text_field_tag "group_name", params[:group_name], id: 'group_name',class: 'form-control select2'
57 +
49 58
50 59 .row
51 60 .col-md-12
52 61 = submit_tag "Preview Result", class: 'btn btn-default'
53 62 - if @users
54 63 .row
55 64 .col-md-4
56 65 - if @action
57 66 %h2 Confirmation
58 67 - if @action[:set_enable]
59 68 .alert.alert-info The following users will be set #{(@action[:enabled] ? 'enable' : 'disable')}.
60 69 - if @action[:gen_password]
61 70 .alert.alert-info The password of the following users will be randomly generated.
62 71 .row
63 72 .col-md-4
64 73 = submit_tag "Perform", class: 'btn btn-primary'
65 74 .row
66 75 .col-md-12
67 76 The pattern matches #{@users.count} following users.
68 77 %br
69 78 - @users.each do |user|
70 79 = user.login
71 80 = ' '
72 81 = user.full_name
73 82 = ' '
74 83 = "(#{user.remark})" if user.remark
75 84 %br
76 85
77 86
You need to be logged in to leave comments. Login now