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

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