ACK - Works great ;-)
--
Benjamin LAN-SUN-LUK
Le 12/11/08 21:22, « Darryl L. Pierce » <mcpierce(a)gmail.com> a écrit :
An email is dispatched to the user's *old* address when their address is
changed.
Signed-off-by: Darryl L. Pierce <mcpierce(a)gmail.com>
---
app/controllers/users_controller.rb | 3 +++
app/models/user_mailer.rb | 8 ++++++++
.../user_mailer/email_change_notification.html.erb | 19 +++++++++++++++++++
test/functional/users_controller_test.rb | 18 ++++++++++++++++--
4 files changed, 46 insertions(+), 2 deletions(-)
create mode 100644 app/views/user_mailer/email_change_notification.html.erb
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index ddda50d..76c5525 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -84,9 +84,12 @@ class UsersController < ApplicationController
def update
respond_to do |format|
if @this_user.can_edit?(@user)
+ old_email = @this_user.email
@this_user.update_attributes(params[:user])
if @this_user.save
+ UserMailer.deliver_email_change_notification(@this_user,old_email) unless
old_email == @this_user.email
+
flash[:message] = "User has been updated."
format.html { redirect_to user_path }
else
diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb
index 808c6e5..993d0f8 100644
--- a/app/models/user_mailer.rb
+++ b/app/models/user_mailer.rb
@@ -24,6 +24,14 @@ class UserMailer < ActionMailer::Base
body :user => user, :token => token
end
+ # Sendsd an email to the old address when the user changes his email address.
+ def email_change_notification(user, old_email)
+ recipients old_email
+ from MAIL_CONFIG[:from]
+ subject "Email change notification"
+ body :user => user, :old_email => old_email
+ end
+
# Send an e-mail to an user and notify him his new password.
def new_generated_password(user, new_password)
recipients user.email
diff --git a/app/views/user_mailer/email_change_notification.html.erb
b/app/views/user_mailer/email_change_notification.html.erb
new file mode 100644
index 0000000..e039372
--- /dev/null
+++ b/app/views/user_mailer/email_change_notification.html.erb
@@ -0,0 +1,19 @@
+Dear <%= @user.display_name %>,
+
+This email is to inform you that your old email address:
+
+<%= @old_email %>
+
+has been replaced with the following address:
+
+<%= @user.email %>
+
+You will need to use this new email address in future to log in.
+
+To review your account, please go to: <%= user_url @user %>
+
+Thank you.
+
+--
+Sent to you by ProjXP.
+<%= root_url %>
\ No newline at end of file
diff --git a/test/functional/users_controller_test.rb
b/test/functional/users_controller_test.rb
index 6551501..65ecb70 100644
--- a/test/functional/users_controller_test.rb
+++ b/test/functional/users_controller_test.rb
@@ -189,23 +189,37 @@ class UsersControllerTest < ActionController::TestCase
# Ensures admins can update a user's account.
def test_update_as_admin
put :update,
- {:id => @user.id, :user => {:email => "test(a)fodder.com"}},
+ {:id => @user.id, :user => {:display_name => "Don Farkle"}},
{:user_id => @admin.id}
assert_redirected_to user_path(@user)
- assert_equal "test(a)fodder.com", User.find_by_id((a)user.id).email,
+ assert_equal "Don Farkle", User.find_by_id((a)user.id).display_name,
"User should have been updated."
end
# Ensures users can update their accounts.
def test_update
put :update,
+ {:id => @user.id, :user => {:display_name => "Joe Farkles"}},
+ {:user_id => @user.id}
+
+ assert_redirected_to user_path(@user)
+ assert_equal "Joe Farkles", User.find_by_id((a)user.id).display_name,
+ "User should have been updated."
+ end
+
+ # Ensures an email is sent to the user if they change their email.
+ def test_update_with_email_change
+ put :update,
{:id => @user.id, :user => {:email => "test(a)fodder.com"}},
{:user_id => @user.id}
assert_redirected_to user_path(@user)
assert_equal "test(a)fodder.com", User.find_by_id((a)user.id).email,
"User should have been updated."
+ email = @emails.first
+ assert email, "No email was generated."
+ assert_equal "test(a)fodder.com", email.to[0], "Email sent to wrong
user."
end
# Ensures that a valid user must be supplied for a backlog.
--
1.5.6.5
_______________________________________________
projxp-devel mailing list
projxp-devel(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/projxp-devel