Description:
change to encrypted cookies
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r853:da84d756f1f9 - - 2 files changed: 8 inserted, 6 deleted
@@ -1,251 +1,253 | |||||
|
1 | require 'ipaddr' |
|
1 | require 'ipaddr' |
|
2 | require "securerandom" |
|
2 | require "securerandom" |
|
3 |
|
3 | ||
|
4 | class ApplicationController < ActionController::Base |
|
4 | class ApplicationController < ActionController::Base |
|
5 | protect_from_forgery |
|
5 | protect_from_forgery |
|
6 |
|
6 | ||
|
7 | before_action :current_user |
|
7 | before_action :current_user |
|
8 | before_action :nav_announcement |
|
8 | before_action :nav_announcement |
|
9 | before_action :unique_visitor_id |
|
9 | before_action :unique_visitor_id |
|
10 |
|
10 | ||
|
11 | SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' |
|
11 | SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' |
|
12 | MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' |
|
12 | MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' |
|
13 | WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore' |
|
13 | WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore' |
|
14 | WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' |
|
14 | WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' |
|
15 |
|
15 | ||
|
16 | #report and redirect for unauthorized activities |
|
16 | #report and redirect for unauthorized activities |
|
17 | def unauthorized_redirect(notice = 'You are not authorized to view the page you requested') |
|
17 | def unauthorized_redirect(notice = 'You are not authorized to view the page you requested') |
|
18 | flash[:notice] = notice |
|
18 | flash[:notice] = notice |
|
19 | redirect_to login_main_path |
|
19 | redirect_to login_main_path |
|
20 | end |
|
20 | end |
|
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 | def authorization_by_roles(allowed_roles) |
|
42 | def authorization_by_roles(allowed_roles) |
|
43 | return false unless check_valid_login |
|
43 | return false unless check_valid_login |
|
44 | unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
44 | unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
45 | unauthorized_redirect |
|
45 | unauthorized_redirect |
|
46 | return false |
|
46 | return false |
|
47 | end |
|
47 | end |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
|
50 | def testcase_authorization |
|
50 | def testcase_authorization |
|
51 | #admin always has privileged |
|
51 | #admin always has privileged |
|
52 | if @current_user.admin? |
|
52 | if @current_user.admin? |
|
53 | return true |
|
53 | return true |
|
54 | end |
|
54 | end |
|
55 |
|
55 | ||
|
56 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
56 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
57 | end |
|
57 | end |
|
58 |
|
58 | ||
|
59 | def unique_visitor_id |
|
59 | def unique_visitor_id |
|
60 | - unless cookies[:uuid] |
|
60 | + unless cookies.encrypted[:uuid] |
|
61 | value = SecureRandom.uuid |
|
61 | value = SecureRandom.uuid |
|
62 | - cookies[:uuid] = { value: value, expires: 20.year } |
|
62 | + cookies.encrypted[:uuid] = { value: value, expires: 20.year } |
|
63 | end |
|
63 | end |
|
|
64 | + puts "encrypt " + cookies.encrypted[:uuid] | ||
|
|
65 | + puts cookies[:uuid] | ||
|
64 | end |
|
66 | end |
|
65 |
|
67 | ||
|
66 | protected |
|
68 | protected |
|
67 |
|
69 | ||
|
68 | #redirect to root (and also force logout) |
|
70 | #redirect to root (and also force logout) |
|
69 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
71 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
70 | def check_valid_login |
|
72 | def check_valid_login |
|
71 | #check if logged in |
|
73 | #check if logged in |
|
72 | unless session[:user_id] |
|
74 | unless session[:user_id] |
|
73 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
75 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
74 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
76 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
75 | else |
|
77 | else |
|
76 | unauthorized_redirect('You need to login') |
|
78 | unauthorized_redirect('You need to login') |
|
77 | end |
|
79 | end |
|
78 | return false |
|
80 | return false |
|
79 | end |
|
81 | end |
|
80 |
|
82 | ||
|
81 | # check if run in single user mode |
|
83 | # check if run in single user mode |
|
82 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
84 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
83 | if @current_user==nil || (!@current_user.admin?) |
|
85 | if @current_user==nil || (!@current_user.admin?) |
|
84 | unauthorized_redirect('You cannot log in at this time') |
|
86 | unauthorized_redirect('You cannot log in at this time') |
|
85 | return false |
|
87 | return false |
|
86 | end |
|
88 | end |
|
87 | end |
|
89 | end |
|
88 |
|
90 | ||
|
89 | # check if the user is enabled |
|
91 | # check if the user is enabled |
|
90 | unless @current_user.enabled? || @current_user.admin? |
|
92 | unless @current_user.enabled? || @current_user.admin? |
|
91 | unauthorized_redirect 'Your account is disabled' |
|
93 | unauthorized_redirect 'Your account is disabled' |
|
92 | return false |
|
94 | return false |
|
93 | end |
|
95 | end |
|
94 |
|
96 | ||
|
95 | # check if user ip is allowed |
|
97 | # check if user ip is allowed |
|
96 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
98 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
97 | unless is_request_ip_allowed? |
|
99 | unless is_request_ip_allowed? |
|
98 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
100 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
99 | return false |
|
101 | return false |
|
100 | end |
|
102 | end |
|
101 | end |
|
103 | end |
|
102 |
|
104 | ||
|
103 | if GraderConfiguration.multicontests? |
|
105 | if GraderConfiguration.multicontests? |
|
104 | return true if @current_user.admin? |
|
106 | return true if @current_user.admin? |
|
105 | begin |
|
107 | begin |
|
106 | if @current_user.contest_stat(true).forced_logout |
|
108 | if @current_user.contest_stat(true).forced_logout |
|
107 | flash[:notice] = 'You have been automatically logged out.' |
|
109 | flash[:notice] = 'You have been automatically logged out.' |
|
108 | redirect_to :controller => 'main', :action => 'index' |
|
110 | redirect_to :controller => 'main', :action => 'index' |
|
109 | end |
|
111 | end |
|
110 | rescue |
|
112 | rescue |
|
111 | end |
|
113 | end |
|
112 | end |
|
114 | end |
|
113 | return true |
|
115 | return true |
|
114 | end |
|
116 | end |
|
115 |
|
117 | ||
|
116 | #redirect to root (and also force logout) |
|
118 | #redirect to root (and also force logout) |
|
117 | #if the user use different ip from the previous connection |
|
119 | #if the user use different ip from the previous connection |
|
118 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
120 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
119 | def authenticate_by_ip_address |
|
121 | def authenticate_by_ip_address |
|
120 | #this assume that we have already authenticate normally |
|
122 | #this assume that we have already authenticate normally |
|
121 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
123 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
122 | user = User.find(session[:user_id]) |
|
124 | user = User.find(session[:user_id]) |
|
123 | if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) |
|
125 | if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) |
|
124 | flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" |
|
126 | flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" |
|
125 | redirect_to :controller => 'main', :action => 'login' |
|
127 | redirect_to :controller => 'main', :action => 'login' |
|
126 | return false |
|
128 | return false |
|
127 | end |
|
129 | end |
|
128 | unless user.last_ip |
|
130 | unless user.last_ip |
|
129 | user.last_ip = request.remote_ip |
|
131 | user.last_ip = request.remote_ip |
|
130 | user.save |
|
132 | user.save |
|
131 | end |
|
133 | end |
|
132 | end |
|
134 | end |
|
133 | return true |
|
135 | return true |
|
134 | end |
|
136 | end |
|
135 |
|
137 | ||
|
136 | def authorization |
|
138 | def authorization |
|
137 | return false unless check_valid_login |
|
139 | return false unless check_valid_login |
|
138 | user = User.find(session[:user_id]) |
|
140 | user = User.find(session[:user_id]) |
|
139 | unless user.roles.detect { |role| |
|
141 | unless user.roles.detect { |role| |
|
140 | role.rights.detect{ |right| |
|
142 | role.rights.detect{ |right| |
|
141 | right.controller == self.class.controller_name and |
|
143 | right.controller == self.class.controller_name and |
|
142 | (right.action == 'all' || right.action == action_name) |
|
144 | (right.action == 'all' || right.action == action_name) |
|
143 | } |
|
145 | } |
|
144 | } |
|
146 | } |
|
145 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
147 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
146 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
148 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
147 | redirect_to :controller => 'main', :action => 'login' |
|
149 | redirect_to :controller => 'main', :action => 'login' |
|
148 | return false |
|
150 | return false |
|
149 | end |
|
151 | end |
|
150 | end |
|
152 | end |
|
151 |
|
153 | ||
|
152 | def verify_time_limit |
|
154 | def verify_time_limit |
|
153 | return true if session[:user_id]==nil |
|
155 | return true if session[:user_id]==nil |
|
154 | user = User.find(session[:user_id], :include => :site) |
|
156 | user = User.find(session[:user_id], :include => :site) |
|
155 | return true if user==nil || user.site == nil |
|
157 | return true if user==nil || user.site == nil |
|
156 | if user.contest_finished? |
|
158 | if user.contest_finished? |
|
157 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
159 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
158 | redirect_to :back |
|
160 | redirect_to :back |
|
159 | return false |
|
161 | return false |
|
160 | end |
|
162 | end |
|
161 | return true |
|
163 | return true |
|
162 | end |
|
164 | end |
|
163 |
|
165 | ||
|
164 | def is_request_ip_allowed? |
|
166 | def is_request_ip_allowed? |
|
165 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
167 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
166 | user_ip = IPAddr.new(request.remote_ip) |
|
168 | user_ip = IPAddr.new(request.remote_ip) |
|
167 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
169 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
168 |
|
170 | ||
|
169 | allowed.delete(' ').split(',').each do |ips| |
|
171 | allowed.delete(' ').split(',').each do |ips| |
|
170 | allow_ips = IPAddr.new(ips) |
|
172 | allow_ips = IPAddr.new(ips) |
|
171 | if allow_ips.include?(user_ip) |
|
173 | if allow_ips.include?(user_ip) |
|
172 | return true |
|
174 | return true |
|
173 | end |
|
175 | end |
|
174 | end |
|
176 | end |
|
175 | return false |
|
177 | return false |
|
176 | end |
|
178 | end |
|
177 | return true |
|
179 | return true |
|
178 | end |
|
180 | end |
|
179 |
|
181 | ||
|
180 | #function for datatable ajax query |
|
182 | #function for datatable ajax query |
|
181 | #return record,total_count,filter_count |
|
183 | #return record,total_count,filter_count |
|
182 | def process_query_record(record, |
|
184 | def process_query_record(record, |
|
183 | total_count: nil, |
|
185 | total_count: nil, |
|
184 | select: '', |
|
186 | select: '', |
|
185 | global_search: [], |
|
187 | global_search: [], |
|
186 | no_search: false, |
|
188 | no_search: false, |
|
187 | force_order: '', |
|
189 | force_order: '', |
|
188 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
190 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
189 | hard_limit: nil) |
|
191 | hard_limit: nil) |
|
190 | arel_table = record.model.arel_table |
|
192 | arel_table = record.model.arel_table |
|
191 |
|
193 | ||
|
192 | if !no_search && params['search'] |
|
194 | if !no_search && params['search'] |
|
193 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
195 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
194 | if !global_value.blank? |
|
196 | if !global_value.blank? |
|
195 | global_value.split.each do |value| |
|
197 | global_value.split.each do |value| |
|
196 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
198 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
197 | record = record.where(global_where) |
|
199 | record = record.where(global_where) |
|
198 | end |
|
200 | end |
|
199 | end |
|
201 | end |
|
200 |
|
202 | ||
|
201 | params['columns'].each do |i, col| |
|
203 | params['columns'].each do |i, col| |
|
202 | if !col['search']['value'].blank? |
|
204 | if !col['search']['value'].blank? |
|
203 | record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%")) |
|
205 | record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%")) |
|
204 | end |
|
206 | end |
|
205 | end |
|
207 | end |
|
206 | end |
|
208 | end |
|
207 |
|
209 | ||
|
208 | if !date_filter.blank? |
|
210 | if !date_filter.blank? |
|
209 | param_since = params[date_param_since] |
|
211 | param_since = params[date_param_since] |
|
210 | param_until = params[date_param_until] |
|
212 | param_until = params[date_param_until] |
|
211 | date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1) |
|
213 | date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1) |
|
212 | date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now() |
|
214 | date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now() |
|
213 | date_range = date_since..(date_until.end_of_day) |
|
215 | date_range = date_since..(date_until.end_of_day) |
|
214 | record = record.where(date_filter.to_sym => date_range) |
|
216 | record = record.where(date_filter.to_sym => date_range) |
|
215 | end |
|
217 | end |
|
216 |
|
218 | ||
|
217 | if force_order.blank? |
|
219 | if force_order.blank? |
|
218 | if params['order'] |
|
220 | if params['order'] |
|
219 | params['order'].each do |i, o| |
|
221 | params['order'].each do |i, o| |
|
220 | colName = params['columns'][o['column']]['name'] |
|
222 | colName = params['columns'][o['column']]['name'] |
|
221 | colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID' |
|
223 | colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID' |
|
222 | record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank? |
|
224 | record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank? |
|
223 | end |
|
225 | end |
|
224 | end |
|
226 | end |
|
225 | else |
|
227 | else |
|
226 | record = record.order(force_order) |
|
228 | record = record.order(force_order) |
|
227 | end |
|
229 | end |
|
228 |
|
230 | ||
|
229 | filterCount = record.count(record.model.primary_key) |
|
231 | filterCount = record.count(record.model.primary_key) |
|
230 | # if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...} |
|
232 | # if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...} |
|
231 | # so we should count the result again.. |
|
233 | # so we should count the result again.. |
|
232 | if filterCount.is_a? Hash |
|
234 | if filterCount.is_a? Hash |
|
233 | filterCount = filterCount.count |
|
235 | filterCount = filterCount.count |
|
234 | end |
|
236 | end |
|
235 |
|
237 | ||
|
236 |
|
238 | ||
|
237 | record = record.offset(params['start'] || 0) |
|
239 | record = record.offset(params['start'] || 0) |
|
238 | record = record.limit(hard_limit) |
|
240 | record = record.limit(hard_limit) |
|
239 | if (params['length']) |
|
241 | if (params['length']) |
|
240 | limit = params['length'].to_i |
|
242 | limit = params['length'].to_i |
|
241 | limit == hard_limit if (hard_limit && hard_limit < limit) |
|
243 | limit == hard_limit if (hard_limit && hard_limit < limit) |
|
242 | record = record.limit(limit) |
|
244 | record = record.limit(limit) |
|
243 | end |
|
245 | end |
|
244 | if (!select.blank?) |
|
246 | if (!select.blank?) |
|
245 | record = record.select(select) |
|
247 | record = record.select(select) |
|
246 | end |
|
248 | end |
|
247 |
|
249 | ||
|
248 | return record, total_count || record.model.count, filterCount |
|
250 | return record, total_count || record.model.count, filterCount |
|
249 | end |
|
251 | end |
|
250 |
|
252 | ||
|
251 | end |
|
253 | end |
@@ -1,99 +1,99 | |||||
|
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[:uuid] |
|
27 | + user.last_ip = cookies.encrypted[:uuid] |
|
28 | else |
|
28 | else |
|
29 | - if user.last_ip != cookies[:uuid] |
|
29 | + if user.last_ip != cookies.encrypted[:uuid] |
|
30 | - user.last_ip =cookies[: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[:admin] = user.admin? |
|
37 | session[:admin] = user.admin? |
|
38 |
|
38 | ||
|
39 | # clear forced logout flag for multicontests contest change |
|
39 | # clear forced logout flag for multicontests contest change |
|
40 | if GraderConfiguration.multicontests? |
|
40 | if GraderConfiguration.multicontests? |
|
41 | contest_stat = user.contest_stat |
|
41 | contest_stat = user.contest_stat |
|
42 | if contest_stat.respond_to? :forced_logout |
|
42 | if contest_stat.respond_to? :forced_logout |
|
43 | if contest_stat.forced_logout |
|
43 | if contest_stat.forced_logout |
|
44 | contest_stat.forced_logout = false |
|
44 | contest_stat.forced_logout = false |
|
45 | contest_stat.save |
|
45 | contest_stat.save |
|
46 | end |
|
46 | end |
|
47 | end |
|
47 | end |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
|
50 | #save login information |
|
50 | #save login information |
|
51 | - Login.create(user_id: user.id, ip_address: cookies[:uuid]) |
|
51 | + Login.create(user_id: user.id, ip_address: cookies.encrypted[:uuid]) |
|
52 |
|
52 | ||
|
53 | redirect_to :controller => 'main', :action => 'list' |
|
53 | redirect_to :controller => 'main', :action => 'list' |
|
54 | end |
|
54 | end |
|
55 |
|
55 | ||
|
56 | def site_login |
|
56 | def site_login |
|
57 | begin |
|
57 | begin |
|
58 | site = Site.find(params[:login][:site_id]) |
|
58 | site = Site.find(params[:login][:site_id]) |
|
59 | rescue ActiveRecord::RecordNotFound |
|
59 | rescue ActiveRecord::RecordNotFound |
|
60 | site = nil |
|
60 | site = nil |
|
61 | end |
|
61 | end |
|
62 | if site==nil |
|
62 | if site==nil |
|
63 | flash[:notice] = 'Wrong site' |
|
63 | flash[:notice] = 'Wrong site' |
|
64 | redirect_to :controller => 'main', :action => 'login' and return |
|
64 | redirect_to :controller => 'main', :action => 'login' and return |
|
65 | end |
|
65 | end |
|
66 | if (site.password) and (site.password == params[:login][:password]) |
|
66 | if (site.password) and (site.password == params[:login][:password]) |
|
67 | session[:site_id] = site.id |
|
67 | session[:site_id] = site.id |
|
68 | redirect_to :controller => 'site', :action => 'index' |
|
68 | redirect_to :controller => 'site', :action => 'index' |
|
69 | else |
|
69 | else |
|
70 | flash[:notice] = 'Wrong site password' |
|
70 | flash[:notice] = 'Wrong site password' |
|
71 | redirect_to :controller => 'site', :action => 'login' |
|
71 | redirect_to :controller => 'site', :action => 'login' |
|
72 | end |
|
72 | end |
|
73 | end |
|
73 | end |
|
74 |
|
74 | ||
|
75 | def logout |
|
75 | def logout |
|
76 | redirect_to root_path |
|
76 | redirect_to root_path |
|
77 | end |
|
77 | end |
|
78 |
|
78 | ||
|
79 | def self.add_authenticator(authenticator) |
|
79 | def self.add_authenticator(authenticator) |
|
80 | @@authenticators << authenticator |
|
80 | @@authenticators << authenticator |
|
81 | end |
|
81 | end |
|
82 |
|
82 | ||
|
83 | protected |
|
83 | protected |
|
84 |
|
84 | ||
|
85 | def get_authenticated_user(login, password) |
|
85 | def get_authenticated_user(login, password) |
|
86 | if @@authenticators.empty? |
|
86 | if @@authenticators.empty? |
|
87 | return User.authenticate(login, password) |
|
87 | return User.authenticate(login, password) |
|
88 | else |
|
88 | else |
|
89 | user = User.authenticate(login, password) |
|
89 | user = User.authenticate(login, password) |
|
90 | @@authenticators.each do |authenticator| |
|
90 | @@authenticators.each do |authenticator| |
|
91 | if not user |
|
91 | if not user |
|
92 | user = authenticator.authenticate(login, password) |
|
92 | user = authenticator.authenticate(login, password) |
|
93 | end |
|
93 | end |
|
94 | end |
|
94 | end |
|
95 | return user |
|
95 | return user |
|
96 | end |
|
96 | end |
|
97 | end |
|
97 | end |
|
98 |
|
98 | ||
|
99 | end |
|
99 | end |
You need to be logged in to leave comments.
Login now