Description:
fix user.admin? bug
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r757:35d86dd3635c - - 2 files changed: 5 inserted, 4 deleted
@@ -1,166 +1,167 | |||
|
1 | 1 | require 'ipaddr' |
|
2 | 2 | |
|
3 | 3 | class ApplicationController < ActionController::Base |
|
4 | 4 | protect_from_forgery |
|
5 | 5 | |
|
6 | 6 | before_action :current_user |
|
7 | 7 | |
|
8 | 8 | SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' |
|
9 | 9 | MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' |
|
10 | 10 | ALLOW_WHITELIST_IP_ONLY_CONF_KEY = 'right.allow_whitelist_ip_only' |
|
11 | 11 | WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' |
|
12 | 12 | |
|
13 | 13 | #report and redirect for unauthorized activities |
|
14 | 14 | def unauthorized_redirect(notice = 'You are not authorized to view the page you requested') |
|
15 | 15 | flash[:notice] = notice |
|
16 | 16 | redirect_to login_main_path |
|
17 | 17 | end |
|
18 | 18 | |
|
19 | 19 | # Returns the current logged-in user (if any). |
|
20 | 20 | def current_user |
|
21 | 21 | return nil unless session[:user_id] |
|
22 | 22 | @current_user ||= User.find(session[:user_id]) |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def admin_authorization |
|
26 | 26 | return false unless check_valid_login |
|
27 | 27 | user = User.includes(:roles).find(session[:user_id]) |
|
28 | 28 | unless user.admin? |
|
29 | 29 | unauthorized_redirect |
|
30 | 30 | return false |
|
31 | 31 | end |
|
32 | 32 | return true |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def authorization_by_roles(allowed_roles) |
|
36 | 36 | return false unless check_valid_login |
|
37 | 37 | user = User.find(session[:user_id]) |
|
38 | 38 | unless user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
39 | 39 | unauthorized_redirect |
|
40 | 40 | return false |
|
41 | 41 | end |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def testcase_authorization |
|
45 | 45 | #admin always has privileged |
|
46 | 46 | if @current_user.admin? |
|
47 | 47 | return true |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | protected |
|
55 | 55 | |
|
56 | 56 | #redirect to root (and also force logout) |
|
57 | 57 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
58 | 58 | def check_valid_login |
|
59 | 59 | #check if logged in |
|
60 | 60 | unless session[:user_id] |
|
61 | 61 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
62 | 62 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
63 | 63 | else |
|
64 | 64 | unauthorized_redirect('You need to login') |
|
65 | 65 | end |
|
66 | 66 | return false |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | # check if run in single user mode |
|
70 | 70 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
71 |
- if @current_user==nil || ( |
|
|
71 | + if @current_user==nil || (!@current_user.admin?) | |
|
72 | 72 | unauthorized_redirect('You cannot log in at this time') |
|
73 | 73 | return false |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | # check if the user is enabled |
|
78 | 78 | unless @current_user.enabled? || @current_user.admin? |
|
79 | 79 | unauthorized_redirect 'Your account is disabled' |
|
80 | 80 | return false |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | # check if user ip is allowed |
|
84 | 84 | unless @current_user.admin? || !GraderConfiguration[ALLOW_WHITELIST_IP_ONLY_CONF_KEY] |
|
85 | 85 | unless is_request_ip_allowed? |
|
86 | 86 | unauthorized_redirect 'Your IP is not allowed' |
|
87 | 87 | return false |
|
88 | 88 | end |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | if GraderConfiguration.multicontests? |
|
92 | 92 | return true if @current_user.admin? |
|
93 | 93 | begin |
|
94 | 94 | if @current_user.contest_stat(true).forced_logout |
|
95 | 95 | flash[:notice] = 'You have been automatically logged out.' |
|
96 | 96 | redirect_to :controller => 'main', :action => 'index' |
|
97 | 97 | end |
|
98 | 98 | rescue |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | return true |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | #redirect to root (and also force logout) |
|
105 | 105 | #if the user use different ip from the previous connection |
|
106 | 106 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
107 | 107 | def authenticate_by_ip_address |
|
108 | 108 | #this assume that we have already authenticate normally |
|
109 | 109 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
110 | 110 | user = User.find(session[:user_id]) |
|
111 | - if (not @current_user.admin? && user.last_ip && user.last_ip != request.remote_ip) | |
|
111 | + puts "User admin #{user.admin?}" | |
|
112 | + if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) | |
|
112 | 113 | flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" |
|
114 | + puts "hahaha" | |
|
113 | 115 | redirect_to :controller => 'main', :action => 'login' |
|
114 | - puts "CHEAT: user #{user.login} tried to login from '#{request.remote_ip}' while last ip is '#{user.last_ip}' at #{Time.zone.now}" | |
|
115 | 116 | return false |
|
116 | 117 | end |
|
117 | 118 | unless user.last_ip |
|
118 | 119 | user.last_ip = request.remote_ip |
|
119 | 120 | user.save |
|
120 | 121 | end |
|
121 | 122 | end |
|
122 | 123 | return true |
|
123 | 124 | end |
|
124 | 125 | |
|
125 | 126 | def authorization |
|
126 | 127 | return false unless check_valid_login |
|
127 | 128 | user = User.find(session[:user_id]) |
|
128 | 129 | unless user.roles.detect { |role| |
|
129 | 130 | role.rights.detect{ |right| |
|
130 | 131 | right.controller == self.class.controller_name and |
|
131 | 132 | (right.action == 'all' || right.action == action_name) |
|
132 | 133 | } |
|
133 | 134 | } |
|
134 | 135 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
135 | 136 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
136 | 137 | redirect_to :controller => 'main', :action => 'login' |
|
137 | 138 | return false |
|
138 | 139 | end |
|
139 | 140 | end |
|
140 | 141 | |
|
141 | 142 | def verify_time_limit |
|
142 | 143 | return true if session[:user_id]==nil |
|
143 | 144 | user = User.find(session[:user_id], :include => :site) |
|
144 | 145 | return true if user==nil || user.site == nil |
|
145 | 146 | if user.contest_finished? |
|
146 | 147 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
147 | 148 | redirect_to :back |
|
148 | 149 | return false |
|
149 | 150 | end |
|
150 | 151 | return true |
|
151 | 152 | end |
|
152 | 153 | |
|
153 | 154 | def is_request_ip_allowed? |
|
154 | 155 | if GraderConfiguration[ALLOW_WHITELIST_IP_ONLY_CONF_KEY] |
|
155 | 156 | user_ip = IPAddr.new(request.remote_ip) |
|
156 | 157 | GraderConfiguration[WHITELIST_IP_LIST_CONF_KEY].delete(' ').split(',').each do |ips| |
|
157 | 158 | allow_ips = IPAddr.new(ips) |
|
158 | 159 | unless allow_ips.includes(user_ip) |
|
159 | 160 | return false |
|
160 | 161 | end |
|
161 | 162 | end |
|
162 | 163 | end |
|
163 | 164 | return true |
|
164 | 165 | end |
|
165 | 166 | |
|
166 | 167 | end |
@@ -1,180 +1,180 | |||
|
1 | 1 | require 'digest/sha1' |
|
2 | 2 | require 'net/pop' |
|
3 | 3 | require 'net/https' |
|
4 | 4 | require 'net/http' |
|
5 | 5 | require 'json' |
|
6 | 6 | |
|
7 | 7 | class User < ActiveRecord::Base |
|
8 | 8 | |
|
9 | 9 | has_and_belongs_to_many :roles |
|
10 | 10 | |
|
11 | 11 | #has_and_belongs_to_many :groups |
|
12 | 12 | has_many :groups_users, class_name: 'GroupUser' |
|
13 | 13 | has_many :groups, :through => :groups_users |
|
14 | 14 | |
|
15 | 15 | has_many :test_requests, -> {order(submitted_at: :desc)} |
|
16 | 16 | |
|
17 | 17 | has_many :messages, -> { order(created_at: :desc) }, |
|
18 | 18 | :class_name => "Message", |
|
19 | 19 | :foreign_key => "sender_id" |
|
20 | 20 | |
|
21 | 21 | has_many :replied_messages, -> { order(created_at: :desc) }, |
|
22 | 22 | :class_name => "Message", |
|
23 | 23 | :foreign_key => "receiver_id" |
|
24 | 24 | |
|
25 | 25 | has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy |
|
26 | 26 | |
|
27 | 27 | belongs_to :site |
|
28 | 28 | belongs_to :country |
|
29 | 29 | |
|
30 | 30 | has_and_belongs_to_many :contests, -> { order(:name)} |
|
31 | 31 | |
|
32 | 32 | scope :activated_users, -> {where activated: true} |
|
33 | 33 | |
|
34 | 34 | validates_presence_of :login |
|
35 | 35 | validates_uniqueness_of :login |
|
36 | 36 | validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/ |
|
37 | 37 | validates_length_of :login, :within => 3..30 |
|
38 | 38 | |
|
39 | 39 | validates_presence_of :full_name |
|
40 | 40 | validates_length_of :full_name, :minimum => 1 |
|
41 | 41 | |
|
42 | 42 | validates_presence_of :password, :if => :password_required? |
|
43 | 43 | validates_length_of :password, :within => 4..20, :if => :password_required? |
|
44 | 44 | validates_confirmation_of :password, :if => :password_required? |
|
45 | 45 | |
|
46 | 46 | validates_format_of :email, |
|
47 | 47 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
48 | 48 | :if => :email_validation? |
|
49 | 49 | validate :uniqueness_of_email_from_activated_users, |
|
50 | 50 | :if => :email_validation? |
|
51 | 51 | validate :enough_time_interval_between_same_email_registrations, |
|
52 | 52 | :if => :email_validation? |
|
53 | 53 | |
|
54 | 54 | # these are for ytopc |
|
55 | 55 | # disable for now |
|
56 | 56 | #validates_presence_of :province |
|
57 | 57 | |
|
58 | 58 | attr_accessor :password |
|
59 | 59 | |
|
60 | 60 | before_save :encrypt_new_password |
|
61 | 61 | before_save :assign_default_site |
|
62 | 62 | before_save :assign_default_contest |
|
63 | 63 | |
|
64 | 64 | # this is for will_paginate |
|
65 | 65 | cattr_reader :per_page |
|
66 | 66 | @@per_page = 50 |
|
67 | 67 | |
|
68 | 68 | def self.authenticate(login, password) |
|
69 | 69 | user = find_by_login(login) |
|
70 | 70 | if user |
|
71 | 71 | return user if user.authenticated?(password) |
|
72 | 72 | end |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def authenticated?(password) |
|
76 | 76 | if self.activated |
|
77 | 77 | hashed_password == User.encrypt(password,self.salt) |
|
78 | 78 | else |
|
79 | 79 | false |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def admin? |
|
84 |
- self.roles. |
|
|
84 | + self.roles.where(name: 'admin').count > 0 | |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def email_for_editing |
|
88 | 88 | if self.email==nil |
|
89 | 89 | "(unknown)" |
|
90 | 90 | elsif self.email=='' |
|
91 | 91 | "(blank)" |
|
92 | 92 | else |
|
93 | 93 | self.email |
|
94 | 94 | end |
|
95 | 95 | end |
|
96 | 96 | |
|
97 | 97 | def email_for_editing=(e) |
|
98 | 98 | self.email=e |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | def alias_for_editing |
|
102 | 102 | if self.alias==nil |
|
103 | 103 | "(unknown)" |
|
104 | 104 | elsif self.alias=='' |
|
105 | 105 | "(blank)" |
|
106 | 106 | else |
|
107 | 107 | self.alias |
|
108 | 108 | end |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def alias_for_editing=(e) |
|
112 | 112 | self.alias=e |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def activation_key |
|
116 | 116 | if self.hashed_password==nil |
|
117 | 117 | encrypt_new_password |
|
118 | 118 | end |
|
119 | 119 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def verify_activation_key(key) |
|
123 | 123 | key == activation_key |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def self.random_password(length=5) |
|
127 | 127 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
128 | 128 | password = '' |
|
129 | 129 | length.times { password << chars[rand(chars.length - 1)] } |
|
130 | 130 | password |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def self.find_non_admin_with_prefix(prefix='') |
|
134 | 134 | users = User.all |
|
135 | 135 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | # Contest information |
|
139 | 139 | |
|
140 | 140 | def self.find_users_with_no_contest() |
|
141 | 141 | users = User.all |
|
142 | 142 | return users.find_all { |u| u.contests.length == 0 } |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | def contest_time_left |
|
147 | 147 | if GraderConfiguration.contest_mode? |
|
148 | 148 | return nil if site==nil |
|
149 | 149 | return site.time_left |
|
150 | 150 | elsif GraderConfiguration.indv_contest_mode? |
|
151 | 151 | time_limit = GraderConfiguration.contest_time_limit |
|
152 | 152 | if time_limit == nil |
|
153 | 153 | return nil |
|
154 | 154 | end |
|
155 | 155 | if contest_stat==nil or contest_stat.started_at==nil |
|
156 | 156 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
157 | 157 | else |
|
158 | 158 | finish_time = contest_stat.started_at + time_limit |
|
159 | 159 | current_time = Time.now.gmtime |
|
160 | 160 | if current_time > finish_time |
|
161 | 161 | return 0 |
|
162 | 162 | else |
|
163 | 163 | return finish_time - current_time |
|
164 | 164 | end |
|
165 | 165 | end |
|
166 | 166 | else |
|
167 | 167 | return nil |
|
168 | 168 | end |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | def contest_finished? |
|
172 | 172 | if GraderConfiguration.contest_mode? |
|
173 | 173 | return false if site==nil |
|
174 | 174 | return site.finished? |
|
175 | 175 | elsif GraderConfiguration.indv_contest_mode? |
|
176 | 176 | return false if self.contest_stat==nil |
|
177 | 177 | return contest_time_left == 0 |
|
178 | 178 | else |
|
179 | 179 | return false |
|
180 | 180 | end |
You need to be logged in to leave comments.
Login now