Description:
update mail functionality. Update syntax for mail gem (we might need more options for mail setting?)
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r348:fc78be8c614c - - 2 files changed: 14 inserted, 3 deleted

@@ -1,156 +1,156
1 require 'net/smtp'
1 require 'net/smtp'
2
2
3 class UsersController < ApplicationController
3 class UsersController < ApplicationController
4
4
5 include MailHelperMethods
5 include MailHelperMethods
6
6
7 before_filter :authenticate, :except => [:new,
7 before_filter :authenticate, :except => [:new,
8 :register,
8 :register,
9 :confirm,
9 :confirm,
10 :forget,
10 :forget,
11 :retrieve_password]
11 :retrieve_password]
12
12
13 before_filter :verify_online_registration, :only => [:new,
13 before_filter :verify_online_registration, :only => [:new,
14 :register,
14 :register,
15 :forget,
15 :forget,
16 :retrieve_password]
16 :retrieve_password]
17
17
18 verify :method => :post, :only => [:chg_passwd],
18 verify :method => :post, :only => [:chg_passwd],
19 :redirect_to => { :action => :index }
19 :redirect_to => { :action => :index }
20
20
21 #in_place_edit_for :user, :alias_for_editing
21 #in_place_edit_for :user, :alias_for_editing
22 #in_place_edit_for :user, :email_for_editing
22 #in_place_edit_for :user, :email_for_editing
23
23
24 def index
24 def index
25 if !GraderConfiguration['system.user_setting_enabled']
25 if !GraderConfiguration['system.user_setting_enabled']
26 redirect_to :controller => 'main', :action => 'list'
26 redirect_to :controller => 'main', :action => 'list'
27 else
27 else
28 @user = User.find(session[:user_id])
28 @user = User.find(session[:user_id])
29 end
29 end
30 end
30 end
31
31
32 def chg_passwd
32 def chg_passwd
33 user = User.find(session[:user_id])
33 user = User.find(session[:user_id])
34 user.password = params[:passwd]
34 user.password = params[:passwd]
35 user.password_confirmation = params[:passwd_verify]
35 user.password_confirmation = params[:passwd_verify]
36 if user.save
36 if user.save
37 flash[:notice] = 'password changed'
37 flash[:notice] = 'password changed'
38 else
38 else
39 flash[:notice] = 'Error: password changing failed'
39 flash[:notice] = 'Error: password changing failed'
40 end
40 end
41 redirect_to :action => 'index'
41 redirect_to :action => 'index'
42 end
42 end
43
43
44 def new
44 def new
45 @user = User.new
45 @user = User.new
46 render :action => 'new', :layout => 'empty'
46 render :action => 'new', :layout => 'empty'
47 end
47 end
48
48
49 def register
49 def register
50 if(params[:cancel])
50 if(params[:cancel])
51 redirect_to :controller => 'main', :action => 'login'
51 redirect_to :controller => 'main', :action => 'login'
52 return
52 return
53 end
53 end
54 @user = User.new(params[:user])
54 @user = User.new(params[:user])
55 @user.password_confirmation = @user.password = User.random_password
55 @user.password_confirmation = @user.password = User.random_password
56 @user.activated = false
56 @user.activated = false
57 if (@user.valid?) and (@user.save)
57 if (@user.valid?) and (@user.save)
58 if send_confirmation_email(@user)
58 if send_confirmation_email(@user)
59 render :action => 'new_splash', :layout => 'empty'
59 render :action => 'new_splash', :layout => 'empty'
60 else
60 else
61 @admin_email = GraderConfiguration['system.admin_email']
61 @admin_email = GraderConfiguration['system.admin_email']
62 render :action => 'email_error', :layout => 'empty'
62 render :action => 'email_error', :layout => 'empty'
63 end
63 end
64 else
64 else
65 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
65 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
66 render :action => 'new', :layout => 'empty'
66 render :action => 'new', :layout => 'empty'
67 end
67 end
68 end
68 end
69
69
70 def confirm
70 def confirm
71 login = params[:login]
71 login = params[:login]
72 key = params[:activation]
72 key = params[:activation]
73 @user = User.find_by_login(login)
73 @user = User.find_by_login(login)
74 if (@user) and (@user.verify_activation_key(key))
74 if (@user) and (@user.verify_activation_key(key))
75 if @user.valid? # check uniquenss of email
75 if @user.valid? # check uniquenss of email
76 @user.activated = true
76 @user.activated = true
77 @user.save
77 @user.save
78 @result = :successful
78 @result = :successful
79 else
79 else
80 @result = :email_used
80 @result = :email_used
81 end
81 end
82 else
82 else
83 @result = :failed
83 @result = :failed
84 end
84 end
85 render :action => 'confirm', :layout => 'empty'
85 render :action => 'confirm', :layout => 'empty'
86 end
86 end
87
87
88 def forget
88 def forget
89 render :action => 'forget', :layout => 'empty'
89 render :action => 'forget', :layout => 'empty'
90 end
90 end
91
91
92 def retrieve_password
92 def retrieve_password
93 email = params[:email]
93 email = params[:email]
94 user = User.find_by_email(email)
94 user = User.find_by_email(email)
95 if user
95 if user
96 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
96 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
97 if last_updated_time > Time.now.gmtime - 5.minutes
97 if last_updated_time > Time.now.gmtime - 5.minutes
98 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
98 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
99 else
99 else
100 user.password = user.password_confirmation = User.random_password
100 user.password = user.password_confirmation = User.random_password
101 user.save
101 user.save
102 send_new_password_email(user)
102 send_new_password_email(user)
103 flash[:notice] = 'New password has been mailed to you.'
103 flash[:notice] = 'New password has been mailed to you.'
104 end
104 end
105 else
105 else
106 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
106 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
107 end
107 end
108 redirect_to :action => 'forget'
108 redirect_to :action => 'forget'
109 end
109 end
110
110
111 protected
111 protected
112
112
113 def verify_online_registration
113 def verify_online_registration
114 if !GraderConfiguration['system.online_registration']
114 if !GraderConfiguration['system.online_registration']
115 redirect_to :controller => 'main', :action => 'login'
115 redirect_to :controller => 'main', :action => 'login'
116 end
116 end
117 end
117 end
118
118
119 def send_confirmation_email(user)
119 def send_confirmation_email(user)
120 contest_name = GraderConfiguration['contest.name']
120 contest_name = GraderConfiguration['contest.name']
121 activation_url = url_for(:action => 'confirm',
121 activation_url = url_for(:action => 'confirm',
122 :login => user.login,
122 :login => user.login,
123 :activation => user.activation_key)
123 :activation => user.activation_key)
124 home_url = url_for(:controller => 'main', :action => 'index')
124 home_url = url_for(:controller => 'main', :action => 'index')
125 mail_subject = "[#{contest_name}] Confirmation"
125 mail_subject = "[#{contest_name}] Confirmation"
126 mail_body = t('registration.email_body', {
126 mail_body = t('registration.email_body', {
127 :full_name => user.full_name,
127 :full_name => user.full_name,
128 :contest_name => contest_name,
128 :contest_name => contest_name,
129 :login => user.login,
129 :login => user.login,
130 :password => user.password,
130 :password => user.password,
131 :activation_url => activation_url,
131 :activation_url => activation_url,
132 - :admin_email => admin_email
132 + :admin_email => GraderConfiguration['system.admin_email']
133 })
133 })
134
134
135 logger.info mail_body
135 logger.info mail_body
136
136
137 send_mail(user.email, mail_subject, mail_body)
137 send_mail(user.email, mail_subject, mail_body)
138 end
138 end
139
139
140 def send_new_password_email(user)
140 def send_new_password_email(user)
141 contest_name = GraderConfiguration['contest.name']
141 contest_name = GraderConfiguration['contest.name']
142 mail_subject = "[#{contest_name}] Password recovery"
142 mail_subject = "[#{contest_name}] Password recovery"
143 mail_body = t('registration.password_retrieval.email_body', {
143 mail_body = t('registration.password_retrieval.email_body', {
144 :full_name => user.full_name,
144 :full_name => user.full_name,
145 :contest_name => contest_name,
145 :contest_name => contest_name,
146 :login => user.login,
146 :login => user.login,
147 :password => user.password,
147 :password => user.password,
148 - :admin_email => admin_email
148 + :admin_email => GraderConfiguration['system.admin_email']
149 })
149 })
150
150
151 logger.info mail_body
151 logger.info mail_body
152
152
153 send_mail(user.email, mail_subject, mail_body)
153 send_mail(user.email, mail_subject, mail_body)
154 end
154 end
155
155
156 end
156 end
@@ -1,30 +1,41
1 module MailHelperMethods
1 module MailHelperMethods
2
2
3 def send_mail(mail_to, mail_subject, mail_body)
3 def send_mail(mail_to, mail_subject, mail_body)
4 mail_from = GraderConfiguration['system.online_registration.from']
4 mail_from = GraderConfiguration['system.online_registration.from']
5 smtp_server = GraderConfiguration['system.online_registration.smtp']
5 smtp_server = GraderConfiguration['system.online_registration.smtp']
6
6
7 if ['fake', 'debug'].include? smtp_server
7 if ['fake', 'debug'].include? smtp_server
8 puts "-------------------------
8 puts "-------------------------
9 To: #{mail_to}
9 To: #{mail_to}
10 From: #{mail_from}
10 From: #{mail_from}
11 Subject: #{mail_subject}
11 Subject: #{mail_subject}
12 #{mail_body}
12 #{mail_body}
13 --------------------------
13 --------------------------
14 "
14 "
15 return true
15 return true
16 end
16 end
17
17
18 mail = Mail.new do
18 mail = Mail.new do
19 from mail_from
19 from mail_from
20 to mail_to
20 to mail_to
21 subject mail_subject
21 subject mail_subject
22 body mail_body
22 body mail_body
23 end
23 end
24
24
25 - mail.delivery_settings = { :address => smtp_server }
25 + mail_option = {
26 + :address => smtp_server,
27 + # :domain => nil,
28 + # :port => 25,
29 + # :user_name => nil,
30 + # :password => nil,
31 + # :authentication=>'plain',
32 + # :enable_starttls_auto => true
33 + }
34 +
35 + mail.delivery_method :smtp, mail_option
36 +
26 mail.deliver
37 mail.deliver
27 end
38 end
28
39
29 end
40 end
30
41
You need to be logged in to leave comments. Login now