OSDN Git Service

Send an email to the user when an administrator activates a registered user (#2656).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 20 Feb 2009 17:04:47 +0000 (17:04 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 20 Feb 2009 17:04:47 +0000 (17:04 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2484 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/users_controller.rb
app/models/mailer.rb
app/views/mailer/account_activated.text.html.rhtml [new file with mode: 0644]
app/views/mailer/account_activated.text.plain.rhtml [new file with mode: 0644]
test/functional/users_controller_test.rb

index ced17d6..e0b508c 100644 (file)
@@ -75,7 +75,11 @@ class UsersController < ApplicationController
       @user.admin = params[:user][:admin] if params[:user][:admin]
       @user.login = params[:user][:login] if params[:user][:login]
       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
-      if @user.update_attributes(params[:user])
+      @user.attributes = params[:user]
+      # Was the account actived ? (do it before User#save clears the change)
+      was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
+      if @user.save
+        Mailer.deliver_account_activated(@user) if was_activated
         flash[:notice] = l(:notice_successful_update)
         # Give a string to redirect_to otherwise it would use status param as the response code
         redirect_to(url_for(:action => 'list', :status => params[:status], :page => params[:page]))
index c0ff7a8..c8deeff 100644 (file)
@@ -133,6 +133,15 @@ class Mailer < ActionMailer::Base
          :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
   end
 
+  # A registered user's account was activated by an administrator
+  def account_activated(user)
+    set_language_if_valid user.language
+    recipients user.mail
+    subject l(:mail_subject_register, Setting.app_title)
+    body :user => user,
+         :login_url => url_for(:controller => 'account', :action => 'login')
+  end
+
   def lost_password(token)
     set_language_if_valid(token.user.language)
     recipients token.user.mail
diff --git a/app/views/mailer/account_activated.text.html.rhtml b/app/views/mailer/account_activated.text.html.rhtml
new file mode 100644 (file)
index 0000000..6dc9522
--- /dev/null
@@ -0,0 +1,2 @@
+<p><%= l(:notice_account_activated) %></p>
+<p><%= l(:label_login) %>: <%= link_to @login_url, @login_url %></p>
diff --git a/app/views/mailer/account_activated.text.plain.rhtml b/app/views/mailer/account_activated.text.plain.rhtml
new file mode 100644 (file)
index 0000000..4dac4fb
--- /dev/null
@@ -0,0 +1,2 @@
+<%= l(:notice_account_activated) %>
+<%= l(:label_login) %>: <%= @login_url %>
index 42e69f4..5080c13 100644 (file)
@@ -126,6 +126,22 @@ class UsersControllerTest < Test::Unit::TestCase
     assert_equal 2, Member.find(1).role_id
   end
   
+  def test_edit_with_activation_should_send_a_notification
+    u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
+    u.login = 'foo'
+    u.status = User::STATUS_REGISTERED
+    u.save!
+    ActionMailer::Base.deliveries.clear
+    Setting.bcc_recipients = '1'
+    
+    post :edit, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
+    assert u.reload.active?
+    mail = ActionMailer::Base.deliveries.last
+    assert_not_nil mail
+    assert_equal ['foo.bar@somenet.foo'], mail.bcc
+    assert mail.body.include?(ll('fr', :notice_account_activated))
+  end
+  
   def test_destroy_membership
     assert_routing(
     #TODO: use DELETE method on user_membership_path, modify form