Project

General

Profile

Feature #2722 » 2722_new_user_notification-v2.patch

Yuichi HARADA, 2021-04-23 07:10

View differences:

app/controllers/account_controller.rb
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
......
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
app/models/mailer.rb
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.
#
app/views/mailer/account_activated_to_admins.html.erb
<p><%= l(:mail_body_account_activated, h(@user.login)) %></p>
<p><%= link_to @url, @url %></p>
app/views/mailer/account_activated_to_admins.text.erb
<%= l(:mail_body_account_activated, @user.login) %>
<%= @url %>
config/locales/en.yml
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"
......
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
lib/redmine/notifiable.rb
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
test/functional/account_controller_test.rb
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
......
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
test/unit/mailer_test.rb
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
(4-4/5)