Description:
added default contest
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r308:c400f7405eee - - 3 files changed: 23 inserted, 1 deleted

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