Description:
force log out when password change
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r884:490fbe2ccf0b - - 2 files changed: 6 inserted, 0 deleted
@@ -21,192 +21,197 | |||||
|
21 |
|
21 | ||
|
22 | # Returns the current logged-in user (if any). |
|
22 | # Returns the current logged-in user (if any). |
|
23 | def current_user |
|
23 | def current_user |
|
24 | return nil unless session[:user_id] |
|
24 | return nil unless session[:user_id] |
|
25 | @current_user ||= User.find(session[:user_id]) |
|
25 | @current_user ||= User.find(session[:user_id]) |
|
26 | end |
|
26 | end |
|
27 |
|
27 | ||
|
28 | def nav_announcement |
|
28 | def nav_announcement |
|
29 | @nav_announcement = Announcement.where(on_nav_bar: true) |
|
29 | @nav_announcement = Announcement.where(on_nav_bar: true) |
|
30 | end |
|
30 | end |
|
31 |
|
31 | ||
|
32 | def admin_authorization |
|
32 | def admin_authorization |
|
33 | return false unless check_valid_login |
|
33 | return false unless check_valid_login |
|
34 | user = User.includes(:roles).find(session[:user_id]) |
|
34 | user = User.includes(:roles).find(session[:user_id]) |
|
35 | unless user.admin? |
|
35 | unless user.admin? |
|
36 | unauthorized_redirect |
|
36 | unauthorized_redirect |
|
37 | return false |
|
37 | return false |
|
38 | end |
|
38 | end |
|
39 | return true |
|
39 | return true |
|
40 | end |
|
40 | end |
|
41 |
|
41 | ||
|
42 | #admin always count as every roles |
|
42 | #admin always count as every roles |
|
43 | def role_authorization(roles) |
|
43 | def role_authorization(roles) |
|
44 | return false unless check_valid_login |
|
44 | return false unless check_valid_login |
|
45 | user = User.find(session[:user_id]) |
|
45 | user = User.find(session[:user_id]) |
|
46 | return true if user.admin? |
|
46 | return true if user.admin? |
|
47 | roles.each do |r| |
|
47 | roles.each do |r| |
|
48 | return true if user.has_role?(r) |
|
48 | return true if user.has_role?(r) |
|
49 | end |
|
49 | end |
|
50 | unauthorized_redirect |
|
50 | unauthorized_redirect |
|
51 | end |
|
51 | end |
|
52 |
|
52 | ||
|
53 | def authorization_by_roles(allowed_roles) |
|
53 | def authorization_by_roles(allowed_roles) |
|
54 | return false unless check_valid_login |
|
54 | return false unless check_valid_login |
|
55 | unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
55 | unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
56 | unauthorized_redirect |
|
56 | unauthorized_redirect |
|
57 | return false |
|
57 | return false |
|
58 | end |
|
58 | end |
|
59 | end |
|
59 | end |
|
60 |
|
60 | ||
|
61 | def testcase_authorization |
|
61 | def testcase_authorization |
|
62 | #admin always has privileged |
|
62 | #admin always has privileged |
|
63 | if @current_user.admin? |
|
63 | if @current_user.admin? |
|
64 | return true |
|
64 | return true |
|
65 | end |
|
65 | end |
|
66 |
|
66 | ||
|
67 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
67 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
68 | end |
|
68 | end |
|
69 |
|
69 | ||
|
70 | def unique_visitor_id |
|
70 | def unique_visitor_id |
|
71 | unless cookies.encrypted[:uuid] |
|
71 | unless cookies.encrypted[:uuid] |
|
72 | value = SecureRandom.uuid |
|
72 | value = SecureRandom.uuid |
|
73 | cookies.encrypted[:uuid] = { value: value, expires: 20.year } |
|
73 | cookies.encrypted[:uuid] = { value: value, expires: 20.year } |
|
74 | return value |
|
74 | return value |
|
75 | else |
|
75 | else |
|
76 | return cookies.encrypted[:uuid] |
|
76 | return cookies.encrypted[:uuid] |
|
77 | end |
|
77 | end |
|
78 | end |
|
78 | end |
|
79 |
|
79 | ||
|
80 | protected |
|
80 | protected |
|
81 |
|
81 | ||
|
82 | #redirect to root (and also force logout) |
|
82 | #redirect to root (and also force logout) |
|
83 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
83 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
84 | def check_valid_login |
|
84 | def check_valid_login |
|
85 | #check if logged in |
|
85 | #check if logged in |
|
86 | unless session[:user_id] |
|
86 | unless session[:user_id] |
|
87 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
87 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
88 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
88 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
89 | else |
|
89 | else |
|
90 | unauthorized_redirect('You need to login') |
|
90 | unauthorized_redirect('You need to login') |
|
91 | end |
|
91 | end |
|
92 | return false |
|
92 | return false |
|
93 | end |
|
93 | end |
|
94 |
|
94 | ||
|
95 | # check if run in single user mode |
|
95 | # check if run in single user mode |
|
96 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
96 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
97 | if @current_user==nil || (!@current_user.admin?) |
|
97 | if @current_user==nil || (!@current_user.admin?) |
|
98 | unauthorized_redirect('You cannot log in at this time') |
|
98 | unauthorized_redirect('You cannot log in at this time') |
|
99 | return false |
|
99 | return false |
|
100 | end |
|
100 | end |
|
101 | end |
|
101 | end |
|
102 |
|
102 | ||
|
103 | # check if the user is enabled |
|
103 | # check if the user is enabled |
|
104 | unless @current_user.enabled? || @current_user.admin? |
|
104 | unless @current_user.enabled? || @current_user.admin? |
|
105 | unauthorized_redirect 'Your account is disabled' |
|
105 | unauthorized_redirect 'Your account is disabled' |
|
106 | return false |
|
106 | return false |
|
107 | end |
|
107 | end |
|
108 |
|
108 | ||
|
109 | # check if user ip is allowed |
|
109 | # check if user ip is allowed |
|
110 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
110 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
111 | unless is_request_ip_allowed? |
|
111 | unless is_request_ip_allowed? |
|
112 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
112 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
113 | return false |
|
113 | return false |
|
114 | end |
|
114 | end |
|
115 | end |
|
115 | end |
|
116 |
|
116 | ||
|
|
117 | + # check if password is changed | ||
|
|
118 | + if session[:last_password_hash] != @current_user.hashed_password | ||
|
|
119 | + unauthorized_redirect 'You are forced to log out.' | ||
|
|
120 | + end | ||
|
|
121 | + | ||
|
117 | if GraderConfiguration.multicontests? |
|
122 | if GraderConfiguration.multicontests? |
|
118 | return true if @current_user.admin? |
|
123 | return true if @current_user.admin? |
|
119 | begin |
|
124 | begin |
|
120 | if @current_user.contest_stat(true).forced_logout |
|
125 | if @current_user.contest_stat(true).forced_logout |
|
121 | flash[:notice] = 'You have been automatically logged out.' |
|
126 | flash[:notice] = 'You have been automatically logged out.' |
|
122 | redirect_to :controller => 'main', :action => 'index' |
|
127 | redirect_to :controller => 'main', :action => 'index' |
|
123 | end |
|
128 | end |
|
124 | rescue |
|
129 | rescue |
|
125 | end |
|
130 | end |
|
126 | end |
|
131 | end |
|
127 | return true |
|
132 | return true |
|
128 | end |
|
133 | end |
|
129 |
|
134 | ||
|
130 | #redirect to root (and also force logout) |
|
135 | #redirect to root (and also force logout) |
|
131 | #if the user use different ip from the previous connection |
|
136 | #if the user use different ip from the previous connection |
|
132 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
137 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
133 | def authenticate_by_ip_address |
|
138 | def authenticate_by_ip_address |
|
134 | #this assume that we have already authenticate normally |
|
139 | #this assume that we have already authenticate normally |
|
135 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
140 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
136 | user = User.find(session[:user_id]) |
|
141 | user = User.find(session[:user_id]) |
|
137 | if (!user.admin? && user.last_ip && user.last_ip != unique_visitor_id) |
|
142 | if (!user.admin? && user.last_ip && user.last_ip != unique_visitor_id) |
|
138 | flash[:notice] = "You cannot use the system from two different places" |
|
143 | flash[:notice] = "You cannot use the system from two different places" |
|
139 | redirect_to :controller => 'main', :action => 'login' |
|
144 | redirect_to :controller => 'main', :action => 'login' |
|
140 | return false |
|
145 | return false |
|
141 | end |
|
146 | end |
|
142 | unless user.last_ip |
|
147 | unless user.last_ip |
|
143 | user.last_ip = unique_visitor_id |
|
148 | user.last_ip = unique_visitor_id |
|
144 | user.save |
|
149 | user.save |
|
145 | end |
|
150 | end |
|
146 | end |
|
151 | end |
|
147 | return true |
|
152 | return true |
|
148 | end |
|
153 | end |
|
149 |
|
154 | ||
|
150 | def authorization |
|
155 | def authorization |
|
151 | return false unless check_valid_login |
|
156 | return false unless check_valid_login |
|
152 | user = User.find(session[:user_id]) |
|
157 | user = User.find(session[:user_id]) |
|
153 | unless user.roles.detect { |role| |
|
158 | unless user.roles.detect { |role| |
|
154 | role.rights.detect{ |right| |
|
159 | role.rights.detect{ |right| |
|
155 | right.controller == self.class.controller_name and |
|
160 | right.controller == self.class.controller_name and |
|
156 | (right.action == 'all' || right.action == action_name) |
|
161 | (right.action == 'all' || right.action == action_name) |
|
157 | } |
|
162 | } |
|
158 | } |
|
163 | } |
|
159 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
164 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
160 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
165 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
161 | redirect_to :controller => 'main', :action => 'login' |
|
166 | redirect_to :controller => 'main', :action => 'login' |
|
162 | return false |
|
167 | return false |
|
163 | end |
|
168 | end |
|
164 | end |
|
169 | end |
|
165 |
|
170 | ||
|
166 | def verify_time_limit |
|
171 | def verify_time_limit |
|
167 | return true if session[:user_id]==nil |
|
172 | return true if session[:user_id]==nil |
|
168 | user = User.find(session[:user_id], :include => :site) |
|
173 | user = User.find(session[:user_id], :include => :site) |
|
169 | return true if user==nil || user.site == nil |
|
174 | return true if user==nil || user.site == nil |
|
170 | if user.contest_finished? |
|
175 | if user.contest_finished? |
|
171 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
176 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
172 | redirect_to :back |
|
177 | redirect_to :back |
|
173 | return false |
|
178 | return false |
|
174 | end |
|
179 | end |
|
175 | return true |
|
180 | return true |
|
176 | end |
|
181 | end |
|
177 |
|
182 | ||
|
178 | def is_request_ip_allowed? |
|
183 | def is_request_ip_allowed? |
|
179 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
184 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
180 | user_ip = IPAddr.new(request.remote_ip) |
|
185 | user_ip = IPAddr.new(request.remote_ip) |
|
181 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
186 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
182 |
|
187 | ||
|
183 | allowed.delete(' ').split(',').each do |ips| |
|
188 | allowed.delete(' ').split(',').each do |ips| |
|
184 | allow_ips = IPAddr.new(ips) |
|
189 | allow_ips = IPAddr.new(ips) |
|
185 | if allow_ips.include?(user_ip) |
|
190 | if allow_ips.include?(user_ip) |
|
186 | return true |
|
191 | return true |
|
187 | end |
|
192 | end |
|
188 | end |
|
193 | end |
|
189 | return false |
|
194 | return false |
|
190 | end |
|
195 | end |
|
191 | return true |
|
196 | return true |
|
192 | end |
|
197 | end |
|
193 |
|
198 | ||
|
194 | #function for datatable ajax query |
|
199 | #function for datatable ajax query |
|
195 | #return record,total_count,filter_count |
|
200 | #return record,total_count,filter_count |
|
196 | def process_query_record(record, |
|
201 | def process_query_record(record, |
|
197 | total_count: nil, |
|
202 | total_count: nil, |
|
198 | select: '', |
|
203 | select: '', |
|
199 | global_search: [], |
|
204 | global_search: [], |
|
200 | no_search: false, |
|
205 | no_search: false, |
|
201 | force_order: '', |
|
206 | force_order: '', |
|
202 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
207 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
203 | hard_limit: nil) |
|
208 | hard_limit: nil) |
|
204 | arel_table = record.model.arel_table |
|
209 | arel_table = record.model.arel_table |
|
205 |
|
210 | ||
|
206 | if !no_search && params['search'] |
|
211 | if !no_search && params['search'] |
|
207 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
212 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
208 | if !global_value.blank? |
|
213 | if !global_value.blank? |
|
209 | global_value.split.each do |value| |
|
214 | global_value.split.each do |value| |
|
210 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
215 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
211 | record = record.where(global_where) |
|
216 | record = record.where(global_where) |
|
212 | end |
|
217 | end |
@@ -1,99 +1,100 | |||||
|
1 | class LoginController < ApplicationController |
|
1 | class LoginController < ApplicationController |
|
2 |
|
2 | ||
|
3 | @@authenticators = [] |
|
3 | @@authenticators = [] |
|
4 |
|
4 | ||
|
5 | def index |
|
5 | def index |
|
6 | # show login screen |
|
6 | # show login screen |
|
7 | reset_session |
|
7 | reset_session |
|
8 | redirect_to :controller => 'main', :action => 'login' |
|
8 | redirect_to :controller => 'main', :action => 'login' |
|
9 | end |
|
9 | end |
|
10 |
|
10 | ||
|
11 | def login |
|
11 | def login |
|
12 | user = get_authenticated_user(params[:login], params[:password]) |
|
12 | user = get_authenticated_user(params[:login], params[:password]) |
|
13 | unless user |
|
13 | unless user |
|
14 | flash[:notice] = 'Wrong password' |
|
14 | flash[:notice] = 'Wrong password' |
|
15 | redirect_to :controller => 'main', :action => 'login' |
|
15 | redirect_to :controller => 'main', :action => 'login' |
|
16 | return |
|
16 | return |
|
17 | end |
|
17 | end |
|
18 |
|
18 | ||
|
19 | if (!GraderConfiguration['right.bypass_agreement']) and (!params[:accept_agree]) and !user.admin? |
|
19 | if (!GraderConfiguration['right.bypass_agreement']) and (!params[:accept_agree]) and !user.admin? |
|
20 | flash[:notice] = 'You must accept the agreement before logging in' |
|
20 | flash[:notice] = 'You must accept the agreement before logging in' |
|
21 | redirect_to :controller => 'main', :action => 'login' |
|
21 | redirect_to :controller => 'main', :action => 'login' |
|
22 | return |
|
22 | return |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
25 | #store uuid when login |
|
25 | #store uuid when login |
|
26 | if user.last_ip.nil? |
|
26 | if user.last_ip.nil? |
|
27 | user.last_ip = cookies.encrypted[:uuid] |
|
27 | user.last_ip = cookies.encrypted[:uuid] |
|
28 | else |
|
28 | else |
|
29 | if user.last_ip != cookies.encrypted[:uuid] |
|
29 | if user.last_ip != cookies.encrypted[:uuid] |
|
30 | user.last_ip =cookies.encrypted[:uuid] |
|
30 | user.last_ip =cookies.encrypted[:uuid] |
|
31 | #log different login |
|
31 | #log different login |
|
32 | end |
|
32 | end |
|
33 | end |
|
33 | end |
|
34 |
|
34 | ||
|
35 | #process logging in |
|
35 | #process logging in |
|
36 | session[:user_id] = user.id |
|
36 | session[:user_id] = user.id |
|
|
37 | + session[:last_password_hash] = user.hashed_password | ||
|
37 | session[:admin] = user.admin? |
|
38 | session[:admin] = user.admin? |
|
38 |
|
39 | ||
|
39 | # clear forced logout flag for multicontests contest change |
|
40 | # clear forced logout flag for multicontests contest change |
|
40 | if GraderConfiguration.multicontests? |
|
41 | if GraderConfiguration.multicontests? |
|
41 | contest_stat = user.contest_stat |
|
42 | contest_stat = user.contest_stat |
|
42 | if contest_stat.respond_to? :forced_logout |
|
43 | if contest_stat.respond_to? :forced_logout |
|
43 | if contest_stat.forced_logout |
|
44 | if contest_stat.forced_logout |
|
44 | contest_stat.forced_logout = false |
|
45 | contest_stat.forced_logout = false |
|
45 | contest_stat.save |
|
46 | contest_stat.save |
|
46 | end |
|
47 | end |
|
47 | end |
|
48 | end |
|
48 | end |
|
49 | end |
|
49 |
|
50 | ||
|
50 | #save login information |
|
51 | #save login information |
|
51 | Login.create(user_id: user.id, ip_address: cookies.encrypted[:uuid]) |
|
52 | Login.create(user_id: user.id, ip_address: cookies.encrypted[:uuid]) |
|
52 |
|
53 | ||
|
53 | redirect_to :controller => 'main', :action => 'list' |
|
54 | redirect_to :controller => 'main', :action => 'list' |
|
54 | end |
|
55 | end |
|
55 |
|
56 | ||
|
56 | def site_login |
|
57 | def site_login |
|
57 | begin |
|
58 | begin |
|
58 | site = Site.find(params[:login][:site_id]) |
|
59 | site = Site.find(params[:login][:site_id]) |
|
59 | rescue ActiveRecord::RecordNotFound |
|
60 | rescue ActiveRecord::RecordNotFound |
|
60 | site = nil |
|
61 | site = nil |
|
61 | end |
|
62 | end |
|
62 | if site==nil |
|
63 | if site==nil |
|
63 | flash[:notice] = 'Wrong site' |
|
64 | flash[:notice] = 'Wrong site' |
|
64 | redirect_to :controller => 'main', :action => 'login' and return |
|
65 | redirect_to :controller => 'main', :action => 'login' and return |
|
65 | end |
|
66 | end |
|
66 | if (site.password) and (site.password == params[:login][:password]) |
|
67 | if (site.password) and (site.password == params[:login][:password]) |
|
67 | session[:site_id] = site.id |
|
68 | session[:site_id] = site.id |
|
68 | redirect_to :controller => 'site', :action => 'index' |
|
69 | redirect_to :controller => 'site', :action => 'index' |
|
69 | else |
|
70 | else |
|
70 | flash[:notice] = 'Wrong site password' |
|
71 | flash[:notice] = 'Wrong site password' |
|
71 | redirect_to :controller => 'site', :action => 'login' |
|
72 | redirect_to :controller => 'site', :action => 'login' |
|
72 | end |
|
73 | end |
|
73 | end |
|
74 | end |
|
74 |
|
75 | ||
|
75 | def logout |
|
76 | def logout |
|
76 | redirect_to root_path |
|
77 | redirect_to root_path |
|
77 | end |
|
78 | end |
|
78 |
|
79 | ||
|
79 | def self.add_authenticator(authenticator) |
|
80 | def self.add_authenticator(authenticator) |
|
80 | @@authenticators << authenticator |
|
81 | @@authenticators << authenticator |
|
81 | end |
|
82 | end |
|
82 |
|
83 | ||
|
83 | protected |
|
84 | protected |
|
84 |
|
85 | ||
|
85 | def get_authenticated_user(login, password) |
|
86 | def get_authenticated_user(login, password) |
|
86 | if @@authenticators.empty? |
|
87 | if @@authenticators.empty? |
|
87 | return User.authenticate(login, password) |
|
88 | return User.authenticate(login, password) |
|
88 | else |
|
89 | else |
|
89 | user = User.authenticate(login, password) |
|
90 | user = User.authenticate(login, password) |
|
90 | @@authenticators.each do |authenticator| |
|
91 | @@authenticators.each do |authenticator| |
|
91 | if not user |
|
92 | if not user |
|
92 | user = authenticator.authenticate(login, password) |
|
93 | user = authenticator.authenticate(login, password) |
|
93 | end |
|
94 | end |
|
94 | end |
|
95 | end |
|
95 | return user |
|
96 | return user |
|
96 | end |
|
97 | end |
|
97 | end |
|
98 | end |
|
98 |
|
99 | ||
|
99 | end |
|
100 | end |
You need to be logged in to leave comments.
Login now