diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index ff8631e90..7c75016f2 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -186,6 +186,8 @@ class AccountController < ApplicationController user.activate if user.save token.destroy + # Send notification to admins. + Mailer.deliver_activated_to_admins(user) flash[:notice] = l(:notice_account_activated) end redirect_to signin_path @@ -337,6 +339,8 @@ class AccountController < ApplicationController user.activate user.last_login_on = Time.now if user.save + # Send notification to admins. + Mailer.deliver_activated_to_admins(user) self.logged_user = user flash[:notice] = l(:notice_account_activated) redirect_to my_account_path diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 48bf0eed4..a09f73044 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -405,8 +405,8 @@ class Mailer < ActionMailer::Base # The email will be sent to the email address specifiedby recipient if provided. # # Exemple: - # Mailer.deliver_account_activated(user, token) - # Mailer.deliver_account_activated(user, token, 'foo@example.net') + # Mailer.deliver_lost_password(user, token) + # Mailer.deliver_lost_password(user, token, 'foo@example.net') def self.deliver_lost_password(user, token, recipient=nil) lost_password(user, token, recipient).deliver_later end @@ -445,6 +445,28 @@ class Mailer < ActionMailer::Base register(user, token).deliver_later end + # Builds a mail to admin about user account has activated. + def account_activated_to_admins(admin, user) + @user = user + @url = url_for(:controller => :users, :action => :edit, :id => user.id, :tab => :memberships) + mail :to => admin, + :subject => l(:mail_subject_account_activated, Setting.app_title) + end + + # Sends notification to admins about user account has activated. + # + # Exemple: + # Mailer.deliver_activated_to_admins(user) + def self.deliver_activated_to_admins(user) + return unless user.active? + return unless Setting.send_notification_to_admins_when_activated? + # Send the email to all active administrators + admins = User.active.where(:admin => true) + admins.each do |admin| + account_activated_to_admins(admin, user).deliver_later + end + end + # Build a mail to user and the additional recipients given in # options[:recipients] about a security related event made by sender. # diff --git a/app/views/mailer/account_activated_to_admins.html.erb b/app/views/mailer/account_activated_to_admins.html.erb new file mode 100644 index 000000000..87aedd52f --- /dev/null +++ b/app/views/mailer/account_activated_to_admins.html.erb @@ -0,0 +1,2 @@ +

<%= l(:mail_body_account_activated, h(@user.login)) %>

+

<%= link_to @url, @url %>

\ No newline at end of file diff --git a/app/views/mailer/account_activated_to_admins.text.erb b/app/views/mailer/account_activated_to_admins.text.erb new file mode 100644 index 000000000..7b8b9d04e --- /dev/null +++ b/app/views/mailer/account_activated_to_admins.text.erb @@ -0,0 +1,2 @@ +<%= l(:mail_body_account_activated, @user.login) %> +<%= @url %> \ No newline at end of file diff --git a/app/views/settings/_authentication.html.erb b/app/views/settings/_authentication.html.erb index 9a39497b8..85eff9e76 100644 --- a/app/views/settings/_authentication.html.erb +++ b/app/views/settings/_authentication.html.erb @@ -13,11 +13,15 @@ [l(:label_registration_manual_activation), "2"], [l(:label_registration_automatic_activation), "3"]], :onchange => - "if (this.value != '0') { $('#settings_show_custom_fields_on_registration').removeAttr('disabled'); } else { $('#settings_show_custom_fields_on_registration').attr('disabled', true); }" %>

+ "$('#settings_show_custom_fields_on_registration').prop('disabled', (this.value == '0')); $('#settings_send_notification_to_admins_when_activated').prop('disabled', (this.value == '0' || this.value == '2'));" +%>

<%= setting_check_box :show_custom_fields_on_registration, :disabled => !Setting.self_registration? %>

+

<%= setting_check_box :send_notification_to_admins_when_activated, + :disabled => [0, 2].include?(Setting.self_registration.to_i) %>

+

<%= setting_text_field :password_min_length, :size => 6 %>

<%= setting_multiselect :password_required_char_classes, Setting::PASSWORD_CHAR_CLASSES.keys.collect {|c| [l("label_password_char_class_#{c}"), c]} , :inline => true %>

@@ -38,7 +42,7 @@

<%= setting_select :session_lifetime, session_lifetime_options %>

<%= setting_select :session_timeout, session_timeout_options %>

- +

