Description:
fix user profile, add test
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r759:6c24fe0db627 - - 7 files changed: 64 inserted, 43 deleted

@@ -1,66 +1,74
1 require 'net/smtp'
1 require 'net/smtp'
2
2
3 class UsersController < ApplicationController
3 class UsersController < ApplicationController
4
4
5 include MailHelperMethods
5 include MailHelperMethods
6
6
7 before_action :check_valid_login, :except => [:new,
7 before_action :check_valid_login, :except => [:new,
8 :register,
8 :register,
9 :confirm,
9 :confirm,
10 :forget,
10 :forget,
11 :retrieve_password]
11 :retrieve_password]
12
12
13 before_action :verify_online_registration, :only => [:new,
13 before_action :verify_online_registration, :only => [:new,
14 :register,
14 :register,
15 :forget,
15 :forget,
16 :retrieve_password]
16 :retrieve_password]
17 - before_action :check_valid_login, :profile_authorization, only: [:profile]
18
17
19 before_action :admin_authorization, only: [:stat, :toggle_activate, :toggle_enable]
18 before_action :admin_authorization, only: [:stat, :toggle_activate, :toggle_enable]
20
19
21
20
22 #in_place_edit_for :user, :alias_for_editing
21 #in_place_edit_for :user, :alias_for_editing
23 #in_place_edit_for :user, :email_for_editing
22 #in_place_edit_for :user, :email_for_editing
24
23
25 def index
24 def index
26 if !GraderConfiguration['system.user_setting_enabled']
25 if !GraderConfiguration['system.user_setting_enabled']
27 redirect_to :controller => 'main', :action => 'list'
26 redirect_to :controller => 'main', :action => 'list'
28 else
27 else
29 @user = User.find(session[:user_id])
28 @user = User.find(session[:user_id])
30 end
29 end
31 end
30 end
32
31
32 + # edit logged in user profile
33 + def profile
34 + if !GraderConfiguration['system.user_setting_enabled']
35 + redirect_to :controller => 'main', :action => 'list'
36 + else
37 + @user = current_user;
38 + end
39 + end
40 +
33 def chg_passwd
41 def chg_passwd
34 user = User.find(session[:user_id])
42 user = User.find(session[:user_id])
35 - user.password = params[:passwd]
43 + user.password = params[:password]
36 - user.password_confirmation = params[:passwd_verify]
44 + user.password_confirmation = params[:password_confirmation]
37 if user.save
45 if user.save
38 flash[:notice] = 'password changed'
46 flash[:notice] = 'password changed'
39 else
47 else
40 flash[:notice] = 'Error: password changing failed'
48 flash[:notice] = 'Error: password changing failed'
41 end
49 end
42 - redirect_to :action => 'index'
50 + redirect_to :action => 'profile'
43 end
51 end
44
52
45 def new
53 def new
46 @user = User.new
54 @user = User.new
47 render :action => 'new', :layout => 'empty'
55 render :action => 'new', :layout => 'empty'
48 end
56 end
49
57
50 def register
58 def register
51 if(params[:cancel])
59 if(params[:cancel])
52 redirect_to :controller => 'main', :action => 'login'
60 redirect_to :controller => 'main', :action => 'login'
53 return
61 return
54 end
62 end
55 @user = User.new(user_params)
63 @user = User.new(user_params)
56 @user.password_confirmation = @user.password = User.random_password
64 @user.password_confirmation = @user.password = User.random_password
57 @user.activated = false
65 @user.activated = false
58 if (@user.valid?) and (@user.save)
66 if (@user.valid?) and (@user.save)
59 if send_confirmation_email(@user)
67 if send_confirmation_email(@user)
60 render :action => 'new_splash', :layout => 'empty'
68 render :action => 'new_splash', :layout => 'empty'
61 else
69 else
62 @admin_email = GraderConfiguration['system.admin_email']
70 @admin_email = GraderConfiguration['system.admin_email']
63 render :action => 'email_error', :layout => 'empty'
71 render :action => 'email_error', :layout => 'empty'
64 end
72 end
65 else
73 else
66 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
74 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
@@ -194,26 +202,25
194
202
195 logger.info mail_body
203 logger.info mail_body
196
204
197 send_mail(user.email, mail_subject, mail_body)
205 send_mail(user.email, mail_subject, mail_body)
198 end
206 end
199
207
200 # allow viewing of regular user profile only when options allow so
208 # allow viewing of regular user profile only when options allow so
201 # only admins can view admins profile
209 # only admins can view admins profile
202 def profile_authorization
210 def profile_authorization
203 #if view admins' profile, allow only admin
211 #if view admins' profile, allow only admin
204 return false unless(params[:id])
212 return false unless(params[:id])
205 user = User.find(params[:id])
213 user = User.find(params[:id])
206 return false unless user
214 return false unless user
207 return admin_authorization if user.admin?
215 return admin_authorization if user.admin?
208 return true if GraderConfiguration["right.user_view_submission"]
216 return true if GraderConfiguration["right.user_view_submission"]
209
217
210 #finally, we allow only admin
218 #finally, we allow only admin
211 admin_authorization
219 admin_authorization
212 end
220 end
213
221
214 private
222 private
215 def user_params
223 def user_params
216 params.require(:user).permit(:login, :full_name, :email)
224 params.require(:user).permit(:login, :full_name, :email)
217 end
225 end
218 -
219 end
226 end
@@ -53,43 +53,43
53 = add_menu( 'Message ', 'messages', 'console')
53 = add_menu( 'Message ', 'messages', 'console')
54 %li.divider{role: 'separator'}
54 %li.divider{role: 'separator'}
55 = add_menu( 'System config', 'configurations', 'index')
55 = add_menu( 'System config', 'configurations', 'index')
56 %li.divider{role: 'separator'}
56 %li.divider{role: 'separator'}
57 = add_menu( 'Sites', 'sites', 'index')
57 = add_menu( 'Sites', 'sites', 'index')
58 = add_menu( 'Contests', 'contest_management', 'index')
58 = add_menu( 'Contests', 'contest_management', 'index')
59 / report
59 / report
60 %li.dropdown
60 %li.dropdown
61 %a.dropdown-toggle{href: '#', data: {toggle:'dropdown'}, aria: {haspopup:"true", expanded:"false"}, role: "button"}
61 %a.dropdown-toggle{href: '#', data: {toggle:'dropdown'}, aria: {haspopup:"true", expanded:"false"}, role: "button"}
62 Report
62 Report
63 %span.caret
63 %span.caret
64 %ul.dropdown-menu
64 %ul.dropdown-menu
65 = add_menu( 'Current Score', 'report', 'current_score')
65 = add_menu( 'Current Score', 'report', 'current_score')
66 = add_menu( 'Score Report', 'report', 'max_score')
66 = add_menu( 'Score Report', 'report', 'max_score')
67 = add_menu( 'Report', 'report', 'multiple_login')
67 = add_menu( 'Report', 'report', 'multiple_login')
68 - if (ungraded = Submission.where('graded_at is null').where('submitted_at < ?', 1.minutes.ago).count) > 0
68 - if (ungraded = Submission.where('graded_at is null').where('submitted_at < ?', 1.minutes.ago).count) > 0
69 =link_to "#{ungraded} backlogs!",
69 =link_to "#{ungraded} backlogs!",
70 grader_list_path,
70 grader_list_path,
71 class: 'navbar-btn btn btn-default btn-warning', data: {toggle: 'tooltip'},title: 'Number of ungraded submission'
71 class: 'navbar-btn btn btn-default btn-warning', data: {toggle: 'tooltip'},title: 'Number of ungraded submission'
72
72
73 %ul.nav.navbar-nav.navbar-right
73 %ul.nav.navbar-nav.navbar-right
74 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-question-sign')}".html_safe, 'main', 'help')
74 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-question-sign')}".html_safe, 'main', 'help')
75 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-comment')}".html_safe, 'messages', 'index', {title: I18n.t('menu.messages'), data: {toggle: 'tooltip'}})
75 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-comment')}".html_safe, 'messages', 'index', {title: I18n.t('menu.messages'), data: {toggle: 'tooltip'}})
76 - if GraderConfiguration['system.user_setting_enabled']
76 - if GraderConfiguration['system.user_setting_enabled']
77 - = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-cog')}".html_safe, 'users', 'index', {title: I18n.t('menu.settings'), data: {toggle: 'tooltip'}})
77 + = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-cog', id: 'user_profile')}".html_safe, 'users', 'profile', {title: I18n.t('menu.settings'), data: {toggle: 'tooltip'}})
78 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-log-out')} #{@current_user.full_name}".html_safe, 'main', 'login', {title: I18n.t('menu.log_out'), data: {toggle: 'tooltip'}})
78 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-log-out')} #{@current_user.full_name}".html_safe, 'main', 'login', {title: I18n.t('menu.log_out'), data: {toggle: 'tooltip'}})
79
79
80 /
80 /
81 - if (@current_user!=nil) and (session[:admin])
81 - if (@current_user!=nil) and (session[:admin])
82 %nav.navbar.navbar-fixed-top.navbar-inverse.secondnavbar
82 %nav.navbar.navbar-fixed-top.navbar-inverse.secondnavbar
83 .container-fluid
83 .container-fluid
84 .collapse.navbar-collapse
84 .collapse.navbar-collapse
85 %ul.nav.navbar-nav
85 %ul.nav.navbar-nav
86 = add_menu( '[Announcements]', 'announcements', 'index')
86 = add_menu( '[Announcements]', 'announcements', 'index')
87 = add_menu( '[Msg console]', 'messages', 'console')
87 = add_menu( '[Msg console]', 'messages', 'console')
88 = add_menu( '[Problems]', 'problems', 'index')
88 = add_menu( '[Problems]', 'problems', 'index')
89 = add_menu( '[Users]', 'user_admin', 'index')
89 = add_menu( '[Users]', 'user_admin', 'index')
90 = add_menu( '[Results]', 'user_admin', 'user_stat')
90 = add_menu( '[Results]', 'user_admin', 'user_stat')
91 = add_menu( '[Report]', 'report', 'multiple_login')
91 = add_menu( '[Report]', 'report', 'multiple_login')
92 = add_menu( '[Graders]', 'graders', 'list')
92 = add_menu( '[Graders]', 'graders', 'list')
93 = add_menu( '[Contests]', 'contest_management', 'index')
93 = add_menu( '[Contests]', 'contest_management', 'index')
94 = add_menu( '[Sites]', 'sites', 'index')
94 = add_menu( '[Sites]', 'sites', 'index')
95 = add_menu( '[System config]', 'configurations', 'index')
95 = add_menu( '[System config]', 'configurations', 'index')
@@ -1,14 +1,12
1 = simple_form_for(@user) do |f|
1 = simple_form_for(@user) do |f|
2 = f.error_notification
2 = f.error_notification
3 - .row
4 - .col-md-6.col-md-offset-2
5 = f.input :login, label: 'Login'
3 = f.input :login, label: 'Login'
6 = f.input :full_name, label: 'Full name'
4 = f.input :full_name, label: 'Full name'
7 = f.input :password
5 = f.input :password
8 = f.input :password_confirmation
6 = f.input :password_confirmation
9 = f.input :email
7 = f.input :email
10 = f.input :alias
8 = f.input :alias
11 = f.input :remark
9 = f.input :remark
12 - = f.button :submit, class: 'btn btn-success'
10 + = f.button :submit, class: 'btn btn-primary'
13 = link_to 'Cancel', :back, class: 'btn btn-default'
11 = link_to 'Cancel', :back, class: 'btn btn-default'
14
12
@@ -1,4 +1,9
1 + .container-fluid
2 + .row
3 + .col-md-6
1 %h1 Editing user
4 %h1 Editing user
5 + .row
6 + .col-md-6
2 = simple_form_for @user, url: user_admin_path(@user) do |f|
7 = simple_form_for @user, url: user_admin_path(@user) do |f|
3 = render partial: 'form', local: f
8 = render partial: 'form', local: f
4
9
@@ -1,36 +1,24
1 - = user_title_bar(@user)
2 -
3 - %h1 Your account settings
4 -
5 - -#%p
6 - -#You can edit your alias and e-mails. Just click on the text and edit it.
7
1
8 - %table.table.table-bordered{:style => "width:30%"}
2 + .container-fluid
9 - %tr
10 - %th Login
11 - %td= @user.login
12 - %tr
13 - %th Full name
14 - %td= @user.full_name
15 - -#%tr
16 - -#%th.uinfo Alias
17 - -#%td.uinfo= in_place_editor_field :user, 'alias_for_editing', {}, :rows => 1
18 - -#%tr
19 - -#%th.uinfo E-mail
20 - -#%td.uinfo= in_place_editor_field :user, 'email_for_editing', {}, :rows => 1
21 - %tr
22 - %th Password
23 - %td
24 = form_tag :action => 'chg_passwd', :method => 'post' do
3 = form_tag :action => 'chg_passwd', :method => 'post' do
25 - %table
4 + .row
26 - %tr
5 + .col-md-6
27 - %td
6 + %h1 Your account settings
28 - %input{:type => "password", :class => "form-control", :name => "passwd", :id => "passwd"}
7 + .form-group
29 - %td (new)
8 + %label{:for => "login"} Login
30 - %tr
9 + =@user.login
31 - %td
10 + .form-group
32 - %input{:type => "password", :class => "form-control", :name => "passwd_verify", :id => "passwd_verify"}
11 + %label{:for => "full_name"} Full name
33 - %td (verify)
12 + =@user.full_name
34 - %tr
13 + .form-group
35 - %td{:colspan => "2"}
14 + %label{:for => "password"} Password
36 - %input{:type => "button", :class => "btn btn-default", :name => "commit", :value => "Change Password"}
15 + =password_field_tag :password, nil, class: 'form-control'
16 + .form-group
17 + %label{:for => "password_confirmation"} Password confirmation
18 + =password_field_tag :password_confirmation, nil, class: 'form-control'
19 + .row
20 + .col-md-6
21 + =submit_tag 'Edit', class: 'btn btn-primary'
22 +
23 +
24 +
@@ -51,48 +51,52
51 delete 'remove_all_problem', to: 'groups#remove_all_problem', as: 'remove_all_problem'
51 delete 'remove_all_problem', to: 'groups#remove_all_problem', as: 'remove_all_problem'
52 end
52 end
53 collection do
53 collection do
54
54
55 end
55 end
56 end
56 end
57
57
58 resources :testcases, only: [] do
58 resources :testcases, only: [] do
59 member do
59 member do
60 get 'download_input'
60 get 'download_input'
61 get 'download_sol'
61 get 'download_sol'
62 end
62 end
63 collection do
63 collection do
64 get 'show_problem/:problem_id(/:test_num)' => 'testcases#show_problem', as: 'show_problem'
64 get 'show_problem/:problem_id(/:test_num)' => 'testcases#show_problem', as: 'show_problem'
65 end
65 end
66 end
66 end
67
67
68 resources :grader_configuration, controller: 'configurations'
68 resources :grader_configuration, controller: 'configurations'
69
69
70 resources :users do
70 resources :users do
71 member do
71 member do
72 get 'toggle_activate', 'toggle_enable'
72 get 'toggle_activate', 'toggle_enable'
73 get 'stat'
73 get 'stat'
74 end
74 end
75 + collection do
76 + get 'profile'
77 + post 'chg_passwd'
78 + end
75 end
79 end
76
80
77 resources :submissions do
81 resources :submissions do
78 member do
82 member do
79 get 'download'
83 get 'download'
80 get 'compiler_msg'
84 get 'compiler_msg'
81 get 'rejudge'
85 get 'rejudge'
82 get 'source'
86 get 'source'
83 end
87 end
84 collection do
88 collection do
85 get 'prob/:problem_id', to: 'submissions#index', as: 'problem'
89 get 'prob/:problem_id', to: 'submissions#index', as: 'problem'
86 get 'direct_edit_problem/:problem_id(/:user_id)', to: 'submissions#direct_edit_problem', as: 'direct_edit_problem'
90 get 'direct_edit_problem/:problem_id(/:user_id)', to: 'submissions#direct_edit_problem', as: 'direct_edit_problem'
87 get 'get_latest_submission_status/:uid/:pid', to: 'submissions#get_latest_submission_status', as: 'get_latest_submission_status'
91 get 'get_latest_submission_status/:uid/:pid', to: 'submissions#get_latest_submission_status', as: 'get_latest_submission_status'
88 end
92 end
89 end
93 end
90
94
91
95
92 #user admin
96 #user admin
93 resources :user_admin do
97 resources :user_admin do
94 collection do
98 collection do
95 match 'bulk_manage', via: [:get, :post]
99 match 'bulk_manage', via: [:get, :post]
96 get 'bulk_mail'
100 get 'bulk_mail'
97 get 'user_stat'
101 get 'user_stat'
98 get 'import'
102 get 'import'
@@ -72,31 +72,50
72 within 'header' do
72 within 'header' do
73 click_on 'Manage'
73 click_on 'Manage'
74 click_on 'Problem', match: :first
74 click_on 'Problem', match: :first
75 end
75 end
76 assert_text "Turn off all problems"
76 assert_text "Turn off all problems"
77 end
77 end
78
78
79 test "try using admin from normal user" do
79 test "try using admin from normal user" do
80 login 'admin','admin'
80 login 'admin','admin'
81 visit bulk_manage_user_admin_index_path
81 visit bulk_manage_user_admin_index_path
82 assert_current_path bulk_manage_user_admin_index_path
82 assert_current_path bulk_manage_user_admin_index_path
83 visit logout_main_path
83 visit logout_main_path
84
84
85 login 'jack','morning'
85 login 'jack','morning'
86 visit bulk_manage_user_admin_index_path
86 visit bulk_manage_user_admin_index_path
87 assert_text 'You are not authorized'
87 assert_text 'You are not authorized'
88 assert_current_path login_main_path
88 assert_current_path login_main_path
89
89
90 login 'james','morning'
90 login 'james','morning'
91 visit new_list_user_admin_index_path
91 visit new_list_user_admin_index_path
92 assert_text 'You are not authorized'
92 assert_text 'You are not authorized'
93 assert_current_path login_main_path
93 assert_current_path login_main_path
94 end
94 end
95
95
96 + test "login then change password" do
97 + newpassword = '1234asdf'
98 + login 'john', 'hello'
99 + visit profile_users_path
100 +
101 + fill_in 'password', with: newpassword
102 + fill_in 'password_confirmation', with: newpassword
103 +
104 + click_on 'Edit'
105 +
106 + visit logout_main_path
107 + login 'john', 'hello'
108 + assert_text 'Wrong password'
109 +
110 + login 'john', newpassword
111 + assert_text "MAIN"
112 + assert_text "Submission"
113 + end
114 +
96 def login(username,password)
115 def login(username,password)
97 visit root_path
116 visit root_path
98 fill_in "Login", with: username
117 fill_in "Login", with: username
99 fill_in "Password", with: password
118 fill_in "Password", with: password
100 click_on "Login"
119 click_on "Login"
101 end
120 end
102 end
121 end
You need to be logged in to leave comments. Login now