Description:
moved send mail code back to helper
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r336:4e59f67ded1a - - 3 files changed: 22 inserted, 50 deleted

@@ -1,26 +1,28
1 1 class UserAdminController < ApplicationController
2 2
3 + include MailHelperMethods
4 +
3 5 before_filter :admin_authorization
4 6
5 7 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
6 8 verify :method => :post, :only => [ :destroy,
7 9 :create, :create_from_list,
8 10 :update,
9 11 :manage_contest,
10 12 :bulk_mail
11 13 ],
12 14 :redirect_to => { :action => :list }
13 15
14 16 def index
15 17 list
16 18 render :action => 'list'
17 19 end
18 20
19 21 def list
20 22 @user_count = User.count
21 23 if params[:page] == 'all'
22 24 @users = User.all
23 25 @paginated = false
24 26 else
25 27 @users = User.paginate :page => params[:page]
26 28 @paginated = true
@@ -305,61 +307,54
305 307 # mass mailing
306 308
307 309 def mass_mailing
308 310 end
309 311
310 312 def bulk_mail
311 313 lines = params[:login_list]
312 314 if !lines or lines.blank?
313 315 flash[:notice] = 'You entered an empty list.'
314 316 redirect_to :action => 'mass_mailing' and return
315 317 end
316 318
317 319 mail_subject = params[:subject]
318 320 if !mail_subject or mail_subject.blank?
319 321 flash[:notice] = 'You entered an empty mail subject.'
320 322 redirect_to :action => 'mass_mailing' and return
321 323 end
322 324
323 325 mail_body = params[:email_body]
324 326 if !mail_body or mail_body.blank?
325 327 flash[:notice] = 'You entered an empty mail body.'
326 328 redirect_to :action => 'mass_mailing' and return
327 329 end
328 330
329 - admin_email = GraderConfiguration['system.admin_email']
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 - Mail.deliver do
337 - from admin_email
338 - to user.email
339 - subject mail_subject
340 - body mail_body
341 - end
336 + send_mail(user.email, mail_subject, mail_body)
342 337 note << user.login
343 338 end
344 339 end
345 340
346 341 flash[:notice] = 'User(s) ' + note.join(', ') +
347 342 ' were successfully modified. '
348 343 redirect_to :action => 'mass_mailing'
349 344 end
350 345
351 346 protected
352 347
353 348 def random_password(length=5)
354 349 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
355 350 newpass = ""
356 351 length.times { newpass << chars[rand(chars.size-1)] }
357 352 return newpass
358 353 end
359 354
360 355 def import_from_file(f)
361 356 data_hash = YAML.load(f)
362 357 @import_log = ""
363 358
364 359 country_data = data_hash[:countries]
365 360 site_data = data_hash[:sites]
@@ -419,49 +414,42
419 414 end
420 415
421 416 def logout_users(users)
422 417 users.each do |user|
423 418 contest_stat = user.contest_stat(true)
424 419 if contest_stat and !contest_stat.forced_logout
425 420 contest_stat.forced_logout = true
426 421 contest_stat.save
427 422 end
428 423 end
429 424 end
430 425
431 426 def send_contest_update_notification_email(user, contest)
432 427 contest_title_name = GraderConfiguration['contest.name']
433 428 contest_name = contest.name
434 429 mail_subject = t('contest.notification.email_subject', {
435 430 :contest_title_name => contest_title_name,
436 431 :contest_name => contest_name })
437 432 mail_body = t('contest.notification.email_body', {
438 433 :full_name => user.full_name,
439 434 :contest_title_name => contest_title_name,
440 435 :contest_name => contest.name,
441 436 })
442 437
443 - admin_email = GraderConfiguration['system.admin_email']
444 -
445 438 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
439 + send_mail(user.email, mail_subject, mail_body)
452 440 end
453 441
454 442 def find_contest_and_user_from_contest_id(id)
455 443 if id!='none'
456 444 @contest = Contest.find(id)
457 445 else
458 446 @contest = nil
459 447 end
460 448 if @contest
461 449 @users = @contest.users
462 450 else
463 451 @users = User.find_users_with_no_contest
464 452 end
465 453 return [@contest, @users]
466 454 end
467 455 end
@@ -1,28 +1,30
1 1 require 'net/smtp'
2 2
3 3 class UsersController < ApplicationController
4 4
5 + include MailHelperMethods
6 +
5 7 before_filter :authenticate, :except => [:new,
6 8 :register,
7 9 :confirm,
8 10 :forget,
9 11 :retrieve_password]
10 12
11 13 before_filter :verify_online_registration, :only => [:new,
12 14 :register,
13 15 :forget,
14 16 :retrieve_password]
15 17
16 18 verify :method => :post, :only => [:chg_passwd],
17 19 :redirect_to => { :action => :index }
18 20
19 21 #in_place_edit_for :user, :alias_for_editing
20 22 #in_place_edit_for :user, :email_for_editing
21 23
22 24 def index
23 25 if !GraderConfiguration['system.user_setting_enabled']
24 26 redirect_to :controller => 'main', :action => 'list'
25 27 else
26 28 @user = User.find(session[:user_id])
27 29 end
28 30 end
@@ -95,72 +97,60
95 97 if last_updated_time > Time.now.gmtime - 5.minutes
96 98 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
97 99 else
98 100 user.password = user.password_confirmation = User.random_password
99 101 user.save
100 102 send_new_password_email(user)
101 103 flash[:notice] = 'New password has been mailed to you.'
102 104 end
103 105 else
104 106 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
105 107 end
106 108 redirect_to :action => 'forget'
107 109 end
108 110
109 111 protected
110 112
111 113 def verify_online_registration
112 114 if !GraderConfiguration['system.online_registration']
113 115 redirect_to :controller => 'main', :action => 'login'
114 116 end
115 117 end
116 118
117 119 def send_confirmation_email(user)
118 120 contest_name = GraderConfiguration['contest.name']
119 - admin_email = GraderConfiguration['system.admin_email']
120 121 activation_url = url_for(:action => 'confirm',
121 122 :login => user.login,
122 123 :activation => user.activation_key)
123 124 home_url = url_for(:controller => 'main', :action => 'index')
124 125 mail_subject = "[#{contest_name}] Confirmation"
125 126 mail_body = t('registration.email_body', {
126 127 :full_name => user.full_name,
127 128 :contest_name => contest_name,
128 129 :login => user.login,
129 130 :password => user.password,
130 131 :activation_url => activation_url,
131 132 :admin_email => admin_email
132 133 })
133 134
134 135 logger.info mail_body
135 136
136 - Mail.deliver do
137 - from admin_email
138 - to user.email
139 - subject mail_subject
140 - body mail_body
141 - end
137 + send_mail(user.email, mail_subject, mail_body)
142 138 end
143 139
144 140 def send_new_password_email(user)
145 141 contest_name = GraderConfiguration['contest.name']
146 - admin_email = GraderConfiguration['system.admin_email']
147 142 mail_subject = "[#{contest_name}] Password recovery"
148 143 mail_body = t('registration.password_retrieval.email_body', {
149 144 :full_name => user.full_name,
150 145 :contest_name => contest_name,
151 146 :login => user.login,
152 147 :password => user.password,
153 148 :admin_email => admin_email
154 149 })
155 150
156 151 logger.info mail_body
157 152
158 - Mail.deliver do
159 - from admin_email
160 - to user.email
161 - subject mail_subject
162 - body mail_body
163 - end
153 + send_mail(user.email, mail_subject, mail_body)
164 154 end
165 155
166 156 end
@@ -1,36 +1,30
1 1 module MailHelperMethods
2 2
3 - def send_mail(to, subject, body)
4 - mail = TMail::Mail.new
5 - mail.to = to
6 - mail.from = Configuration['system.online_registration.from']
7 - mail.subject = subject
8 - mail.body = body
9 -
10 - smtp_server = Configuration['system.online_registration.smtp']
3 + def send_mail(mail_to, mail_subject, mail_body)
4 + mail_from = GraderConfiguration['system.online_registration.from']
5 + smtp_server = GraderConfiguration['system.online_registration.smtp']
11 6
12 7 if ['fake', 'debug'].include? smtp_server
13 8 puts "-------------------------
14 - To: #{mail.to}
15 - From: #{mail.from}
16 - Subject: #{mail.subject}
17 - #{mail.body}
9 + To: #{mail_to}
10 + From: #{mail_from}
11 + Subject: #{mail_subject}
12 + #{mail_body}
18 13 --------------------------
19 14 "
20 15 return true
21 16 end
22 17
23 - begin
24 - Net::SMTP.start(smtp_server) do |smtp|
25 - smtp.send_message(mail.to_s, mail.from, mail.to)
26 - end
27 - result = true
28 - rescue
29 - result = false
18 + mail = Mail.new do
19 + from mail_from
20 + to mail_to
21 + subject mail_subject
22 + body mail_body
30 23 end
31 24
32 - result
25 + mail.delivery_settings = { :address => smtp_server }
26 + mail.deliver
33 27 end
34 28
35 29 end
36 30
You need to be logged in to leave comments. Login now