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
@@ -30,123 +30,54
30 def new
30 def new
31 @user = User.new
31 @user = User.new
32 end
32 end
33
33
34 def create
34 def create
35 @user = User.new(user_params)
35 @user = User.new(user_params)
36 @user.activated = true
36 @user.activated = true
37 if @user.save
37 if @user.save
38 flash[:notice] = 'User was successfully created.'
38 flash[:notice] = 'User was successfully created.'
39 redirect_to :action => 'index'
39 redirect_to :action => 'index'
40 else
40 else
41 render :action => 'new'
41 render :action => 'new'
42 - end
42 + end
43 end
43 end
44
44
45 def clear_last_ip
45 def clear_last_ip
46 @user = User.find(params[:id])
46 @user = User.find(params[:id])
47 @user.last_ip = nil
47 @user.last_ip = nil
48 @user.save
48 @user.save
49 redirect_to action: 'index', page: params[:page]
49 redirect_to action: 'index', page: params[:page]
50 end
50 end
51
51
52 def create_from_list
52 def create_from_list
53 lines = params[:user_list]
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
56 + res = User.create_from_list(lines)
89 - if items.length>=5
57 + error_logins = res[:error_logins]
90 - remark = items[4].strip;
58 + error_msg = res[:first_error]
91 - has_remark = true
59 + ok_user = res[:created_users]
92 - end
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 #add to group
62 #add to group
127 if params[:add_to_group]
63 if params[:add_to_group]
128 - group = Group.where(id: params[:group_id]).first
64 + group = Group.find_by(id: params[:group_id])&.add_users_skip_existing(ok_user)
129 - if group
130 - group.users << ok_user
131 - end
132 end
65 end
133
66
134 # show flash
67 # show flash
135 - if note.size > 0
68 + if ok_user.count > 0
136 - flash[:success] = 'User(s) ' + note.join(', ') +
69 + flash[:success] = "#{ok_user.count} user(s) was created or updated successfully"
137 - ' were successfully created. ' +
138 - '( (+) - created with random passwords.)'
139 end
70 end
140 - if error_note.size > 0
71 + if error_logins.size > 0
141 flash[:error] = "Following user(s) failed to be created: " + error_note.join(', ') + ". The error of the first failed one are: " + error_msg;
72 flash[:error] = "Following user(s) failed to be created: " + error_note.join(', ') + ". The error of the first failed one are: " + error_msg;
142 end
73 end
143 redirect_to :action => 'index'
74 redirect_to :action => 'index'
144 end
75 end
145
76
146 def edit
77 def edit
147 @user = User.find(params[:id])
78 @user = User.find(params[:id])
148 end
79 end
149
80
150 def update
81 def update
151 @user = User.find(params[:id])
82 @user = User.find(params[:id])
152 if @user.update_attributes(user_params)
83 if @user.update_attributes(user_params)
@@ -392,52 +323,57
392 def bulk_mail
323 def bulk_mail
393 lines = params[:login_list]
324 lines = params[:login_list]
394 if !lines or lines.blank?
325 if !lines or lines.blank?
395 flash[:notice] = 'You entered an empty list.'
326 flash[:notice] = 'You entered an empty list.'
396 redirect_to :action => 'mass_mailing' and return
327 redirect_to :action => 'mass_mailing' and return
397 end
328 end
398
329
399 mail_subject = params[:subject]
330 mail_subject = params[:subject]
400 if !mail_subject or mail_subject.blank?
331 if !mail_subject or mail_subject.blank?
401 flash[:notice] = 'You entered an empty mail subject.'
332 flash[:notice] = 'You entered an empty mail subject.'
402 redirect_to :action => 'mass_mailing' and return
333 redirect_to :action => 'mass_mailing' and return
403 end
334 end
404 -
335 +
405 mail_body = params[:email_body]
336 mail_body = params[:email_body]
406 if !mail_body or mail_body.blank?
337 if !mail_body or mail_body.blank?
407 flash[:notice] = 'You entered an empty mail body.'
338 flash[:notice] = 'You entered an empty mail body.'
408 redirect_to :action => 'mass_mailing' and return
339 redirect_to :action => 'mass_mailing' and return
409 end
340 end
410
341
411 note = []
342 note = []
412 users = []
343 users = []
413 lines.split("\n").each do |line|
344 lines.split("\n").each do |line|
414 user = User.find_by_login(line.chomp)
345 user = User.find_by_login(line.chomp)
415 if user
346 if user
416 send_mail(user.email, mail_subject, mail_body)
347 send_mail(user.email, mail_subject, mail_body)
417 note << user.login
348 note << user.login
418 end
349 end
419 end
350 end
420 -
351 +
421 flash[:notice] = 'User(s) ' + note.join(', ') +
352 flash[:notice] = 'User(s) ' + note.join(', ') +
422 ' were successfully modified. '
353 ' were successfully modified. '
423 redirect_to :action => 'mass_mailing'
354 redirect_to :action => 'mass_mailing'
424 end
355 end
425
356
426 #bulk manage
357 #bulk manage
427 def bulk_manage
358 def bulk_manage
428
359
429 - begin
360 + begin
430 - @users = User.where('(login REGEXP ?) OR (remark REGEXP ?)',params[:regex],params[:regex]) if params[:regex]
361 + if params[:filter_group]
431 - @users.count if @users #i don't know why I have to call count, but if I won't exception is not raised
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 rescue Exception
368 rescue Exception
433 flash[:error] = 'Regular Expression is malformed'
369 flash[:error] = 'Regular Expression is malformed'
434 @users = nil
370 @users = nil
435 end
371 end
436
372
437 if params[:commit]
373 if params[:commit]
438 @action = {}
374 @action = {}
439 @action[:set_enable] = params[:enabled]
375 @action[:set_enable] = params[:enabled]
440 @action[:enabled] = params[:enable] == "1"
376 @action[:enabled] = params[:enable] == "1"
441 @action[:gen_password] = params[:gen_password]
377 @action[:gen_password] = params[:gen_password]
442 @action[:add_group] = params[:add_group]
378 @action[:add_group] = params[:add_group]
443 @action[:group_name] = params[:group_name]
379 @action[:group_name] = params[:group_name]
@@ -1,13 +1,20
1 class Group < ActiveRecord::Base
1 class Group < ActiveRecord::Base
2 has_many :groups_problems, class_name: 'GroupProblem'
2 has_many :groups_problems, class_name: 'GroupProblem'
3 has_many :problems, :through => :groups_problems
3 has_many :problems, :through => :groups_problems
4
4
5 has_many :groups_users, class_name: 'GroupUser'
5 has_many :groups_users, class_name: 'GroupUser'
6 has_many :users, :through => :groups_users
6 has_many :users, :through => :groups_users
7
7
8 #has_and_belongs_to_many :problems
8 #has_and_belongs_to_many :problems
9 #has_and_belongs_to_many :users
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 end
19 end
13
20
@@ -127,28 +127,24
127
127
128 def verify_activation_key(key)
128 def verify_activation_key(key)
129 key == activation_key
129 key == activation_key
130 end
130 end
131
131
132 def self.random_password(length=5)
132 def self.random_password(length=5)
133 chars = 'abcdefghjkmnopqrstuvwxyz'
133 chars = 'abcdefghjkmnopqrstuvwxyz'
134 password = ''
134 password = ''
135 length.times { password << chars[rand(chars.length - 1)] }
135 length.times { password << chars[rand(chars.length - 1)] }
136 password
136 password
137 end
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 # Contest information
140 # Contest information
145
141
146 def self.find_users_with_no_contest()
142 def self.find_users_with_no_contest()
147 users = User.all
143 users = User.all
148 return users.find_all { |u| u.contests.length == 0 }
144 return users.find_all { |u| u.contests.length == 0 }
149 end
145 end
150
146
151
147
152 def contest_time_left
148 def contest_time_left
153 if GraderConfiguration.contest_mode?
149 if GraderConfiguration.contest_mode?
154 return nil if site==nil
150 return nil if site==nil
@@ -305,31 +301,109
305
301
306 #check if the user has the right to view that problem
302 #check if the user has the right to view that problem
307 #this also consider group based problem policy
303 #this also consider group based problem policy
308 def can_view_problem?(problem)
304 def can_view_problem?(problem)
309 return true if admin?
305 return true if admin?
310 return available_problems.include? problem
306 return available_problems.include? problem
311 end
307 end
312
308
313 def self.clear_last_login
309 def self.clear_last_login
314 User.update_all(:last_ip => nil)
310 User.update_all(:last_ip => nil)
315 end
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 protected
391 protected
318 def encrypt_new_password
392 def encrypt_new_password
319 return if password.blank?
393 return if password.blank?
320 self.salt = (10+rand(90)).to_s
394 self.salt = (10+rand(90)).to_s
321 self.hashed_password = User.encrypt(self.password,self.salt)
395 self.hashed_password = User.encrypt(self.password,self.salt)
322 end
396 end
323 -
397 +
324 def assign_default_site
398 def assign_default_site
325 # have to catch error when migrating (because self.site is not available).
399 # have to catch error when migrating (because self.site is not available).
326 begin
400 begin
327 if self.site==nil
401 if self.site==nil
328 self.site = Site.find_by_name('default')
402 self.site = Site.find_by_name('default')
329 if self.site==nil
403 if self.site==nil
330 self.site = Site.find(1) # when 'default has be renamed'
404 self.site = Site.find(1) # when 'default has be renamed'
331 end
405 end
332 end
406 end
333 rescue
407 rescue
334 end
408 end
335 end
409 end
@@ -14,24 +14,30
14 %p
14 %p
15 Example
15 Example
16 %ul
16 %ul
17 %li
17 %li
18 %code root
18 %code root
19 matches every user whose login contains "root"
19 matches every user whose login contains "root"
20 %li
20 %li
21 %code ^56
21 %code ^56
22 matches every user whose login starts with "56"
22 matches every user whose login starts with "56"
23 %li
23 %li
24 %code 21$
24 %code 21$
25 matches every user whose login ends with "21"
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 .col-md-6
32 .col-md-6
27 .panel.panel-primary
33 .panel.panel-primary
28 .panel-title.panel-heading
34 .panel-title.panel-heading
29 Action
35 Action
30 .panel-body
36 .panel-body
31 .row.form-group
37 .row.form-group
32 .col-md-6
38 .col-md-6
33 %label.checkbox-inline
39 %label.checkbox-inline
34 = check_box_tag "enabled", true, params[:enabled]
40 = check_box_tag "enabled", true, params[:enabled]
35 Change "Enabled" to
41 Change "Enabled" to
36 .col-md-3
42 .col-md-3
37 %label.radio-inline
43 %label.radio-inline
You need to be logged in to leave comments. Login now