Description:
added default contest
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r308:c400f7405eee - - 2 files changed: 22 inserted, 0 deleted
@@ -8,96 +8,97 | |||
|
8 | 8 | |
|
9 | 9 | has_many :messages, |
|
10 | 10 | :class_name => "Message", |
|
11 | 11 | :foreign_key => "sender_id", |
|
12 | 12 | :order => 'created_at DESC' |
|
13 | 13 | |
|
14 | 14 | has_many :replied_messages, |
|
15 | 15 | :class_name => "Message", |
|
16 | 16 | :foreign_key => "receiver_id", |
|
17 | 17 | :order => 'created_at DESC' |
|
18 | 18 | |
|
19 | 19 | has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy |
|
20 | 20 | |
|
21 | 21 | belongs_to :site |
|
22 | 22 | belongs_to :country |
|
23 | 23 | |
|
24 | 24 | has_and_belongs_to_many :contests, :uniq => true, :order => 'name' |
|
25 | 25 | |
|
26 | 26 | named_scope :activated_users, :conditions => {:activated => true} |
|
27 | 27 | |
|
28 | 28 | validates_presence_of :login |
|
29 | 29 | validates_uniqueness_of :login |
|
30 | 30 | validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/ |
|
31 | 31 | validates_length_of :login, :within => 3..30 |
|
32 | 32 | |
|
33 | 33 | validates_presence_of :full_name |
|
34 | 34 | validates_length_of :full_name, :minimum => 1 |
|
35 | 35 | |
|
36 | 36 | validates_presence_of :password, :if => :password_required? |
|
37 | 37 | validates_length_of :password, :within => 4..20, :if => :password_required? |
|
38 | 38 | validates_confirmation_of :password, :if => :password_required? |
|
39 | 39 | |
|
40 | 40 | validates_format_of :email, |
|
41 | 41 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
42 | 42 | :if => :email_validation? |
|
43 | 43 | validate :uniqueness_of_email_from_activated_users, |
|
44 | 44 | :if => :email_validation? |
|
45 | 45 | validate :enough_time_interval_between_same_email_registrations, |
|
46 | 46 | :if => :email_validation? |
|
47 | 47 | |
|
48 | 48 | # these are for ytopc |
|
49 | 49 | # disable for now |
|
50 | 50 | #validates_presence_of :province |
|
51 | 51 | |
|
52 | 52 | attr_accessor :password |
|
53 | 53 | |
|
54 | 54 | before_save :encrypt_new_password |
|
55 | 55 | before_save :assign_default_site |
|
56 | + before_save :assign_default_contest | |
|
56 | 57 | |
|
57 | 58 | # this is for will_paginate |
|
58 | 59 | cattr_reader :per_page |
|
59 | 60 | @@per_page = 50 |
|
60 | 61 | |
|
61 | 62 | def self.authenticate(login, password) |
|
62 | 63 | user = find_by_login(login) |
|
63 | 64 | return user if user && user.authenticated?(password) |
|
64 | 65 | end |
|
65 | 66 | |
|
66 | 67 | def authenticated?(password) |
|
67 | 68 | if self.activated |
|
68 | 69 | hashed_password == User.encrypt(password,self.salt) |
|
69 | 70 | else |
|
70 | 71 | false |
|
71 | 72 | end |
|
72 | 73 | end |
|
73 | 74 | |
|
74 | 75 | def admin? |
|
75 | 76 | self.roles.detect {|r| r.name == 'admin' } |
|
76 | 77 | end |
|
77 | 78 | |
|
78 | 79 | def email_for_editing |
|
79 | 80 | if self.email==nil |
|
80 | 81 | "(unknown)" |
|
81 | 82 | elsif self.email=='' |
|
82 | 83 | "(blank)" |
|
83 | 84 | else |
|
84 | 85 | self.email |
|
85 | 86 | end |
|
86 | 87 | end |
|
87 | 88 | |
|
88 | 89 | def email_for_editing=(e) |
|
89 | 90 | self.email=e |
|
90 | 91 | end |
|
91 | 92 | |
|
92 | 93 | def alias_for_editing |
|
93 | 94 | if self.alias==nil |
|
94 | 95 | "(unknown)" |
|
95 | 96 | elsif self.alias=='' |
|
96 | 97 | "(blank)" |
|
97 | 98 | else |
|
98 | 99 | self.alias |
|
99 | 100 | end |
|
100 | 101 | end |
|
101 | 102 | |
|
102 | 103 | def alias_for_editing=(e) |
|
103 | 104 | self.alias=e |
@@ -228,82 +229,95 | |||
|
228 | 229 | |
|
229 | 230 | def available_problems |
|
230 | 231 | if not Configuration.multicontests? |
|
231 | 232 | return Problem.find_available_problems |
|
232 | 233 | else |
|
233 | 234 | contest_problems = [] |
|
234 | 235 | pin = {} |
|
235 | 236 | contests.enabled.each do |contest| |
|
236 | 237 | contest.problems.available.each do |problem| |
|
237 | 238 | if not pin.has_key? problem.id |
|
238 | 239 | contest_problems << problem |
|
239 | 240 | end |
|
240 | 241 | pin[problem.id] = true |
|
241 | 242 | end |
|
242 | 243 | end |
|
243 | 244 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
244 | 245 | return contest_problems + other_avaiable_problems |
|
245 | 246 | end |
|
246 | 247 | end |
|
247 | 248 | |
|
248 | 249 | def can_view_problem?(problem) |
|
249 | 250 | if not Configuration.multicontests? |
|
250 | 251 | return problem.available |
|
251 | 252 | else |
|
252 | 253 | return problem_in_user_contests? problem |
|
253 | 254 | end |
|
254 | 255 | end |
|
255 | 256 | |
|
256 | 257 | protected |
|
257 | 258 | def encrypt_new_password |
|
258 | 259 | return if password.blank? |
|
259 | 260 | self.salt = (10+rand(90)).to_s |
|
260 | 261 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
261 | 262 | end |
|
262 | 263 | |
|
263 | 264 | def assign_default_site |
|
264 | 265 | # have to catch error when migrating (because self.site is not available). |
|
265 | 266 | begin |
|
266 | 267 | if self.site==nil |
|
267 | 268 | self.site = Site.find_by_name('default') |
|
268 | 269 | if self.site==nil |
|
269 | 270 | self.site = Site.find(1) # when 'default has be renamed' |
|
270 | 271 | end |
|
271 | 272 | end |
|
272 | 273 | rescue |
|
273 | 274 | end |
|
274 | 275 | end |
|
275 | 276 | |
|
277 | + def assign_default_contest | |
|
278 | + # have to catch error when migrating (because self.site is not available). | |
|
279 | + begin | |
|
280 | + if self.contests.length == 0 | |
|
281 | + default_contest = Contest.find_by_name(Configuration['contest.default_contest_name']) | |
|
282 | + if default_contest | |
|
283 | + self.contests = [default_contest] | |
|
284 | + end | |
|
285 | + end | |
|
286 | + rescue | |
|
287 | + end | |
|
288 | + end | |
|
289 | + | |
|
276 | 290 | def password_required? |
|
277 | 291 | self.hashed_password.blank? || !self.password.blank? |
|
278 | 292 | end |
|
279 | 293 | |
|
280 | 294 | def self.encrypt(string,salt) |
|
281 | 295 | Digest::SHA1.hexdigest(salt + string) |
|
282 | 296 | end |
|
283 | 297 | |
|
284 | 298 | def uniqueness_of_email_from_activated_users |
|
285 | 299 | user = User.activated_users.find_by_email(self.email) |
|
286 | 300 | if user and (user.login != self.login) |
|
287 | 301 | self.errors.add_to_base("Email has already been taken") |
|
288 | 302 | end |
|
289 | 303 | end |
|
290 | 304 | |
|
291 | 305 | def enough_time_interval_between_same_email_registrations |
|
292 | 306 | return if !self.new_record? |
|
293 | 307 | return if self.activated |
|
294 | 308 | open_user = User.find_by_email(self.email, |
|
295 | 309 | :order => 'created_at DESC') |
|
296 | 310 | if open_user and open_user.created_at and |
|
297 | 311 | (open_user.created_at > Time.now.gmtime - 5.minutes) |
|
298 | 312 | self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") |
|
299 | 313 | end |
|
300 | 314 | end |
|
301 | 315 | |
|
302 | 316 | def email_validation? |
|
303 | 317 | begin |
|
304 | 318 | return VALIDATE_USER_EMAILS |
|
305 | 319 | rescue |
|
306 | 320 | return false |
|
307 | 321 | end |
|
308 | 322 | end |
|
309 | 323 | end |
@@ -62,97 +62,105 | |||
|
62 | 62 | |
|
63 | 63 | # If Configuration['system.online_registration'] is true, the |
|
64 | 64 | # system allows online registration, and will use these |
|
65 | 65 | # information for sending confirmation emails. |
|
66 | 66 | { |
|
67 | 67 | :key => 'system.online_registration.smtp', |
|
68 | 68 | :value_type => 'string', |
|
69 | 69 | :default_value => 'smtp.somehost.com' |
|
70 | 70 | }, |
|
71 | 71 | |
|
72 | 72 | { |
|
73 | 73 | :key => 'system.online_registration.from', |
|
74 | 74 | :value_type => 'string', |
|
75 | 75 | :default_value => 'your.email@address' |
|
76 | 76 | }, |
|
77 | 77 | |
|
78 | 78 | { |
|
79 | 79 | :key => 'system.admin_email', |
|
80 | 80 | :value_type => 'string', |
|
81 | 81 | :default_value => 'admin@admin.email' |
|
82 | 82 | }, |
|
83 | 83 | |
|
84 | 84 | { |
|
85 | 85 | :key => 'system.user_setting_enabled', |
|
86 | 86 | :value_type => 'boolean', |
|
87 | 87 | :default_value => 'true', |
|
88 | 88 | :description => 'If this option is true, users can change their settings' |
|
89 | 89 | }, |
|
90 | 90 | |
|
91 | 91 | # If Configuration['contest.test_request.early_timeout'] is true |
|
92 | 92 | # the user will not be able to use test request at 30 minutes |
|
93 | 93 | # before the contest ends. |
|
94 | 94 | { |
|
95 | 95 | :key => 'contest.test_request.early_timeout', |
|
96 | 96 | :value_type => 'boolean', |
|
97 | 97 | :default_value => 'false' |
|
98 | 98 | }, |
|
99 | 99 | |
|
100 | 100 | { |
|
101 | 101 | :key => 'system.multicontests', |
|
102 | 102 | :value_type => 'boolean', |
|
103 | 103 | :default_value => 'false' |
|
104 | 104 | }, |
|
105 | 105 | |
|
106 | 106 | { |
|
107 | 107 | :key => 'contest.confirm_indv_contest_start', |
|
108 | 108 | :value_type => 'boolean', |
|
109 | 109 | :default_value => 'false' |
|
110 | + }, | |
|
111 | + | |
|
112 | + { | |
|
113 | + :key => 'contest.default_contest_name', | |
|
114 | + :value_type => 'string', | |
|
115 | + :default_value => 'none', | |
|
116 | + :description => "New user will be assigned to this contest automatically, if it exists. Set to 'none' if there is no default contest." | |
|
110 | 117 | } |
|
118 | + | |
|
111 | 119 | ] |
|
112 | 120 | |
|
113 | 121 | |
|
114 | 122 | def create_configuration_key(key, |
|
115 | 123 | value_type, |
|
116 | 124 | default_value, |
|
117 | 125 | description='') |
|
118 | 126 | conf = (Configuration.find_by_key(key) || |
|
119 | 127 | Configuration.new(:key => key, |
|
120 | 128 | :value_type => value_type, |
|
121 | 129 | :value => default_value)) |
|
122 | 130 | conf.description = description |
|
123 | 131 | conf.save |
|
124 | 132 | end |
|
125 | 133 | |
|
126 | 134 | def seed_config |
|
127 | 135 | CONFIGURATIONS.each do |conf| |
|
128 | 136 | if conf.has_key? :description |
|
129 | 137 | desc = conf[:description] |
|
130 | 138 | else |
|
131 | 139 | desc = '' |
|
132 | 140 | end |
|
133 | 141 | create_configuration_key(conf[:key], |
|
134 | 142 | conf[:value_type], |
|
135 | 143 | conf[:default_value], |
|
136 | 144 | desc) |
|
137 | 145 | end |
|
138 | 146 | end |
|
139 | 147 | |
|
140 | 148 | def seed_roles |
|
141 | 149 | return if Role.find_by_name('admin') |
|
142 | 150 | |
|
143 | 151 | role = Role.create(:name => 'admin') |
|
144 | 152 | user_admin_right = Right.create(:name => 'user_admin', |
|
145 | 153 | :controller => 'user_admin', |
|
146 | 154 | :action => 'all') |
|
147 | 155 | problem_admin_right = Right.create(:name=> 'problem_admin', |
|
148 | 156 | :controller => 'problems', |
|
149 | 157 | :action => 'all') |
|
150 | 158 | |
|
151 | 159 | graders_right = Right.create(:name => 'graders_admin', |
|
152 | 160 | :controller => 'graders', |
|
153 | 161 | :action => 'all') |
|
154 | 162 | |
|
155 | 163 | role.rights << user_admin_right; |
|
156 | 164 | role.rights << problem_admin_right; |
|
157 | 165 | role.rights << graders_right; |
|
158 | 166 | role.save |
You need to be logged in to leave comments.
Login now