diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index c6d13d417..90088a208 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -187,6 +187,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 @@ -444,6 +446,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 f58a1c88d..28505f96e 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -456,6 +456,30 @@ 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 = edit_user_url(: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? + + if %w(sent_to_administrators new_user_self_activated).any?{|e| Setting.notified_events.include?(e)} + # 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 + 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/config/locales/en.yml b/config/locales/en.yml index 9d779a2fe..bf3f42cf1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -254,6 +254,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" @@ -1109,6 +1111,8 @@ en: label_display_type_board: Board label_my_bookmarks: My bookmarks label_assign_to_me: Assign to me + label_sent_to_administrators: Sent to administrators + label_new_user_self_activated: New user self-activated button_login: Login button_submit: Submit diff --git a/lib/redmine/notifiable.rb b/lib/redmine/notifiable.rb index dbb5de335..440551877 100644 --- a/lib/redmine/notifiable.rb +++ b/lib/redmine/notifiable.rb @@ -24,6 +24,8 @@ module Redmine notifications << Notifiable.new('message_posted') notifications << Notifiable.new('wiki_content_added') notifications << Notifiable.new('wiki_content_updated') + notifications << Notifiable.new('sent_to_administrators') + notifications << Notifiable.new('new_user_self_activated', 'sent_to_administrators') notifications end end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index d9b227121..2e6494bbc 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 @@ -327,6 +327,40 @@ class AccountControllerTest < Redmine::ControllerTest end end + def test_get_activate_with_token_should_send_notification_to_admins + user = User.generate!(:status => User::STATUS_REGISTERED) + assert !user.active? + token = Token.create(:user => user, :action => 'register') + ActionMailer::Base.deliveries.clear + with_settings :notified_events => %w(new_user_self_activated) do + get :activate, :params => { + :token => token.value + } + end + 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 + + def test_get_activate_with_token_should_not_send_notification_to_admins + user = User.generate!(:status => User::STATUS_REGISTERED) + assert !user.active? + token = Token.create(:user => user, :action => 'register') + ActionMailer::Base.deliveries.clear + with_settings :notified_events => [] do + get :activate, :params => { + :token => token.value + } + end + assert_redirected_to '/login' + user.reload + assert user.active? + assert_equal 0, ActionMailer::Base.deliveries.size + end + # See integration/account_test.rb for the full test def test_post_register_with_registration_on with_settings :self_registration => '3' do diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb index f31a35015..f240a0e20 100644 --- a/test/unit/mailer_test.rb +++ b/test/unit/mailer_test.rb @@ -697,6 +697,32 @@ class MailerTest < ActiveSupport::TestCase end end + def test_activated_account_should_send_notification_to_admins + with_settings :notified_events => %w(new_user_self_activated) 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 :notified_events => [] 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