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: 76 inserted, 55 deleted

@@ -1,90 +1,98
1 1 require 'net/smtp'
2 2
3 3 class UsersController < ApplicationController
4 4
5 5 include MailHelperMethods
6 6
7 7 before_action :check_valid_login, :except => [:new,
8 8 :register,
9 9 :confirm,
10 10 :forget,
11 11 :retrieve_password]
12 12
13 13 before_action :verify_online_registration, :only => [:new,
14 14 :register,
15 15 :forget,
16 16 :retrieve_password]
17 - before_action :check_valid_login, :profile_authorization, only: [:profile]
18 17
19 18 before_action :admin_authorization, only: [:stat, :toggle_activate, :toggle_enable]
20 19
21 20
22 21 #in_place_edit_for :user, :alias_for_editing
23 22 #in_place_edit_for :user, :email_for_editing
24 23
25 24 def index
26 25 if !GraderConfiguration['system.user_setting_enabled']
27 26 redirect_to :controller => 'main', :action => 'list'
28 27 else
29 28 @user = User.find(session[:user_id])
30 29 end
31 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 41 def chg_passwd
34 42 user = User.find(session[:user_id])
35 - user.password = params[:passwd]
36 - user.password_confirmation = params[:passwd_verify]
43 + user.password = params[:password]
44 + user.password_confirmation = params[:password_confirmation]
37 45 if user.save
38 46 flash[:notice] = 'password changed'
39 47 else
40 48 flash[:notice] = 'Error: password changing failed'
41 49 end
42 - redirect_to :action => 'index'
50 + redirect_to :action => 'profile'
43 51 end
44 52
45 53 def new
46 54 @user = User.new
47 55 render :action => 'new', :layout => 'empty'
48 56 end
49 57
50 58 def register
51 59 if(params[:cancel])
52 60 redirect_to :controller => 'main', :action => 'login'
53 61 return
54 62 end
55 63 @user = User.new(user_params)
56 64 @user.password_confirmation = @user.password = User.random_password
57 65 @user.activated = false
58 66 if (@user.valid?) and (@user.save)
59 67 if send_confirmation_email(@user)
60 68 render :action => 'new_splash', :layout => 'empty'
61 69 else
62 70 @admin_email = GraderConfiguration['system.admin_email']
63 71 render :action => 'email_error', :layout => 'empty'
64 72 end
65 73 else
66 74 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
67 75 render :action => 'new', :layout => 'empty'
68 76 end
69 77 end
70 78
71 79 def confirm
72 80 login = params[:login]
73 81 key = params[:activation]
74 82 @user = User.find_by_login(login)
75 83 if (@user) and (@user.verify_activation_key(key))
76 84 if @user.valid? # check uniquenss of email
77 85 @user.activated = true
78 86 @user.save
79 87 @result = :successful
80 88 else
81 89 @result = :email_used
82 90 end
83 91 else
84 92 @result = :failed
85 93 end
86 94 render :action => 'confirm', :layout => 'empty'
87 95 end
88 96
89 97 def forget
90 98 render :action => 'forget', :layout => 'empty'
@@ -170,50 +178,49
170 178 mail_body = t('registration.email_body', {
171 179 :full_name => user.full_name,
172 180 :contest_name => contest_name,
173 181 :login => user.login,
174 182 :password => user.password,
175 183 :activation_url => activation_url,
176 184 :admin_email => GraderConfiguration['system.admin_email']
177 185 })
178 186
179 187 logger.info mail_body
180 188
181 189 send_mail(user.email, mail_subject, mail_body)
182 190 end
183 191
184 192 def send_new_password_email(user)
185 193 contest_name = GraderConfiguration['contest.name']
186 194 mail_subject = "[#{contest_name}] Password recovery"
187 195 mail_body = t('registration.password_retrieval.email_body', {
188 196 :full_name => user.full_name,
189 197 :contest_name => contest_name,
190 198 :login => user.login,
191 199 :password => user.password,
192 200 :admin_email => GraderConfiguration['system.admin_email']
193 201 })
194 202
195 203 logger.info mail_body
196 204
197 205 send_mail(user.email, mail_subject, mail_body)
198 206 end
199 207
200 208 # allow viewing of regular user profile only when options allow so
201 209 # only admins can view admins profile
202 210 def profile_authorization
203 211 #if view admins' profile, allow only admin
204 212 return false unless(params[:id])
205 213 user = User.find(params[:id])
206 214 return false unless user
207 215 return admin_authorization if user.admin?
208 216 return true if GraderConfiguration["right.user_view_submission"]
209 217
210 218 #finally, we allow only admin
211 219 admin_authorization
212 220 end
213 221
214 222 private
215 223 def user_params
216 224 params.require(:user).permit(:login, :full_name, :email)
217 225 end
218 -
219 226 end
@@ -29,67 +29,67
29 29 %div.navbar-btn.btn.btn-success#countdown= "ANALYSIS MODE"
30 30 - elsif GraderConfiguration.time_limit_mode?
31 31 - if @current_user.contest_finished?
32 32 %div.navbar-btn.btn.btn-danger#countdown= "Contest is over"
33 33 - elsif !@current_user.contest_started?
34 34 %div.navbar-btn.btn.btn-primary#countdown= (t 'title_bar.contest_not_started')
35 35 - else
36 36 %div.navbar-btn.btn.btn-primary#countdown asdf
37 37 :javascript
38 38 $("#countdown").countdown({until: "+#{@current_user.contest_time_left.to_i}s", layout: 'Time left: {hnn}:{mnn}:{snn}'});
39 39 / admin section
40 40 - if (@current_user!=nil) and (session[:admin])
41 41 / management
42 42 %li.dropdown
43 43 %a.dropdown-toggle{href: '#', data: {toggle:'dropdown'}, aria: {haspopup:"true", expanded:"false"}, role: "button"}
44 44 Manage
45 45 %span.caret
46 46 %ul.dropdown-menu
47 47 = add_menu( 'Announcements', 'announcements', 'index')
48 48 = add_menu( 'Problems', 'problems', 'index')
49 49 = add_menu( 'Tags', 'tags', 'index')
50 50 = add_menu( 'Users', 'user_admin', 'index')
51 51 = add_menu( 'User Groups', 'groups', 'index')
52 52 = add_menu( 'Graders', 'graders', 'list')
53 53 = add_menu( 'Message ', 'messages', 'console')
54 54 %li.divider{role: 'separator'}
55 55 = add_menu( 'System config', 'configurations', 'index')
56 56 %li.divider{role: 'separator'}
57 57 = add_menu( 'Sites', 'sites', 'index')
58 58 = add_menu( 'Contests', 'contest_management', 'index')
59 59 / report
60 60 %li.dropdown
61 61 %a.dropdown-toggle{href: '#', data: {toggle:'dropdown'}, aria: {haspopup:"true", expanded:"false"}, role: "button"}
62 62 Report
63 63 %span.caret
64 64 %ul.dropdown-menu
65 65 = add_menu( 'Current Score', 'report', 'current_score')
66 66 = add_menu( 'Score Report', 'report', 'max_score')
67 67 = add_menu( 'Report', 'report', 'multiple_login')
68 68 - if (ungraded = Submission.where('graded_at is null').where('submitted_at < ?', 1.minutes.ago).count) > 0
69 69 =link_to "#{ungraded} backlogs!",
70 70 grader_list_path,
71 71 class: 'navbar-btn btn btn-default btn-warning', data: {toggle: 'tooltip'},title: 'Number of ungraded submission'
72 72
73 73 %ul.nav.navbar-nav.navbar-right
74 74 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-question-sign')}".html_safe, 'main', 'help')
75 75 = add_menu("#{content_tag(:span,'',class: 'glyphicon glyphicon-comment')}".html_safe, 'messages', 'index', {title: I18n.t('menu.messages'), data: {toggle: 'tooltip'}})
76 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 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 81 - if (@current_user!=nil) and (session[:admin])
82 82 %nav.navbar.navbar-fixed-top.navbar-inverse.secondnavbar
83 83 .container-fluid
84 84 .collapse.navbar-collapse
85 85 %ul.nav.navbar-nav
86 86 = add_menu( '[Announcements]', 'announcements', 'index')
87 87 = add_menu( '[Msg console]', 'messages', 'console')
88 88 = add_menu( '[Problems]', 'problems', 'index')
89 89 = add_menu( '[Users]', 'user_admin', 'index')
90 90 = add_menu( '[Results]', 'user_admin', 'user_stat')
91 91 = add_menu( '[Report]', 'report', 'multiple_login')
92 92 = add_menu( '[Graders]', 'graders', 'list')
93 93 = add_menu( '[Contests]', 'contest_management', 'index')
94 94 = add_menu( '[Sites]', 'sites', 'index')
95 95 = add_menu( '[System config]', 'configurations', 'index')
@@ -1,14 +1,12
1 1 = simple_form_for(@user) do |f|
2 2 = f.error_notification
3 - .row
4 - .col-md-6.col-md-offset-2
5 - = f.input :login, label: 'Login'
6 - = f.input :full_name, label: 'Full name'
7 - = f.input :password
8 - = f.input :password_confirmation
9 - = f.input :email
10 - = f.input :alias
11 - = f.input :remark
12 - = f.button :submit, class: 'btn btn-success'
13 - = link_to 'Cancel', :back, class: 'btn btn-default'
3 + = f.input :login, label: 'Login'
4 + = f.input :full_name, label: 'Full name'
5 + = f.input :password
6 + = f.input :password_confirmation
7 + = f.input :email
8 + = f.input :alias
9 + = f.input :remark
10 + = f.button :submit, class: 'btn btn-primary'
11 + = link_to 'Cancel', :back, class: 'btn btn-default'
14 12
@@ -1,4 +1,9
1 - %h1 Editing user
2 - = simple_form_for @user, url: user_admin_path(@user) do |f|
3 - = render partial: 'form', local: f
1 + .container-fluid
2 + .row
3 + .col-md-6
4 + %h1 Editing user
5 + .row
6 + .col-md-6
7 + = simple_form_for @user, url: user_admin_path(@user) do |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%"}
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
25 - %table
26 - %tr
27 - %td
28 - %input{:type => "password", :class => "form-control", :name => "passwd", :id => "passwd"}
29 - %td (new)
30 - %tr
31 - %td
32 - %input{:type => "password", :class => "form-control", :name => "passwd_verify", :id => "passwd_verify"}
33 - %td (verify)
34 - %tr
35 - %td{:colspan => "2"}
36 - %input{:type => "button", :class => "btn btn-default", :name => "commit", :value => "Change Password"}
2 + .container-fluid
3 + = form_tag :action => 'chg_passwd', :method => 'post' do
4 + .row
5 + .col-md-6
6 + %h1 Your account settings
7 + .form-group
8 + %label{:for => "login"} Login
9 + =@user.login
10 + .form-group
11 + %label{:for => "full_name"} Full name
12 + =@user.full_name
13 + .form-group
14 + %label{:for => "password"} 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 +
@@ -27,96 +27,100
27 27 member do
28 28 get 'toggle'
29 29 get 'toggle_test'
30 30 get 'toggle_view_testcase'
31 31 get 'stat'
32 32 end
33 33 collection do
34 34 get 'turn_all_off'
35 35 get 'turn_all_on'
36 36 get 'import'
37 37 get 'manage'
38 38 get 'quick_create'
39 39 post 'do_manage'
40 40 post 'do_import'
41 41 end
42 42 end
43 43
44 44 resources :groups do
45 45 member do
46 46 post 'add_user', to: 'groups#add_user', as: 'add_user'
47 47 delete 'remove_user/:user_id', to: 'groups#remove_user', as: 'remove_user'
48 48 delete 'remove_all_user', to: 'groups#remove_all_user', as: 'remove_all_user'
49 49 post 'add_problem', to: 'groups#add_problem', as: 'add_problem'
50 50 delete 'remove_problem/:problem_id', to: 'groups#remove_problem', as: 'remove_problem'
51 51 delete 'remove_all_problem', to: 'groups#remove_all_problem', as: 'remove_all_problem'
52 52 end
53 53 collection do
54 54
55 55 end
56 56 end
57 57
58 58 resources :testcases, only: [] do
59 59 member do
60 60 get 'download_input'
61 61 get 'download_sol'
62 62 end
63 63 collection do
64 64 get 'show_problem/:problem_id(/:test_num)' => 'testcases#show_problem', as: 'show_problem'
65 65 end
66 66 end
67 67
68 68 resources :grader_configuration, controller: 'configurations'
69 69
70 70 resources :users do
71 71 member do
72 72 get 'toggle_activate', 'toggle_enable'
73 73 get 'stat'
74 74 end
75 + collection do
76 + get 'profile'
77 + post 'chg_passwd'
78 + end
75 79 end
76 80
77 81 resources :submissions do
78 82 member do
79 83 get 'download'
80 84 get 'compiler_msg'
81 85 get 'rejudge'
82 86 get 'source'
83 87 end
84 88 collection do
85 89 get 'prob/:problem_id', to: 'submissions#index', as: 'problem'
86 90 get 'direct_edit_problem/:problem_id(/:user_id)', to: 'submissions#direct_edit_problem', as: 'direct_edit_problem'
87 91 get 'get_latest_submission_status/:uid/:pid', to: 'submissions#get_latest_submission_status', as: 'get_latest_submission_status'
88 92 end
89 93 end
90 94
91 95
92 96 #user admin
93 97 resources :user_admin do
94 98 collection do
95 99 match 'bulk_manage', via: [:get, :post]
96 100 get 'bulk_mail'
97 101 get 'user_stat'
98 102 get 'import'
99 103 get 'new_list'
100 104 get 'admin'
101 105 get 'active'
102 106 get 'mass_mailing'
103 107 get 'revoke_admin'
104 108 post 'grant_admin'
105 109 match 'create_from_list', via: [:get, :post]
106 110 match 'random_all_passwords', via: [:get, :post]
107 111 end
108 112 member do
109 113 get 'clear_last_ip'
110 114 end
111 115 end
112 116
113 117 resources :contest_management, only: [:index] do
114 118 collection do
115 119 get 'user_stat'
116 120 get 'clear_stat'
117 121 get 'clear_all_stat'
118 122 get 'change_contest_mode'
119 123 end
120 124 end
121 125
122 126 #get 'user_admin', to: 'user_admin#index'
@@ -48,55 +48,74
48 48 click_on 'Users', match: :first
49 49 end
50 50
51 51 click_on 'New list of users', match: :first
52 52 find(:css, 'textarea').fill_in with:"abc1,Boaty McBoatface,abcdef,alias1,remark1,\nabc2,Boaty2 McSecond,acbdef123,aias2,remark2"
53 53 click_on 'create users'
54 54
55 55 assert_text('remark1')
56 56 assert_text('remark2')
57 57 end
58 58
59 59 test "grant admin right" do
60 60 login 'admin', 'admin'
61 61 within 'header' do
62 62 click_on 'Manage'
63 63 click_on 'Users', match: :first
64 64 end
65 65
66 66 click_on "View administrator"
67 67 fill_in 'login', with: 'john'
68 68 click_on "Grant"
69 69
70 70 visit logout_main_path
71 71 login 'john','hello'
72 72 within 'header' do
73 73 click_on 'Manage'
74 74 click_on 'Problem', match: :first
75 75 end
76 76 assert_text "Turn off all problems"
77 77 end
78 78
79 79 test "try using admin from normal user" do
80 80 login 'admin','admin'
81 81 visit bulk_manage_user_admin_index_path
82 82 assert_current_path bulk_manage_user_admin_index_path
83 83 visit logout_main_path
84 84
85 85 login 'jack','morning'
86 86 visit bulk_manage_user_admin_index_path
87 87 assert_text 'You are not authorized'
88 88 assert_current_path login_main_path
89 89
90 90 login 'james','morning'
91 91 visit new_list_user_admin_index_path
92 92 assert_text 'You are not authorized'
93 93 assert_current_path login_main_path
94 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 115 def login(username,password)
97 116 visit root_path
98 117 fill_in "Login", with: username
99 118 fill_in "Password", with: password
100 119 click_on "Login"
101 120 end
102 121 end
You need to be logged in to leave comments. Login now