Description:
sends mails by mail gem
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r331:4917c2675f37 - - 4 files changed: 66 inserted, 47 deleted

@@ -1,50 +1,50
1 1 source 'https://rubygems.org'
2 2
3 3 gem 'rails', '3.2.8'
4 4
5 5 # Bundle edge Rails instead:
6 6 # gem 'rails', :git => 'git://github.com/rails/rails.git'
7 7
8 8 gem 'mysql2'
9 9
10 10 # Gems used only for assets and not required
11 11 # in production environments by default.
12 12 group :assets do
13 13 gem 'sass-rails', '~> 3.2.3'
14 14 gem 'coffee-rails', '~> 3.2.1'
15 15
16 16 # See https://github.com/sstephenson/execjs#readme for more supported runtimes
17 17 # gem 'therubyracer', :platforms => :ruby
18 18
19 19 gem 'uglifier', '>= 1.0.3'
20 20 end
21 21
22 22 gem 'prototype-rails'
23 23
24 24 # To use ActiveModel has_secure_password
25 25 # gem 'bcrypt-ruby', '~> 3.0.0'
26 26
27 27 # To use Jbuilder templates for JSON
28 28 # gem 'jbuilder'
29 29
30 30 # Use unicorn as the app server
31 31 # gem 'unicorn'
32 32
33 33 # Deploy with Capistrano
34 34 # gem 'capistrano'
35 35
36 36 # To use debugger
37 37 # gem 'debugger'
38 38
39 39 gem "haml"
40 - gem "tmail"
40 + gem "mail"
41 41 gem "rdiscount"
42 42 gem "test-unit"
43 43 gem 'will_paginate', '~> 3.0.0'
44 44 gem 'dynamic_form'
45 45 gem 'in_place_editing'
46 46 gem 'verification', :git => 'git://github.com/sikachu/verification.git'
47 47
48 48 group :test, :development do
49 49 gem "rspec-rails", "~> 2.0"
50 50 end
@@ -1,148 +1,147
1 1 GIT
2 2 remote: git://github.com/sikachu/verification.git
3 3 revision: 344ad2535da3dc9671872628ff9c79d6f59af9da
4 4 specs:
5 5 verification (1.0.3)
6 6 actionpack (>= 3.0.0, < 3.3.0)
7 7 activesupport (>= 3.0.0, < 3.3.0)
8 8
9 9 GEM
10 10 remote: https://rubygems.org/
11 11 specs:
12 12 actionmailer (3.2.8)
13 13 actionpack (= 3.2.8)
14 14 mail (~> 2.4.4)
15 15 actionpack (3.2.8)
16 16 activemodel (= 3.2.8)
17 17 activesupport (= 3.2.8)
18 18 builder (~> 3.0.0)
19 19 erubis (~> 2.7.0)
20 20 journey (~> 1.0.4)
21 21 rack (~> 1.4.0)
22 22 rack-cache (~> 1.2)
23 23 rack-test (~> 0.6.1)
24 24 sprockets (~> 2.1.3)
25 25 activemodel (3.2.8)
26 26 activesupport (= 3.2.8)
27 27 builder (~> 3.0.0)
28 28 activerecord (3.2.8)
29 29 activemodel (= 3.2.8)
30 30 activesupport (= 3.2.8)
31 31 arel (~> 3.0.2)
32 32 tzinfo (~> 0.3.29)
33 33 activeresource (3.2.8)
34 34 activemodel (= 3.2.8)
35 35 activesupport (= 3.2.8)
36 36 activesupport (3.2.8)
37 37 i18n (~> 0.6)
38 38 multi_json (~> 1.0)
39 39 arel (3.0.2)
40 40 builder (3.0.3)
41 41 coffee-rails (3.2.2)
42 42 coffee-script (>= 2.2.0)
43 43 railties (~> 3.2.0)
44 44 coffee-script (2.2.0)
45 45 coffee-script-source
46 46 execjs
47 47 coffee-script-source (1.3.3)
48 48 diff-lcs (1.1.3)
49 49 dynamic_form (1.1.4)
50 50 erubis (2.7.0)
51 51 execjs (1.4.0)
52 52 multi_json (~> 1.0)
53 53 haml (3.1.7)
54 54 hike (1.2.1)
55 55 i18n (0.6.1)
56 56 in_place_editing (1.2.0)
57 57 journey (1.0.4)
58 58 json (1.7.5)
59 59 mail (2.4.4)
60 60 i18n (>= 0.4.0)
61 61 mime-types (~> 1.16)
62 62 treetop (~> 1.4.8)
63 63 mime-types (1.19)
64 64 multi_json (1.3.6)
65 65 mysql2 (0.3.11)
66 66 polyglot (0.3.3)
67 67 prototype-rails (3.2.1)
68 68 rails (~> 3.2)
69 69 rack (1.4.1)
70 70 rack-cache (1.2)
71 71 rack (>= 0.4)
72 72 rack-ssl (1.3.2)
73 73 rack
74 74 rack-test (0.6.2)
75 75 rack (>= 1.0)
76 76 rails (3.2.8)
77 77 actionmailer (= 3.2.8)
78 78 actionpack (= 3.2.8)
79 79 activerecord (= 3.2.8)
80 80 activeresource (= 3.2.8)
81 81 activesupport (= 3.2.8)
82 82 bundler (~> 1.0)
83 83 railties (= 3.2.8)
84 84 railties (3.2.8)
85 85 actionpack (= 3.2.8)
86 86 activesupport (= 3.2.8)
87 87 rack-ssl (~> 1.3.2)
88 88 rake (>= 0.8.7)
89 89 rdoc (~> 3.4)
90 90 thor (>= 0.14.6, < 2.0)
91 91 rake (0.9.2.2)
92 92 rdiscount (1.6.8)
93 93 rdoc (3.12)
94 94 json (~> 1.4)
95 95 rspec (2.11.0)
96 96 rspec-core (~> 2.11.0)
97 97 rspec-expectations (~> 2.11.0)
98 98 rspec-mocks (~> 2.11.0)
99 99 rspec-core (2.11.1)
100 100 rspec-expectations (2.11.3)
101 101 diff-lcs (~> 1.1.3)
102 102 rspec-mocks (2.11.3)
103 103 rspec-rails (2.11.0)
104 104 actionpack (>= 3.0)
105 105 activesupport (>= 3.0)
106 106 railties (>= 3.0)
107 107 rspec (~> 2.11.0)
108 108 sass (3.2.1)
109 109 sass-rails (3.2.5)
110 110 railties (~> 3.2.0)
111 111 sass (>= 3.1.10)
112 112 tilt (~> 1.3)
113 113 sprockets (2.1.3)
114 114 hike (~> 1.2)
115 115 rack (~> 1.0)
116 116 tilt (~> 1.1, != 1.3.0)
117 117 test-unit (2.5.2)
118 118 thor (0.16.0)
119 119 tilt (1.3.3)
120 - tmail (1.2.7.1)
121 120 treetop (1.4.10)
122 121 polyglot
123 122 polyglot (>= 0.3.1)
124 123 tzinfo (0.3.33)
125 124 uglifier (1.3.0)
126 125 execjs (>= 0.3.0)
127 126 multi_json (~> 1.0, >= 1.0.2)
128 127 will_paginate (3.0.3)
129 128
130 129 PLATFORMS
131 130 ruby
132 131
133 132 DEPENDENCIES
134 133 coffee-rails (~> 3.2.1)
135 134 dynamic_form
136 135 haml
137 136 in_place_editing
137 + mail
138 138 mysql2
139 139 prototype-rails
140 140 rails (= 3.2.8)
141 141 rdiscount
142 142 rspec-rails (~> 2.0)
143 143 sass-rails (~> 3.2.3)
144 144 test-unit
145 - tmail
146 145 uglifier (>= 1.0.3)
147 146 verification!
148 147 will_paginate (~> 3.0.0)
@@ -1,455 +1,467
1 1 class UserAdminController < ApplicationController
2 2
3 - #include MailHelperMethods
4 -
5 3 before_filter :admin_authorization
6 4
7 5 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
8 6 verify :method => :post, :only => [ :destroy,
9 7 :create, :create_from_list,
10 8 :update,
11 9 :manage_contest,
12 10 :bulk_mail
13 11 ],
14 12 :redirect_to => { :action => :list }
15 13
16 14 def index
17 15 list
18 16 render :action => 'list'
19 17 end
20 18
21 19 def list
22 20 @user_count = User.count
23 21 if params[:page] == 'all'
24 22 @users = User.all
25 23 @paginated = false
26 24 else
27 25 @users = User.paginate :page => params[:page]
28 26 @paginated = true
29 27 end
30 28 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
31 29 @contests = Contest.enabled
32 30 end
33 31
34 32 def active
35 33 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
36 34 @users = []
37 35 sessions.each do |session|
38 36 if session.data[:user_id]
39 37 @users << User.find(session.data[:user_id])
40 38 end
41 39 end
42 40 end
43 41
44 42 def show
45 43 @user = User.find(params[:id])
46 44 end
47 45
48 46 def new
49 47 @user = User.new
50 48 end
51 49
52 50 def create
53 51 @user = User.new(params[:user])
54 52 @user.activated = true
55 53 if @user.save
56 54 flash[:notice] = 'User was successfully created.'
57 55 redirect_to :action => 'list'
58 56 else
59 57 render :action => 'new'
60 58 end
61 59 end
62 60
63 61 def create_from_list
64 62 lines = params[:user_list]
65 63
66 64 note = []
67 65
68 66 lines.split("\n").each do |line|
69 67 items = line.chomp.split(',')
70 68 if items.length>=2
71 69 login = items[0]
72 70 full_name = items[1]
73 71
74 72 added_random_password = false
75 73 if items.length>=3
76 74 password = items[2].chomp(" ")
77 75 user_alias = (items.length>=4) ? items[3] : login
78 76 else
79 77 password = random_password
80 78 user_alias = (items.length>=4) ? items[3] : login
81 79 added_random_password = true
82 80 end
83 81
84 82 user = User.new({:login => login,
85 83 :full_name => full_name,
86 84 :password => password,
87 85 :password_confirmation => password,
88 86 :alias => user_alias})
89 87 user.activated = true
90 88 user.save
91 89
92 90 if added_random_password
93 91 note << "'#{login}' (+)"
94 92 else
95 93 note << login
96 94 end
97 95 end
98 96 end
99 97 flash[:notice] = 'User(s) ' + note.join(', ') +
100 98 ' were successfully created. ' +
101 99 '( (+) - created with random passwords.)'
102 100 redirect_to :action => 'list'
103 101 end
104 102
105 103 def edit
106 104 @user = User.find(params[:id])
107 105 end
108 106
109 107 def update
110 108 @user = User.find(params[:id])
111 109 if @user.update_attributes(params[:user])
112 110 flash[:notice] = 'User was successfully updated.'
113 111 redirect_to :action => 'show', :id => @user
114 112 else
115 113 render :action => 'edit'
116 114 end
117 115 end
118 116
119 117 def destroy
120 118 User.find(params[:id]).destroy
121 119 redirect_to :action => 'list'
122 120 end
123 121
124 122 def user_stat
125 123 @problems = Problem.find_available_problems
126 124 @users = User.find(:all, :include => [:contests, :contest_stat])
127 125 @scorearray = Array.new
128 126 @users.each do |u|
129 127 ustat = Array.new
130 128 ustat[0] = u
131 129 @problems.each do |p|
132 130 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
133 131 if (sub!=nil) and (sub.points!=nil)
134 132 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
135 133 else
136 134 ustat << [0,false]
137 135 end
138 136 end
139 137 @scorearray << ustat
140 138 end
141 139 end
142 140
143 141 def import
144 142 if params[:file]==''
145 143 flash[:notice] = 'Error importing no file'
146 144 redirect_to :action => 'list' and return
147 145 end
148 146 import_from_file(params[:file])
149 147 end
150 148
151 149 def random_all_passwords
152 150 users = User.find(:all)
153 151 @prefix = params[:prefix] || ''
154 152 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
155 153 @changed = false
156 154 if request.request_method == :post
157 155 @non_admin_users.each do |user|
158 156 password = random_password
159 157 user.password = password
160 158 user.password_confirmation = password
161 159 user.save
162 160 end
163 161 @changed = true
164 162 end
165 163 end
166 164
167 165 # contest management
168 166
169 167 def contests
170 168 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
171 169 @contests = Contest.enabled
172 170 end
173 171
174 172 def assign_from_list
175 173 contest_id = params[:users_contest_id]
176 174 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
177 175 contest = Contest.find(params[:new_contest][:id])
178 176 if !contest
179 177 flash[:notice] = 'Error: no contest'
180 178 redirect_to :action => 'contests', :id =>contest_id
181 179 end
182 180
183 181 note = []
184 182 users.each do |u|
185 183 u.contests = [contest]
186 184 note << u.login
187 185 end
188 186 flash[:notice] = 'User(s) ' + note.join(', ') +
189 187 " were successfully reassigned to #{contest.title}."
190 188 redirect_to :action => 'contests', :id =>contest.id
191 189 end
192 190
193 191 def add_to_contest
194 192 user = User.find(params[:id])
195 193 contest = Contest.find(params[:contest_id])
196 194 if user and contest
197 195 user.contests << contest
198 196 end
199 197 redirect_to :action => 'list'
200 198 end
201 199
202 200 def remove_from_contest
203 201 user = User.find(params[:id])
204 202 contest = Contest.find(params[:contest_id])
205 203 if user and contest
206 204 user.contests.delete(contest)
207 205 end
208 206 redirect_to :action => 'list'
209 207 end
210 208
211 209 def contest_management
212 210 end
213 211
214 212 def manage_contest
215 213 contest = Contest.find(params[:contest][:id])
216 214 if !contest
217 215 flash[:notice] = 'You did not choose the contest.'
218 216 redirect_to :action => 'contest_management' and return
219 217 end
220 218
221 219 operation = params[:operation]
222 220
223 221 if not ['add','remove','assign'].include? operation
224 222 flash[:notice] = 'You did not choose the operation to perform.'
225 223 redirect_to :action => 'contest_management' and return
226 224 end
227 225
228 226 lines = params[:login_list]
229 227 if !lines or lines.blank?
230 228 flash[:notice] = 'You entered an empty list.'
231 229 redirect_to :action => 'contest_management' and return
232 230 end
233 231
234 232 note = []
235 233 users = []
236 234 lines.split("\n").each do |line|
237 235 user = User.find_by_login(line.chomp)
238 236 if user
239 237 if operation=='add'
240 238 if ! user.contests.include? contest
241 239 user.contests << contest
242 240 end
243 241 elsif operation=='remove'
244 242 user.contests.delete(contest)
245 243 else
246 244 user.contests = [contest]
247 245 end
248 246
249 247 if params[:reset_timer]
250 248 user.contest_stat.forced_logout = true
251 249 user.contest_stat.reset_timer_and_save
252 250 end
253 251
254 252 if params[:notification_emails]
255 253 send_contest_update_notification_email(user, contest)
256 254 end
257 255
258 256 note << user.login
259 257 users << user
260 258 end
261 259 end
262 260
263 261 if params[:reset_timer]
264 262 logout_users(users)
265 263 end
266 264
267 265 flash[:notice] = 'User(s) ' + note.join(', ') +
268 266 ' were successfully modified. '
269 267 redirect_to :action => 'contest_management'
270 268 end
271 269
272 270 # admin management
273 271
274 272 def admin
275 273 @admins = User.find(:all).find_all {|user| user.admin? }
276 274 end
277 275
278 276 def grant_admin
279 277 login = params[:login]
280 278 user = User.find_by_login(login)
281 279 if user!=nil
282 280 admin_role = Role.find_by_name('admin')
283 281 user.roles << admin_role
284 282 else
285 283 flash[:notice] = 'Unknown user'
286 284 end
287 285 flash[:notice] = 'User added as admins'
288 286 redirect_to :action => 'admin'
289 287 end
290 288
291 289 def revoke_admin
292 290 user = User.find(params[:id])
293 291 if user==nil
294 292 flash[:notice] = 'Unknown user'
295 293 redirect_to :action => 'admin' and return
296 294 elsif user.login == 'root'
297 295 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
298 296 redirect_to :action => 'admin' and return
299 297 end
300 298
301 299 admin_role = Role.find_by_name('admin')
302 300 user.roles.delete(admin_role)
303 301 flash[:notice] = 'User permission revoked'
304 302 redirect_to :action => 'admin'
305 303 end
306 304
307 305 # mass mailing
308 306
309 307 def mass_mailing
310 308 end
311 309
312 310 def bulk_mail
313 311 lines = params[:login_list]
314 312 if !lines or lines.blank?
315 313 flash[:notice] = 'You entered an empty list.'
316 314 redirect_to :action => 'mass_mailing' and return
317 315 end
318 316
319 - subject = params[:subject]
320 - if !subject or subject.blank?
317 + mail_subject = params[:subject]
318 + if !mail_subject or mail_subject.blank?
321 319 flash[:notice] = 'You entered an empty mail subject.'
322 320 redirect_to :action => 'mass_mailing' and return
323 321 end
322 +
323 + mail_body = params[:email_body]
324 + if !mail_body or mail_body.blank?
325 + flash[:notice] = 'You entered an empty mail body.'
326 + redirect_to :action => 'mass_mailing' and return
327 + end
324 328
325 - body = params[:email_body]
326 - if !body or body.blank?
327 - flash[:notice] = 'You entered an empty mail body.'
328 - redirect_to :action => 'mass_mailing' and return
329 - end
329 + admin_email = GraderConfiguration['system.admin_email']
330 330
331 331 note = []
332 332 users = []
333 333 lines.split("\n").each do |line|
334 334 user = User.find_by_login(line.chomp)
335 335 if user
336 - send_mail(user.email, subject, body)
336 + Mail.deliver do
337 + from admin_email
338 + to user.email
339 + subject mail_subject
340 + body mail_body
341 + end
337 342 note << user.login
338 343 end
339 344 end
340 345
341 346 flash[:notice] = 'User(s) ' + note.join(', ') +
342 347 ' were successfully modified. '
343 348 redirect_to :action => 'mass_mailing'
344 349 end
345 350
346 351 protected
347 352
348 353 def random_password(length=5)
349 354 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
350 355 newpass = ""
351 356 length.times { newpass << chars[rand(chars.size-1)] }
352 357 return newpass
353 358 end
354 359
355 360 def import_from_file(f)
356 361 data_hash = YAML.load(f)
357 362 @import_log = ""
358 363
359 364 country_data = data_hash[:countries]
360 365 site_data = data_hash[:sites]
361 366 user_data = data_hash[:users]
362 367
363 368 # import country
364 369 countries = {}
365 370 country_data.each_pair do |id,country|
366 371 c = Country.find_by_name(country[:name])
367 372 if c!=nil
368 373 countries[id] = c
369 374 @import_log << "Found #{country[:name]}\n"
370 375 else
371 376 countries[id] = Country.new(:name => country[:name])
372 377 countries[id].save
373 378 @import_log << "Created #{country[:name]}\n"
374 379 end
375 380 end
376 381
377 382 # import sites
378 383 sites = {}
379 384 site_data.each_pair do |id,site|
380 385 s = Site.find_by_name(site[:name])
381 386 if s!=nil
382 387 @import_log << "Found #{site[:name]}\n"
383 388 else
384 389 s = Site.new(:name => site[:name])
385 390 @import_log << "Created #{site[:name]}\n"
386 391 end
387 392 s.password = site[:password]
388 393 s.country = countries[site[:country_id]]
389 394 s.save
390 395 sites[id] = s
391 396 end
392 397
393 398 # import users
394 399 user_data.each_pair do |id,user|
395 400 u = User.find_by_login(user[:login])
396 401 if u!=nil
397 402 @import_log << "Found #{user[:login]}\n"
398 403 else
399 404 u = User.new(:login => user[:login])
400 405 @import_log << "Created #{user[:login]}\n"
401 406 end
402 407 u.full_name = user[:name]
403 408 u.password = user[:password]
404 409 u.country = countries[user[:country_id]]
405 410 u.site = sites[user[:site_id]]
406 411 u.activated = true
407 412 u.email = "empty-#{u.login}@none.com"
408 413 if not u.save
409 414 @import_log << "Errors\n"
410 415 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
411 416 end
412 417 end
413 418
414 419 end
415 420
416 421 def logout_users(users)
417 422 users.each do |user|
418 423 contest_stat = user.contest_stat(true)
419 424 if contest_stat and !contest_stat.forced_logout
420 425 contest_stat.forced_logout = true
421 426 contest_stat.save
422 427 end
423 428 end
424 429 end
425 430
426 431 def send_contest_update_notification_email(user, contest)
427 432 contest_title_name = GraderConfiguration['contest.name']
428 433 contest_name = contest.name
429 - subject = t('contest.notification.email_subject', {
430 - :contest_title_name => contest_title_name,
431 - :contest_name => contest_name })
432 - body = t('contest.notification.email_body', {
433 - :full_name => user.full_name,
434 - :contest_title_name => contest_title_name,
435 - :contest_name => contest.name,
436 - })
434 + mail_subject = t('contest.notification.email_subject', {
435 + :contest_title_name => contest_title_name,
436 + :contest_name => contest_name })
437 + mail_body = t('contest.notification.email_body', {
438 + :full_name => user.full_name,
439 + :contest_title_name => contest_title_name,
440 + :contest_name => contest.name,
441 + })
437 442
438 - logger.info body
439 - send_mail(user.email, subject, body)
443 + admin_email = GraderConfiguration['system.admin_email']
444 +
445 + logger.info mail_body
446 + Mail.deliver do
447 + from admin_email
448 + to user.email
449 + subject mail_subject
450 + body mail_body
451 + end
440 452 end
441 453
442 454 def find_contest_and_user_from_contest_id(id)
443 455 if id!='none'
444 456 @contest = Contest.find(id)
445 457 else
446 458 @contest = nil
447 459 end
448 460 if @contest
449 461 @users = @contest.users
450 462 else
451 463 @users = User.find_users_with_no_contest
452 464 end
453 465 return [@contest, @users]
454 466 end
455 467 end
@@ -1,158 +1,166
1 - require 'tmail'
2 1 require 'net/smtp'
3 2
4 3 class UsersController < ApplicationController
5 4
6 - #include MailHelperMethods
7 -
8 5 before_filter :authenticate, :except => [:new,
9 6 :register,
10 7 :confirm,
11 8 :forget,
12 9 :retrieve_password]
13 10
14 11 before_filter :verify_online_registration, :only => [:new,
15 12 :register,
16 13 :forget,
17 14 :retrieve_password]
18 15
19 16 verify :method => :post, :only => [:chg_passwd],
20 17 :redirect_to => { :action => :index }
21 18
22 19 #in_place_edit_for :user, :alias_for_editing
23 20 #in_place_edit_for :user, :email_for_editing
24 21
25 22 def index
26 23 if !GraderConfiguration['system.user_setting_enabled']
27 24 redirect_to :controller => 'main', :action => 'list'
28 25 else
29 26 @user = User.find(session[:user_id])
30 27 end
31 28 end
32 29
33 30 def chg_passwd
34 31 user = User.find(session[:user_id])
35 32 user.password = params[:passwd]
36 33 user.password_confirmation = params[:passwd_verify]
37 34 if user.save
38 35 flash[:notice] = 'password changed'
39 36 else
40 37 flash[:notice] = 'Error: password changing failed'
41 38 end
42 39 redirect_to :action => 'index'
43 40 end
44 41
45 42 def new
46 43 @user = User.new
47 44 render :action => 'new', :layout => 'empty'
48 45 end
49 46
50 47 def register
51 48 if(params[:cancel])
52 49 redirect_to :controller => 'main', :action => 'login'
53 50 return
54 51 end
55 52 @user = User.new(params[:user])
56 53 @user.password_confirmation = @user.password = User.random_password
57 54 @user.activated = false
58 55 if (@user.valid?) and (@user.save)
59 56 if send_confirmation_email(@user)
60 57 render :action => 'new_splash', :layout => 'empty'
61 58 else
62 59 @admin_email = GraderConfiguration['system.admin_email']
63 60 render :action => 'email_error', :layout => 'empty'
64 61 end
65 62 else
66 63 @user.errors.add_to_base("Email cannot be blank") if @user.email==''
67 64 render :action => 'new', :layout => 'empty'
68 65 end
69 66 end
70 67
71 68 def confirm
72 69 login = params[:login]
73 70 key = params[:activation]
74 71 @user = User.find_by_login(login)
75 72 if (@user) and (@user.verify_activation_key(key))
76 73 if @user.valid? # check uniquenss of email
77 74 @user.activated = true
78 75 @user.save
79 76 @result = :successful
80 77 else
81 78 @result = :email_used
82 79 end
83 80 else
84 81 @result = :failed
85 82 end
86 83 render :action => 'confirm', :layout => 'empty'
87 84 end
88 85
89 86 def forget
90 87 render :action => 'forget', :layout => 'empty'
91 88 end
92 89
93 90 def retrieve_password
94 91 email = params[:email]
95 92 user = User.find_by_email(email)
96 93 if user
97 94 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
98 95 if last_updated_time > Time.now.gmtime - 5.minutes
99 96 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
100 97 else
101 98 user.password = user.password_confirmation = User.random_password
102 99 user.save
103 100 send_new_password_email(user)
104 101 flash[:notice] = 'New password has been mailed to you.'
105 102 end
106 103 else
107 104 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
108 105 end
109 106 redirect_to :action => 'forget'
110 107 end
111 108
112 109 protected
113 110
114 111 def verify_online_registration
115 112 if !GraderConfiguration['system.online_registration']
116 113 redirect_to :controller => 'main', :action => 'login'
117 114 end
118 115 end
119 116
120 117 def send_confirmation_email(user)
121 118 contest_name = GraderConfiguration['contest.name']
122 119 admin_email = GraderConfiguration['system.admin_email']
123 120 activation_url = url_for(:action => 'confirm',
124 121 :login => user.login,
125 122 :activation => user.activation_key)
126 123 home_url = url_for(:controller => 'main', :action => 'index')
127 - subject = "[#{contest_name}] Confirmation"
128 - body = t('registration.email_body', {
129 - :full_name => user.full_name,
130 - :contest_name => contest_name,
131 - :login => user.login,
132 - :password => user.password,
133 - :activation_url => activation_url,
134 - :admin_email => admin_email
135 - })
124 + mail_subject = "[#{contest_name}] Confirmation"
125 + mail_body = t('registration.email_body', {
126 + :full_name => user.full_name,
127 + :contest_name => contest_name,
128 + :login => user.login,
129 + :password => user.password,
130 + :activation_url => activation_url,
131 + :admin_email => admin_email
132 + })
136 133
137 - logger.info body
134 + logger.info mail_body
138 135
139 - send_mail(user.email, subject, body)
136 + Mail.deliver do
137 + from admin_email
138 + to user.email
139 + subject mail_subject
140 + body mail_body
141 + end
140 142 end
141 143
142 144 def send_new_password_email(user)
143 145 contest_name = GraderConfiguration['contest.name']
144 146 admin_email = GraderConfiguration['system.admin_email']
145 - subject = "[#{contest_name}] Password recovery"
146 - body = t('registration.password_retrieval.email_body', {
147 - :full_name => user.full_name,
148 - :contest_name => contest_name,
149 - :login => user.login,
150 - :password => user.password,
151 - :admin_email => admin_email
152 - })
147 + mail_subject = "[#{contest_name}] Password recovery"
148 + mail_body = t('registration.password_retrieval.email_body', {
149 + :full_name => user.full_name,
150 + :contest_name => contest_name,
151 + :login => user.login,
152 + :password => user.password,
153 + :admin_email => admin_email
154 + })
153 155
154 - logger.info body
155 - send_mail(user.email, subject, body)
156 + logger.info mail_body
157 +
158 + Mail.deliver do
159 + from admin_email
160 + to user.email
161 + subject mail_subject
162 + body mail_body
163 + end
156 164 end
157 165
158 166 end
You need to be logged in to leave comments. Login now