OSDN Git Service

Ability to send an email with password when changing a user's password (#3566).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Jul 2009 14:06:14 +0000 (14:06 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Jul 2009 14:06:14 +0000 (14:06 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2813 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/users_controller.rb
app/views/users/_general.rhtml
test/functional/users_controller_test.rb

index 72a4b6c..e9fdfaf 100644 (file)
@@ -79,7 +79,11 @@ class UsersController < ApplicationController
       # 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
+        if was_activated
+          Mailer.deliver_account_activated(@user)
+        elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
+          Mailer.deliver_account_information(@user, params[:password])
+        end
         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 673f09a..9695e4f 100644 (file)
@@ -1,4 +1,7 @@
 <% labelled_tabular_form_for :user, @user, :url => { :controller => 'users', :action => "edit", :tab => nil } do |f| %>
 <%= render :partial => 'form', :locals => { :f => f } %>
 <%= submit_tag l(:button_save) %>
+<% if @user.active? %>
+       <%= check_box_tag 'send_information', 1, true %> <%= l(:label_send_information) %>
+<% end %>
 <% end %>
index e9aad1a..5f62b15 100644 (file)
@@ -107,6 +107,43 @@ class UsersControllerTest < Test::Unit::TestCase
     )
   end
   
+  def test_edit
+    ActionMailer::Base.deliveries.clear
+    post :edit, :id => 2, :user => {:firstname => 'Changed'}
+    assert_equal 'Changed', User.find(2).firstname
+    assert ActionMailer::Base.deliveries.empty?
+  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_edit_with_password_change_should_send_a_notification
+    ActionMailer::Base.deliveries.clear
+    Setting.bcc_recipients = '1'
+    
+    u = User.find(2)
+    post :edit, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1'
+    assert_equal User.hash_password('newpass'), u.reload.hashed_password 
+    
+    mail = ActionMailer::Base.deliveries.last
+    assert_not_nil mail
+    assert_equal [u.mail], mail.bcc
+    assert mail.body.include?('newpass')
+  end
+  
   def test_add_membership_routing
     assert_routing(
       {:method => :post, :path => '/users/123/memberships'},
@@ -128,22 +165,6 @@ class UsersControllerTest < Test::Unit::TestCase
     assert_equal [2], Member.find(1).role_ids
   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