<%= l(:text_session_expiration_settings) %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index ba4b4b5ee..79c8480b5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -239,6 +239,8 @@ en: mail_body_account_information: Your account information mail_subject_account_activation_request: "%{value} account activation request" mail_body_account_activation_request: "A new user (%{value}) has registered. The account is pending your approval:" + mail_subject_account_activated: "%{value} account activated" + mail_body_account_activated: "A new user (%{value}) has been activated. Please set the roles of the projects:" mail_subject_reminder: "%{count} issue(s) due in the next %{days} days" mail_body_reminder: "%{count} issue(s) that are assigned to you are due in the next %{days} days:" mail_subject_wiki_content_added: "'%{id}' wiki page has been added" @@ -396,6 +398,7 @@ en: setting_login_required: Authentication required setting_self_registration: Self-registration setting_show_custom_fields_on_registration: Show custom fields on registration + setting_send_notification_to_admins_when_activated: Send notification to admins when activated setting_attachment_max_size: Maximum attachment size setting_issues_export_limit: Issues export limit setting_mail_from: Emission email address diff --git a/config/settings.yml b/config/settings.yml index 05081c0fa..fcaed2ffc 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -31,6 +31,8 @@ self_registration: security_notifications: 1 show_custom_fields_on_registration: default: 1 +send_notification_to_admins_when_activated: + default: 0 lost_password: default: 1 security_notifications: 1 diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 0ad5611b8..c025a5684 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -20,7 +20,7 @@ require File.expand_path('../../test_helper', __FILE__) class AccountControllerTest < Redmine::ControllerTest - fixtures :users, :email_addresses, :roles + fixtures :users, :email_addresses, :roles, :auth_sources, :tokens def setup User.current = nil @@ -300,9 +300,46 @@ class AccountControllerTest < Redmine::ControllerTest end end + def test_get_activate_with_token_should_send_notification_to_admins + with_settings :send_notification_to_admins_when_activated => '1' do + user = User.generate!(:status => User::STATUS_REGISTERED) + assert !user.active? + token = Token.create(:user => user, :action => 'register') + + ActionMailer::Base.deliveries.clear + get :activate, :params => { + :token => token.value + } + assert_redirected_to '/login' + user.reload + assert user.active? + assert_equal 1, ActionMailer::Base.deliveries.size + mail = ActionMailer::Base.deliveries.last + assert_match /\saccount\sactivated\z/, mail.subject + end + end + + def test_get_activate_with_token_should_not_send_notification_to_admins + with_settings :send_notification_to_admins_when_activated => '0' do + user = User.generate!(:status => User::STATUS_REGISTERED) + assert !user.active? + token = Token.create(:user => user, :action => 'register') + + ActionMailer::Base.deliveries.clear + get :activate, :params => { + :token => token.value + } + assert_redirected_to '/login' + user.reload + assert user.active? + assert_equal 0, ActionMailer::Base.deliveries.size + end + end + # See integration/account_test.rb for the full test def test_post_register_with_registration_on - with_settings :self_registration => '3' do + with_settings :self_registration => '3', :send_notification_to_admins_when_activated => '1' do + ActionMailer::Base.deliveries.clear assert_difference 'User.count' do post :register, :params => { :user => { @@ -312,11 +349,13 @@ class AccountControllerTest < Redmine::ControllerTest :firstname => 'John', :lastname => 'Doe', :mail => 'register@example.com' - } } assert_redirected_to '/my/account' end + assert_equal 1, ActionMailer::Base.deliveries.size + mail = ActionMailer::Base.deliveries.last + assert_match /\saccount\sactivated\z/, mail.subject user = User.order('id DESC').first assert_equal 'register', user.login assert_equal 'John', user.firstname diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb index 486131a8f..a70885ca0 100644 --- a/test/unit/mailer_test.rb +++ b/test/unit/mailer_test.rb @@ -637,6 +637,32 @@ class MailerTest < ActiveSupport::TestCase end end + def test_activated_account_should_send_notification_to_admins + with_settings :send_notification_to_admins_when_activated => '1' do + user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE) + Mailer.deliver_activated_to_admins(user) + + assert_equal 1, ActionMailer::Base.deliveries.size + mail = last_email + assert_match /\saccount\sactivated\z/, mail.subject + assert_equal [User.find_by_login('admin').mail], mail.bcc + assert_select_email do + assert_select 'p', :text => 'A new user (foobar) has been activated. Please set the roles of the projects:' + url = "http://localhost:3000/users/#{user.id}/edit?tab=memberships" + assert_select 'a[href=?]', url, :text => url + end + end + end + + def test_activated_account_should_not_send_notification_to_admins + with_settings :send_notification_to_admins_when_activated => '0' do + user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE) + Mailer.deliver_activated_to_admins(user) + + assert_equal 0, ActionMailer::Base.deliveries.size + end + end + def test_test_email_later user = User.find(1) assert Mailer.test_email(user).deliver_later