Description:
update new list of users
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r808:8690f354dd15 - - 5 files changed: 116 inserted, 88 deleted

@@ -0,0 +1,5
1 + class AddIndexToTaskStatus < ActiveRecord::Migration[5.2]
2 + def change
3 + add_index :tasks, :status
4 + end
5 + end
@@ -1,188 +1,119
1 1 require 'csv'
2 2
3 3 class UserAdminController < ApplicationController
4 4
5 5 include MailHelperMethods
6 6
7 7 before_action :admin_authorization
8 8
9 9 def index
10 10 @user_count = User.count
11 11 @users = User.all
12 12 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
13 13 @contests = Contest.enabled
14 14 end
15 15
16 16 def active
17 17 sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago)
18 18 @users = []
19 19 sessions.each do |session|
20 20 if session.data[:user_id]
21 21 @users << User.find(session.data[:user_id])
22 22 end
23 23 end
24 24 end
25 25
26 26 def show
27 27 @user = User.find(params[:id])
28 28 end
29 29
30 30 def new
31 31 @user = User.new
32 32 end
33 33
34 34 def create
35 35 @user = User.new(user_params)
36 36 @user.activated = true
37 37 if @user.save
38 38 flash[:notice] = 'User was successfully created.'
39 39 redirect_to :action => 'index'
40 40 else
41 41 render :action => 'new'
42 - end
42 + end
43 43 end
44 44
45 45 def clear_last_ip
46 46 @user = User.find(params[:id])
47 47 @user.last_ip = nil
48 48 @user.save
49 49 redirect_to action: 'index', page: params[:page]
50 50 end
51 51
52 52 def create_from_list
53 53 lines = params[:user_list]
54 54
55 - note = []
56 - error_note = []
57 - error_msg = nil
58 - ok_user = []
59 -
60 - lines.split("\n").each do |line|
61 - #split with large limit, this will cause consecutive ',' to be result in a blank
62 - items = line.chomp.split(',',1000)
63 - if items.length>=2
64 - login = items[0]
65 - full_name = items[1]
66 - remark =''
67 - user_alias = ''
68 -
69 - added_random_password = false
70 - added_password = false
71 - if items.length >= 3
72 - if items[2].chomp(" ").length > 0
73 - password = items[2].chomp(" ")
74 - added_password = true
75 - end
76 - else
77 - password = random_password
78 - added_random_password=true;
79 - end
80 -
81 - if items.length>= 4 and items[3].chomp(" ").length > 0;
82 - user_alias = items[3].chomp(" ")
83 - else
84 - user_alias = login
85 - end
86 -
87 55
88 - has_remark = false
89 - if items.length>=5
90 - remark = items[4].strip;
91 - has_remark = true
92 - end
56 + res = User.create_from_list(lines)
57 + error_logins = res[:error_logins]
58 + error_msg = res[:first_error]
59 + ok_user = res[:created_users]
93 60
94 - user = User.find_by_login(login)
95 - if (user)
96 - user.full_name = full_name
97 - user.remark = remark if has_remark
98 - user.password = password if added_password || added_random_password
99 - else
100 - #create a random password if none are given
101 - password = random_password unless password
102 - user = User.new({:login => login,
103 - :full_name => full_name,
104 - :password => password,
105 - :password_confirmation => password,
106 - :alias => user_alias,
107 - :remark => remark})
108 - end
109 - user.activated = true
110 -
111 - if user.save
112 - if added_random_password
113 - note << "'#{login}' (+)"
114 - else
115 - note << login
116 - end
117 - ok_user << user
118 - else
119 - error_note << "'#{login}'"
120 - error_msg = user.errors.full_messages.to_sentence unless error_msg
121 - end
122 -
123 - end
124 - end
125 61
126 62 #add to group
127 63 if params[:add_to_group]
128 - group = Group.where(id: params[:group_id]).first
129 - if group
130 - group.users << ok_user
131 - end
64 + group = Group.find_by(id: params[:group_id])&.add_users_skip_existing(ok_user)
132 65 end
133 66
134 67 # show flash
135 - if note.size > 0
136 - flash[:success] = 'User(s) ' + note.join(', ') +
137 - ' were successfully created. ' +
138 - '( (+) - created with random passwords.)'
68 + if ok_user.count > 0
69 + flash[:success] = "#{ok_user.count} user(s) was created or updated successfully"
139 70 end
140 - if error_note.size > 0
71 + if error_logins.size > 0
141 72 flash[:error] = "Following user(s) failed to be created: " + error_note.join(', ') + ". The error of the first failed one are: " + error_msg;
142 73 end
143 74 redirect_to :action => 'index'
144 75 end
145 76
146 77 def edit
147 78 @user = User.find(params[:id])
148 79 end
149 80
150 81 def update
151 82 @user = User.find(params[:id])
152 83 if @user.update_attributes(user_params)
153 84 flash[:notice] = 'User was successfully updated.'
154 85 redirect_to :action => 'show', :id => @user
155 86 else
156 87 render :action => 'edit'
157 88 end
158 89 end
159 90
160 91 def destroy
161 92 User.find(params[:id]).destroy
162 93 redirect_to :action => 'index'
163 94 end
164 95
165 96 def user_stat
166 97 if params[:commit] == 'download csv'
167 98 @problems = Problem.all
168 99 else
169 100 @problems = Problem.available_problems
170 101 end
171 102 @users = User.includes(:contests, :contest_stat).where(enabled: true)
172 103 @scorearray = Array.new
173 104 @users.each do |u|
174 105 ustat = Array.new
175 106 ustat[0] = u
176 107 @problems.each do |p|
177 108 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
178 109 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
179 110 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
180 111 else
181 112 ustat << [0,false]
182 113 end
183 114 end
184 115 @scorearray << ustat
185 116 end
186 117 if params[:commit] == 'download csv' then
187 118 csv = gen_csv_from_scorearray(@scorearray,@problems)
188 119 send_data csv, filename: 'last_score.csv'
@@ -356,124 +287,129
356 287
357 288 def admin
358 289 @admins = Role.where(name: 'admin').take.users
359 290 @tas = Role.where(name: 'ta').take.users
360 291 end
361 292
362 293 def modify_role
363 294 user = User.find_by_login(params[:login])
364 295 role = Role.find_by_name(params[:role])
365 296 unless user && role
366 297 flash[:error] = 'Unknown user or role'
367 298 redirect_to admin_user_admin_index_path
368 299 return
369 300 end
370 301 if params[:commit] == 'Grant'
371 302 #grant role
372 303 user.roles << role
373 304 flash[:notice] = "User '#{user.login}' has been granted the role '#{role.name}'"
374 305 else
375 306 #revoke role
376 307 if user.login == 'root' && role.name == 'admin'
377 308 flash[:error] = 'You cannot revoke admisnistrator permission from root.'
378 309 redirect_to admin_user_admin_index_path
379 310 return
380 311 end
381 312 user.roles.delete(role)
382 313 flash[:notice] = "The role '#{role.name}' has been revoked from User '#{user.login}'"
383 314 end
384 315 redirect_to admin_user_admin_index_path
385 316 end
386 317
387 318 # mass mailing
388 319
389 320 def mass_mailing
390 321 end
391 322
392 323 def bulk_mail
393 324 lines = params[:login_list]
394 325 if !lines or lines.blank?
395 326 flash[:notice] = 'You entered an empty list.'
396 327 redirect_to :action => 'mass_mailing' and return
397 328 end
398 329
399 330 mail_subject = params[:subject]
400 331 if !mail_subject or mail_subject.blank?
401 332 flash[:notice] = 'You entered an empty mail subject.'
402 333 redirect_to :action => 'mass_mailing' and return
403 334 end
404 -
335 +
405 336 mail_body = params[:email_body]
406 337 if !mail_body or mail_body.blank?
407 338 flash[:notice] = 'You entered an empty mail body.'
408 339 redirect_to :action => 'mass_mailing' and return
409 340 end
410 341
411 342 note = []
412 343 users = []
413 344 lines.split("\n").each do |line|
414 345 user = User.find_by_login(line.chomp)
415 346 if user
416 347 send_mail(user.email, mail_subject, mail_body)
417 348 note << user.login
418 349 end
419 350 end
420 -
351 +
421 352 flash[:notice] = 'User(s) ' + note.join(', ') +
422 353 ' were successfully modified. '
423 354 redirect_to :action => 'mass_mailing'
424 355 end
425 356
426 357 #bulk manage
427 358 def bulk_manage
428 359
429 - begin
430 - @users = User.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) if params[:regex]
431 - @users.count if @users #i don't know why I have to call count, but if I won't exception is not raised
360 + begin
361 + if params[:filter_group]
362 + @users = Group.find_by(id: params[:filter_group_id]).users
363 + else
364 + @users = User.all
365 + end
366 + @users = @users.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) unless params[:regex].blank?
367 + @users.count if @users #test the sql
432 368 rescue Exception
433 369 flash[:error] = 'Regular Expression is malformed'
434 370 @users = nil
435 371 end
436 372
437 373 if params[:commit]
438 374 @action = {}
439 375 @action[:set_enable] = params[:enabled]
440 376 @action[:enabled] = params[:enable] == "1"
441 377 @action[:gen_password] = params[:gen_password]
442 378 @action[:add_group] = params[:add_group]
443 379 @action[:group_name] = params[:group_name]
444 380 end
445 381
446 382 if params[:commit] == "Perform"
447 383 if @action[:set_enable]
448 384 @users.update_all(enabled: @action[:enabled])
449 385 end
450 386 if @action[:gen_password]
451 387 @users.each do |u|
452 388 password = random_password
453 389 u.password = password
454 390 u.password_confirmation = password
455 391 u.save
456 392 end
457 393 end
458 394 if @action[:add_group] and @action[:group_name]
459 395 @group = Group.find(@action[:group_name])
460 396 ok = []
461 397 failed = []
462 398 @users.each do |user|
463 399 begin
464 400 @group.users << user
465 401 ok << user.login
466 402 rescue => e
467 403 failed << user.login
468 404 end
469 405 end
470 406 flash[:success] = "The following users are added to the 'group #{@group.name}': " + ok.join(', ') if ok.count > 0
471 407 flash[:alert] = "The following users are already in the 'group #{@group.name}': " + failed.join(', ') if failed.count > 0
472 408 end
473 409 end
474 410 end
475 411
476 412 protected
477 413
478 414 def random_password(length=5)
479 415 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
@@ -1,13 +1,20
1 1 class Group < ActiveRecord::Base
2 2 has_many :groups_problems, class_name: 'GroupProblem'
3 3 has_many :problems, :through => :groups_problems
4 4
5 5 has_many :groups_users, class_name: 'GroupUser'
6 6 has_many :users, :through => :groups_users
7 7
8 8 #has_and_belongs_to_many :problems
9 9 #has_and_belongs_to_many :users
10 10
11 + def add_users_skip_existing(users_list)
12 + new_list = []
13 + users_list.each do |u|
14 + new_list << u unless users.include? u
15 + end
16 + users << new_list
17 + end
11 18
12 19 end
13 20
@@ -91,100 +91,96
91 91 end
92 92
93 93 def email_for_editing
94 94 if self.email==nil
95 95 "(unknown)"
96 96 elsif self.email==''
97 97 "(blank)"
98 98 else
99 99 self.email
100 100 end
101 101 end
102 102
103 103 def email_for_editing=(e)
104 104 self.email=e
105 105 end
106 106
107 107 def alias_for_editing
108 108 if self.alias==nil
109 109 "(unknown)"
110 110 elsif self.alias==''
111 111 "(blank)"
112 112 else
113 113 self.alias
114 114 end
115 115 end
116 116
117 117 def alias_for_editing=(e)
118 118 self.alias=e
119 119 end
120 120
121 121 def activation_key
122 122 if self.hashed_password==nil
123 123 encrypt_new_password
124 124 end
125 125 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
126 126 end
127 127
128 128 def verify_activation_key(key)
129 129 key == activation_key
130 130 end
131 131
132 132 def self.random_password(length=5)
133 133 chars = 'abcdefghjkmnopqrstuvwxyz'
134 134 password = ''
135 135 length.times { password << chars[rand(chars.length - 1)] }
136 136 password
137 137 end
138 138
139 - def self.find_non_admin_with_prefix(prefix='')
140 - users = User.all
141 - return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
142 - end
143 139
144 140 # Contest information
145 141
146 142 def self.find_users_with_no_contest()
147 143 users = User.all
148 144 return users.find_all { |u| u.contests.length == 0 }
149 145 end
150 146
151 147
152 148 def contest_time_left
153 149 if GraderConfiguration.contest_mode?
154 150 return nil if site==nil
155 151 return site.time_left
156 152 elsif GraderConfiguration.indv_contest_mode?
157 153 time_limit = GraderConfiguration.contest_time_limit
158 154 if time_limit == nil
159 155 return nil
160 156 end
161 157 if contest_stat==nil or contest_stat.started_at==nil
162 158 return (Time.now.gmtime + time_limit) - Time.now.gmtime
163 159 else
164 160 finish_time = contest_stat.started_at + time_limit
165 161 current_time = Time.now.gmtime
166 162 if current_time > finish_time
167 163 return 0
168 164 else
169 165 return finish_time - current_time
170 166 end
171 167 end
172 168 else
173 169 return nil
174 170 end
175 171 end
176 172
177 173 def contest_finished?
178 174 if GraderConfiguration.contest_mode?
179 175 return false if site==nil
180 176 return site.finished?
181 177 elsif GraderConfiguration.indv_contest_mode?
182 178 return false if self.contest_stat==nil
183 179 return contest_time_left == 0
184 180 else
185 181 return false
186 182 end
187 183 end
188 184
189 185 def contest_started?
190 186 if GraderConfiguration.indv_contest_mode?
@@ -269,103 +265,181
269 265 contests.enabled.each do |contest|
270 266 contest.problems.available.each do |problem|
271 267 if not pin.has_key? problem.id
272 268 contest_problems << problem
273 269 end
274 270 pin[problem.id] = true
275 271 end
276 272 end
277 273 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
278 274 return contest_problems + other_avaiable_problems
279 275 end
280 276 end
281 277
282 278 # new feature, get list of available problem in all enabled group that the user belongs to
283 279 def available_problems_in_group
284 280 problem = []
285 281 self.groups.where(enabled: true).each do |group|
286 282 group.problems.where(available: true).each { |p| problem << p }
287 283 end
288 284 problem.uniq!
289 285 if problem
290 286 problem.sort! do |a,b|
291 287 case
292 288 when a.date_added < b.date_added
293 289 1
294 290 when a.date_added > b.date_added
295 291 -1
296 292 else
297 293 a.name <=> b.name
298 294 end
299 295 end
300 296 return problem
301 297 else
302 298 return []
303 299 end
304 300 end
305 301
306 302 #check if the user has the right to view that problem
307 303 #this also consider group based problem policy
308 304 def can_view_problem?(problem)
309 305 return true if admin?
310 306 return available_problems.include? problem
311 307 end
312 308
313 309 def self.clear_last_login
314 310 User.update_all(:last_ip => nil)
315 311 end
316 312
313 + #create multiple user, one per lines of input
314 + def self.create_from_list(lines)
315 + error_logins = []
316 + first_error = nil
317 + created_users = []
318 +
319 + lines.split("\n").each do |line|
320 + #split with large limit, this will cause consecutive ',' to be result in a blank
321 + items = line.chomp.split(',',1000)
322 + if items.length>=2
323 + login = items[0]
324 + full_name = items[1]
325 + remark =''
326 + user_alias = ''
327 +
328 + added_random_password = false
329 + added_password = false
330 +
331 + #given password?
332 + if items.length >= 3
333 + if items[2].chomp(" ").length > 0
334 + password = items[2].chomp(" ")
335 + added_password = true
336 + end
337 + else
338 + password = random_password
339 + added_random_password=true;
340 + end
341 +
342 + #given alias?
343 + if items.length>= 4 and items[3].chomp(" ").length > 0;
344 + user_alias = items[3].chomp(" ")
345 + else
346 + user_alias = login
347 + end
348 +
349 + #given remark?
350 + has_remark = false
351 + if items.length>=5
352 + remark = items[4].strip;
353 + has_remark = true
354 + end
355 +
356 + user = User.find_by_login(login)
357 + if (user)
358 + user.full_name = full_name
359 + user.remark = remark if has_remark
360 + user.password = password if added_password || added_random_password
361 + else
362 + #create a random password if none are given
363 + password = random_password unless password
364 + user = User.new({:login => login,
365 + :full_name => full_name,
366 + :password => password,
367 + :password_confirmation => password,
368 + :alias => user_alias,
369 + :remark => remark})
370 + end
371 + user.activated = true
372 +
373 + if user.save
374 + created_users << user
375 + else
376 + error_logins << "'#{login}'"
377 + first_error = user.errors.full_messages.to_sentence unless first_error
378 + end
379 + end
380 + end
381 +
382 + return {error_logins: error_logins, first_error: first_error, created_users: created_users}
383 +
384 + end
385 +
386 + def self.find_non_admin_with_prefix(prefix='')
387 + users = User.all
388 + return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
389 + end
390 +
317 391 protected
318 392 def encrypt_new_password
319 393 return if password.blank?
320 394 self.salt = (10+rand(90)).to_s
321 395 self.hashed_password = User.encrypt(self.password,self.salt)
322 396 end
323 -
397 +
324 398 def assign_default_site
325 399 # have to catch error when migrating (because self.site is not available).
326 400 begin
327 401 if self.site==nil
328 402 self.site = Site.find_by_name('default')
329 403 if self.site==nil
330 404 self.site = Site.find(1) # when 'default has be renamed'
331 405 end
332 406 end
333 407 rescue
334 408 end
335 409 end
336 410
337 411 def assign_default_contest
338 412 # have to catch error when migrating (because self.site is not available).
339 413 begin
340 414 if self.contests.length == 0
341 415 default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name'])
342 416 if default_contest
343 417 self.contests = [default_contest]
344 418 end
345 419 end
346 420 rescue
347 421 end
348 422 end
349 423
350 424 def password_required?
351 425 self.hashed_password.blank? || !self.password.blank?
352 426 end
353 427
354 428 def self.encrypt(string,salt)
355 429 Digest::SHA1.hexdigest(salt + string)
356 430 end
357 431
358 432 def uniqueness_of_email_from_activated_users
359 433 user = User.activated_users.find_by_email(self.email)
360 434 if user and (user.login != self.login)
361 435 self.errors.add(:base,"Email has already been taken")
362 436 end
363 437 end
364 438
365 439 def enough_time_interval_between_same_email_registrations
366 440 return if !self.new_record?
367 441 return if self.activated
368 442 open_user = User.find_by_email(self.email,
369 443 :order => 'created_at DESC')
370 444 if open_user and open_user.created_at and
371 445 (open_user.created_at > Time.now.gmtime - 5.minutes)
@@ -1,73 +1,79
1 1 %h1 Bulk Manage User
2 2
3 3 = form_tag bulk_manage_user_admin_index_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 + .form-group
27 + .div.checkbox
28 + %label
29 + = check_box_tag :filter_group, 1, params[:filter_group] == '1'
30 + Apply to this group only
31 + = select_tag "filter_group_id", options_from_collection_for_select( Group.all, 'id','name',params[:filter_group_id]), id: 'group_name',class: 'select2'
26 32 .col-md-6
27 33 .panel.panel-primary
28 34 .panel-title.panel-heading
29 35 Action
30 36 .panel-body
31 37 .row.form-group
32 38 .col-md-6
33 39 %label.checkbox-inline
34 40 = check_box_tag "enabled", true, params[:enabled]
35 41 Change "Enabled" to
36 42 .col-md-3
37 43 %label.radio-inline
38 44 = radio_button_tag "enable", 1, params[:enable] == '1', id: 'enable-yes'
39 45 Yes
40 46 .col-md-3
41 47 %label.radio-inline
42 48 = radio_button_tag "enable", 0, params[:enable] == '0', id: 'enable-no'
43 49 No
44 50 .row.form-group
45 51 .col-md-6
46 52 %label.checkbox-inline
47 53 = check_box_tag "gen_password", true, params[:gen_password]
48 54 Generate new random password
49 55 .row.form-group
50 56 .col-md-4
51 57 %label.checkbox-inline
52 58 = check_box_tag "add_group", true, params[:add_group]
53 59 Add users to group
54 60 %label.col-md-3.control-label.text-right Group name
55 61 .col-md-5
56 62 = select_tag "group_name", options_from_collection_for_select( Group.all, 'id','name',params[:group_name]), id: 'group_name',class: 'form-control select2'
57 63
58 64
59 65 .row
60 66 .col-md-12
61 67 = submit_tag "Preview Result", class: 'btn btn-default'
62 68 - if @users
63 69 .row
64 70 .col-md-4
65 71 - if @action
66 72 %h2 Confirmation
67 73 - if @action[:set_enable]
68 74 .alert.alert-info The following users will be set #{(@action[:enabled] ? 'enable' : 'disable')}.
69 75 - if @action[:gen_password]
70 76 .alert.alert-info The password of the following users will be randomly generated.
71 77 .row
72 78 .col-md-4
73 79 = submit_tag "Perform", class: 'btn btn-primary'
You need to be logged in to leave comments. Login now