2722_new_user_notification-v2.patch

Yuichi HARADA, 2021-04-23 07:10

Download (7.79 KB)

View differences:

app/controllers/account_controller.rb
187 187
    user.activate
188 188
    if user.save
189 189
      token.destroy
190
      # Send notification to admins.
191
      Mailer.deliver_activated_to_admins(user)
190 192
      flash[:notice] = l(:notice_account_activated)
191 193
    end
192 194
    redirect_to signin_path
......
444 446
    user.activate
445 447
    user.last_login_on = Time.now
446 448
    if user.save
449
      # Send notification to admins.
450
      Mailer.deliver_activated_to_admins(user)
447 451
      self.logged_user = user
448 452
      flash[:notice] = l(:notice_account_activated)
449 453
      redirect_to my_account_path
app/models/mailer.rb
456 456
    register(user, token).deliver_later
457 457
  end
458 458

  
459
  # Builds a mail to admin about user account has activated.
460
  def account_activated_to_admins(admin, user)
461
    @user = user
462
    @url = edit_user_url(:id => user.id, :tab => :memberships)
463
    mail :to => admin,
464
      :subject => l(:mail_subject_account_activated, Setting.app_title)
465
  end
466

  
467
  # Sends notification to admins about user account has activated.
468
  #
469
  # Exemple:
470
  #   Mailer.deliver_activated_to_admins(user)
471
  def self.deliver_activated_to_admins(user)
472
    return unless user.active?
473

  
474
    if %w(sent_to_administrators new_user_self_activated).any?{|e| Setting.notified_events.include?(e)}
475
      # Send the email to all active administrators
476
      admins = User.active.where(:admin => true)
477
      admins.each do |admin|
478
        account_activated_to_admins(admin, user).deliver_later
479
      end
480
    end
481
  end
482

  
459 483
  # Build a mail to user and the additional recipients given in
460 484
  # options[:recipients] about a security related event made by sender.
461 485
  #
app/views/mailer/account_activated_to_admins.html.erb
1
<p><%= l(:mail_body_account_activated, h(@user.login)) %></p>
2
<p><%= link_to @url, @url %></p>
app/views/mailer/account_activated_to_admins.text.erb
1
<%= l(:mail_body_account_activated, @user.login) %>
2
<%= @url %>
config/locales/en.yml
254 254
  mail_body_account_information: Your account information
255 255
  mail_subject_account_activation_request: "%{value} account activation request"
256 256
  mail_body_account_activation_request: "A new user (%{value}) has registered. The account is pending your approval:"
257
  mail_subject_account_activated: "%{value} account activated"
258
  mail_body_account_activated: "A new user (%{value}) has been activated. Please set the roles of the projects:"
257 259
  mail_subject_reminder: "%{count} issue(s) due in the next %{days} days"
258 260
  mail_body_reminder: "%{count} issue(s) that are assigned to you are due in the next %{days} days:"
259 261
  mail_subject_wiki_content_added: "'%{id}' wiki page has been added"
......
1109 1111
  label_display_type_board: Board
1110 1112
  label_my_bookmarks: My bookmarks
1111 1113
  label_assign_to_me: Assign to me
1114
  label_sent_to_administrators: Sent to administrators
1115
  label_new_user_self_activated: New user self-activated
1112 1116

  
1113 1117
  button_login: Login
1114 1118
  button_submit: Submit
lib/redmine/notifiable.rb
24 24
      notifications << Notifiable.new('message_posted')
25 25
      notifications << Notifiable.new('wiki_content_added')
26 26
      notifications << Notifiable.new('wiki_content_updated')
27
      notifications << Notifiable.new('sent_to_administrators')
28
      notifications << Notifiable.new('new_user_self_activated', 'sent_to_administrators')
27 29
      notifications
28 30
    end
29 31
  end
test/functional/account_controller_test.rb
20 20
require File.expand_path('../../test_helper', __FILE__)
21 21

  
22 22
class AccountControllerTest < Redmine::ControllerTest
23
  fixtures :users, :email_addresses, :roles
23
  fixtures :users, :email_addresses, :roles, :auth_sources, :tokens
24 24

  
25 25
  def setup
26 26
    User.current = nil
......
327 327
    end
328 328
  end
329 329

  
330
  def test_get_activate_with_token_should_send_notification_to_admins
331
    user = User.generate!(:status => User::STATUS_REGISTERED)
332
    assert !user.active?
333
    token = Token.create(:user => user, :action => 'register')
334
    ActionMailer::Base.deliveries.clear
335
    with_settings :notified_events => %w(new_user_self_activated) do
336
      get :activate, :params => {
337
        :token => token.value
338
      }
339
    end
340
    assert_redirected_to '/login'
341
    user.reload
342
    assert user.active?
343
    assert_equal 1, ActionMailer::Base.deliveries.size
344
    mail = ActionMailer::Base.deliveries.last
345
    assert_match /\saccount\sactivated\z/, mail.subject
346
  end
347

  
348
  def test_get_activate_with_token_should_not_send_notification_to_admins
349
    user = User.generate!(:status => User::STATUS_REGISTERED)
350
    assert !user.active?
351
    token = Token.create(:user => user, :action => 'register')
352
    ActionMailer::Base.deliveries.clear
353
    with_settings :notified_events => [] do
354
      get :activate, :params => {
355
        :token => token.value
356
      }
357
    end
358
    assert_redirected_to '/login'
359
    user.reload
360
    assert user.active?
361
    assert_equal 0, ActionMailer::Base.deliveries.size
362
  end
363

  
330 364
  # See integration/account_test.rb for the full test
331 365
  def test_post_register_with_registration_on
332 366
    with_settings :self_registration => '3' do
test/unit/mailer_test.rb
697 697
    end
698 698
  end
699 699

  
700
  def test_activated_account_should_send_notification_to_admins
701
    with_settings :notified_events => %w(new_user_self_activated) do
702
      user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
703
      Mailer.deliver_activated_to_admins(user)
704

  
705
      assert_equal 1, ActionMailer::Base.deliveries.size
706
      mail = last_email
707
      assert_match /\saccount\sactivated\z/, mail.subject
708
      assert_equal [User.find_by_login('admin').mail], mail.bcc
709
      assert_select_email do
710
        assert_select 'p', :text => 'A new user (foobar) has been activated. Please set the roles of the projects:'
711
        url = "http://localhost:3000/users/#{user.id}/edit?tab=memberships"
712
        assert_select 'a[href=?]', url, :text => url
713
      end
714
    end
715
  end
716

  
717
  def test_activated_account_should_not_send_notification_to_admins
718
    with_settings :notified_events => [] do
719
      user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
720
      Mailer.deliver_activated_to_admins(user)
721

  
722
      assert_equal 0, ActionMailer::Base.deliveries.size
723
    end
724
  end
725

  
700 726
  def test_test_email_later
701 727
    user = User.find(1)
702 728
    assert Mailer.test_email(user).deliver_later