Description:
fix user update
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r639:2826fdf23cce - - 2 files changed: 4 inserted, 4 deleted
@@ -169,387 +169,387 | |||
|
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 | # contest management |
|
232 | 232 | |
|
233 | 233 | def contests |
|
234 | 234 | @contest, @users = find_contest_and_user_from_contest_id(params[:id]) |
|
235 | 235 | @contests = Contest.enabled |
|
236 | 236 | end |
|
237 | 237 | |
|
238 | 238 | def assign_from_list |
|
239 | 239 | contest_id = params[:users_contest_id] |
|
240 | 240 | org_contest, users = find_contest_and_user_from_contest_id(contest_id) |
|
241 | 241 | contest = Contest.find(params[:new_contest][:id]) |
|
242 | 242 | if !contest |
|
243 | 243 | flash[:notice] = 'Error: no contest' |
|
244 | 244 | redirect_to :action => 'contests', :id =>contest_id |
|
245 | 245 | end |
|
246 | 246 | |
|
247 | 247 | note = [] |
|
248 | 248 | users.each do |u| |
|
249 | 249 | u.contests = [contest] |
|
250 | 250 | note << u.login |
|
251 | 251 | end |
|
252 | 252 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
253 | 253 | " were successfully reassigned to #{contest.title}." |
|
254 | 254 | redirect_to :action => 'contests', :id =>contest.id |
|
255 | 255 | end |
|
256 | 256 | |
|
257 | 257 | def add_to_contest |
|
258 | 258 | user = User.find(params[:id]) |
|
259 | 259 | contest = Contest.find(params[:contest_id]) |
|
260 | 260 | if user and contest |
|
261 | 261 | user.contests << contest |
|
262 | 262 | end |
|
263 | 263 | redirect_to :action => 'index' |
|
264 | 264 | end |
|
265 | 265 | |
|
266 | 266 | def remove_from_contest |
|
267 | 267 | user = User.find(params[:id]) |
|
268 | 268 | contest = Contest.find(params[:contest_id]) |
|
269 | 269 | if user and contest |
|
270 | 270 | user.contests.delete(contest) |
|
271 | 271 | end |
|
272 | 272 | redirect_to :action => 'index' |
|
273 | 273 | end |
|
274 | 274 | |
|
275 | 275 | def contest_management |
|
276 | 276 | end |
|
277 | 277 | |
|
278 | 278 | def manage_contest |
|
279 | 279 | contest = Contest.find(params[:contest][:id]) |
|
280 | 280 | if !contest |
|
281 | 281 | flash[:notice] = 'You did not choose the contest.' |
|
282 | 282 | redirect_to :action => 'contest_management' and return |
|
283 | 283 | end |
|
284 | 284 | |
|
285 | 285 | operation = params[:operation] |
|
286 | 286 | |
|
287 | 287 | if not ['add','remove','assign'].include? operation |
|
288 | 288 | flash[:notice] = 'You did not choose the operation to perform.' |
|
289 | 289 | redirect_to :action => 'contest_management' and return |
|
290 | 290 | end |
|
291 | 291 | |
|
292 | 292 | lines = params[:login_list] |
|
293 | 293 | if !lines or lines.blank? |
|
294 | 294 | flash[:notice] = 'You entered an empty list.' |
|
295 | 295 | redirect_to :action => 'contest_management' and return |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | note = [] |
|
299 | 299 | users = [] |
|
300 | 300 | lines.split("\n").each do |line| |
|
301 | 301 | user = User.find_by_login(line.chomp) |
|
302 | 302 | if user |
|
303 | 303 | if operation=='add' |
|
304 | 304 | if ! user.contests.include? contest |
|
305 | 305 | user.contests << contest |
|
306 | 306 | end |
|
307 | 307 | elsif operation=='remove' |
|
308 | 308 | user.contests.delete(contest) |
|
309 | 309 | else |
|
310 | 310 | user.contests = [contest] |
|
311 | 311 | end |
|
312 | 312 | |
|
313 | 313 | if params[:reset_timer] |
|
314 | 314 | user.contest_stat.forced_logout = true |
|
315 | 315 | user.contest_stat.reset_timer_and_save |
|
316 | 316 | end |
|
317 | 317 | |
|
318 | 318 | if params[:notification_emails] |
|
319 | 319 | send_contest_update_notification_email(user, contest) |
|
320 | 320 | end |
|
321 | 321 | |
|
322 | 322 | note << user.login |
|
323 | 323 | users << user |
|
324 | 324 | end |
|
325 | 325 | end |
|
326 | 326 | |
|
327 | 327 | if params[:reset_timer] |
|
328 | 328 | logout_users(users) |
|
329 | 329 | end |
|
330 | 330 | |
|
331 | 331 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
332 | 332 | ' were successfully modified. ' |
|
333 | 333 | redirect_to :action => 'contest_management' |
|
334 | 334 | end |
|
335 | 335 | |
|
336 | 336 | # admin management |
|
337 | 337 | |
|
338 | 338 | def admin |
|
339 | 339 | @admins = User.all.find_all {|user| user.admin? } |
|
340 | 340 | end |
|
341 | 341 | |
|
342 | 342 | def grant_admin |
|
343 | 343 | login = params[:login] |
|
344 | 344 | user = User.find_by_login(login) |
|
345 | 345 | if user!=nil |
|
346 | 346 | admin_role = Role.find_by_name('admin') |
|
347 | 347 | user.roles << admin_role |
|
348 | 348 | else |
|
349 | 349 | flash[:notice] = 'Unknown user' |
|
350 | 350 | end |
|
351 | 351 | flash[:notice] = 'User added as admins' |
|
352 | 352 | redirect_to :action => 'admin' |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | def revoke_admin |
|
356 | 356 | user = User.find(params[:id]) |
|
357 | 357 | if user==nil |
|
358 | 358 | flash[:notice] = 'Unknown user' |
|
359 | 359 | redirect_to :action => 'admin' and return |
|
360 | 360 | elsif user.login == 'root' |
|
361 | 361 | flash[:notice] = 'You cannot revoke admisnistrator permission from root.' |
|
362 | 362 | redirect_to :action => 'admin' and return |
|
363 | 363 | end |
|
364 | 364 | |
|
365 | 365 | admin_role = Role.find_by_name('admin') |
|
366 | 366 | user.roles.delete(admin_role) |
|
367 | 367 | flash[:notice] = 'User permission revoked' |
|
368 | 368 | redirect_to :action => 'admin' |
|
369 | 369 | end |
|
370 | 370 | |
|
371 | 371 | # mass mailing |
|
372 | 372 | |
|
373 | 373 | def mass_mailing |
|
374 | 374 | end |
|
375 | 375 | |
|
376 | 376 | def bulk_mail |
|
377 | 377 | lines = params[:login_list] |
|
378 | 378 | if !lines or lines.blank? |
|
379 | 379 | flash[:notice] = 'You entered an empty list.' |
|
380 | 380 | redirect_to :action => 'mass_mailing' and return |
|
381 | 381 | end |
|
382 | 382 | |
|
383 | 383 | mail_subject = params[:subject] |
|
384 | 384 | if !mail_subject or mail_subject.blank? |
|
385 | 385 | flash[:notice] = 'You entered an empty mail subject.' |
|
386 | 386 | redirect_to :action => 'mass_mailing' and return |
|
387 | 387 | end |
|
388 | 388 | |
|
389 | 389 | mail_body = params[:email_body] |
|
390 | 390 | if !mail_body or mail_body.blank? |
|
391 | 391 | flash[:notice] = 'You entered an empty mail body.' |
|
392 | 392 | redirect_to :action => 'mass_mailing' and return |
|
393 | 393 | end |
|
394 | 394 | |
|
395 | 395 | note = [] |
|
396 | 396 | users = [] |
|
397 | 397 | lines.split("\n").each do |line| |
|
398 | 398 | user = User.find_by_login(line.chomp) |
|
399 | 399 | if user |
|
400 | 400 | send_mail(user.email, mail_subject, mail_body) |
|
401 | 401 | note << user.login |
|
402 | 402 | end |
|
403 | 403 | end |
|
404 | 404 | |
|
405 | 405 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
406 | 406 | ' were successfully modified. ' |
|
407 | 407 | redirect_to :action => 'mass_mailing' |
|
408 | 408 | end |
|
409 | 409 | |
|
410 | 410 | protected |
|
411 | 411 | |
|
412 | 412 | def random_password(length=5) |
|
413 | 413 | chars = 'abcdefghijkmnopqrstuvwxyz23456789' |
|
414 | 414 | newpass = "" |
|
415 | 415 | length.times { newpass << chars[rand(chars.size-1)] } |
|
416 | 416 | return newpass |
|
417 | 417 | end |
|
418 | 418 | |
|
419 | 419 | def import_from_file(f) |
|
420 | 420 | data_hash = YAML.load(f) |
|
421 | 421 | @import_log = "" |
|
422 | 422 | |
|
423 | 423 | country_data = data_hash[:countries] |
|
424 | 424 | site_data = data_hash[:sites] |
|
425 | 425 | user_data = data_hash[:users] |
|
426 | 426 | |
|
427 | 427 | # import country |
|
428 | 428 | countries = {} |
|
429 | 429 | country_data.each_pair do |id,country| |
|
430 | 430 | c = Country.find_by_name(country[:name]) |
|
431 | 431 | if c!=nil |
|
432 | 432 | countries[id] = c |
|
433 | 433 | @import_log << "Found #{country[:name]}\n" |
|
434 | 434 | else |
|
435 | 435 | countries[id] = Country.new(:name => country[:name]) |
|
436 | 436 | countries[id].save |
|
437 | 437 | @import_log << "Created #{country[:name]}\n" |
|
438 | 438 | end |
|
439 | 439 | end |
|
440 | 440 | |
|
441 | 441 | # import sites |
|
442 | 442 | sites = {} |
|
443 | 443 | site_data.each_pair do |id,site| |
|
444 | 444 | s = Site.find_by_name(site[:name]) |
|
445 | 445 | if s!=nil |
|
446 | 446 | @import_log << "Found #{site[:name]}\n" |
|
447 | 447 | else |
|
448 | 448 | s = Site.new(:name => site[:name]) |
|
449 | 449 | @import_log << "Created #{site[:name]}\n" |
|
450 | 450 | end |
|
451 | 451 | s.password = site[:password] |
|
452 | 452 | s.country = countries[site[:country_id]] |
|
453 | 453 | s.save |
|
454 | 454 | sites[id] = s |
|
455 | 455 | end |
|
456 | 456 | |
|
457 | 457 | # import users |
|
458 | 458 | user_data.each_pair do |id,user| |
|
459 | 459 | u = User.find_by_login(user[:login]) |
|
460 | 460 | if u!=nil |
|
461 | 461 | @import_log << "Found #{user[:login]}\n" |
|
462 | 462 | else |
|
463 | 463 | u = User.new(:login => user[:login]) |
|
464 | 464 | @import_log << "Created #{user[:login]}\n" |
|
465 | 465 | end |
|
466 | 466 | u.full_name = user[:name] |
|
467 | 467 | u.password = user[:password] |
|
468 | 468 | u.country = countries[user[:country_id]] |
|
469 | 469 | u.site = sites[user[:site_id]] |
|
470 | 470 | u.activated = true |
|
471 | 471 | u.email = "empty-#{u.login}@none.com" |
|
472 | 472 | if not u.save |
|
473 | 473 | @import_log << "Errors\n" |
|
474 | 474 | u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" } |
|
475 | 475 | end |
|
476 | 476 | end |
|
477 | 477 | |
|
478 | 478 | end |
|
479 | 479 | |
|
480 | 480 | def logout_users(users) |
|
481 | 481 | users.each do |user| |
|
482 | 482 | contest_stat = user.contest_stat(true) |
|
483 | 483 | if contest_stat and !contest_stat.forced_logout |
|
484 | 484 | contest_stat.forced_logout = true |
|
485 | 485 | contest_stat.save |
|
486 | 486 | end |
|
487 | 487 | end |
|
488 | 488 | end |
|
489 | 489 | |
|
490 | 490 | def send_contest_update_notification_email(user, contest) |
|
491 | 491 | contest_title_name = GraderConfiguration['contest.name'] |
|
492 | 492 | contest_name = contest.name |
|
493 | 493 | mail_subject = t('contest.notification.email_subject', { |
|
494 | 494 | :contest_title_name => contest_title_name, |
|
495 | 495 | :contest_name => contest_name }) |
|
496 | 496 | mail_body = t('contest.notification.email_body', { |
|
497 | 497 | :full_name => user.full_name, |
|
498 | 498 | :contest_title_name => contest_title_name, |
|
499 | 499 | :contest_name => contest.name, |
|
500 | 500 | }) |
|
501 | 501 | |
|
502 | 502 | logger.info mail_body |
|
503 | 503 | send_mail(user.email, mail_subject, mail_body) |
|
504 | 504 | end |
|
505 | 505 | |
|
506 | 506 | def find_contest_and_user_from_contest_id(id) |
|
507 | 507 | if id!='none' |
|
508 | 508 | @contest = Contest.find(id) |
|
509 | 509 | else |
|
510 | 510 | @contest = nil |
|
511 | 511 | end |
|
512 | 512 | if @contest |
|
513 | 513 | @users = @contest.users |
|
514 | 514 | else |
|
515 | 515 | @users = User.find_users_with_no_contest |
|
516 | 516 | end |
|
517 | 517 | return [@contest, @users] |
|
518 | 518 | end |
|
519 | 519 | |
|
520 | 520 | def gen_csv_from_scorearray(scorearray,problem) |
|
521 | 521 | CSV.generate do |csv| |
|
522 | 522 | #add header |
|
523 | 523 | header = ['User','Name', 'Activated?', 'Logged in', 'Contest'] |
|
524 | 524 | problem.each { |p| header << p.name } |
|
525 | 525 | header += ['Total','Passed'] |
|
526 | 526 | csv << header |
|
527 | 527 | #add data |
|
528 | 528 | scorearray.each do |sc| |
|
529 | 529 | total = num_passed = 0 |
|
530 | 530 | row = Array.new |
|
531 | 531 | sc.each_index do |i| |
|
532 | 532 | if i == 0 |
|
533 | 533 | row << sc[i].login |
|
534 | 534 | row << sc[i].full_name |
|
535 | 535 | row << sc[i].activated |
|
536 | 536 | row << (sc[i].try(:contest_stat).try(:started_at).nil? ? 'no' : 'yes') |
|
537 | 537 | row << sc[i].contests.collect {|c| c.name}.join(', ') |
|
538 | 538 | else |
|
539 | 539 | row << sc[i][0] |
|
540 | 540 | total += sc[i][0] |
|
541 | 541 | num_passed += 1 if sc[i][1] |
|
542 | 542 | end |
|
543 | 543 | end |
|
544 | 544 | row << total |
|
545 | 545 | row << num_passed |
|
546 | 546 | csv << row |
|
547 | 547 | end |
|
548 | 548 | end |
|
549 | 549 | end |
|
550 | 550 | |
|
551 | 551 | private |
|
552 | 552 | def user_params |
|
553 | - params.require(:user).permit(:login,:full_name,:hashed_password,:salt,:alias,:email,:site_id,:country_id,:activated,:enabled,:remark,:last_ip,:section) | |
|
553 | + params.require(:user).permit(:login,:password,:password_confirmation,:email, :alias, :full_name,:remark) | |
|
554 | 554 | end |
|
555 | 555 | end |
@@ -1,50 +1,50 | |||
|
1 | 1 | require 'test_helper' |
|
2 | 2 | |
|
3 | 3 | class AnnouncementsControllerTest < ActionController::TestCase |
|
4 | 4 | setup do |
|
5 | 5 | @announcement = announcements(:one) |
|
6 |
- @request.session[:user_id] = user |
|
|
6 | + @request.session[:user_id] = user(:admin).id | |
|
7 | 7 | end |
|
8 | 8 | |
|
9 | 9 | test "should get index" do |
|
10 | 10 | get :index |
|
11 | 11 | assert_response :success |
|
12 | 12 | assert_not_nil assigns(:announcements) |
|
13 | 13 | end |
|
14 | 14 | |
|
15 | 15 | test "should get new" do |
|
16 | 16 | get :new |
|
17 | 17 | assert_response :success |
|
18 | 18 | end |
|
19 | 19 | |
|
20 | 20 | test "should create announcement" do |
|
21 | 21 | assert_difference('Announcement.count') do |
|
22 |
- post :create, announcement: { |
|
|
22 | + post :create, announcement: { } | |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | test "should show announcement" do |
|
29 | 29 | get :show, id: @announcement |
|
30 | 30 | assert_response :success |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | test "should get edit" do |
|
34 | 34 | get :edit, id: @announcement |
|
35 | 35 | assert_response :success |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | test "should update announcement" do |
|
39 |
- patch :update, id: @announcement, announcement: { |
|
|
39 | + patch :update, id: @announcement, announcement: { } | |
|
40 | 40 | assert_redirected_to announcement_path(assigns(:announcement)) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | test "should destroy announcement" do |
|
44 | 44 | assert_difference('Announcement.count', -1) do |
|
45 | 45 | delete :destroy, id: @announcement |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | assert_redirected_to announcements_path |
|
49 | 49 | end |
|
50 | 50 | end |
You need to be logged in to leave comments.
Login now