2722_new_user_notification.patch

Yuichi HARADA, 2019-10-25 07:25

Download (10.9 KB)

View differences:

app/controllers/account_controller.rb
186 186
    user.activate
187 187
    if user.save
188 188
      token.destroy
189
      # Send notification to admins.
190
      Mailer.deliver_activated_to_admins(user)
189 191
      flash[:notice] = l(:notice_account_activated)
190 192
    end
191 193
    redirect_to signin_path
......
337 339
    user.activate
338 340
    user.last_login_on = Time.now
339 341
    if user.save
342
      # Send notification to admins.
343
      Mailer.deliver_activated_to_admins(user)
340 344
      self.logged_user = user
341 345
      flash[:notice] = l(:notice_account_activated)
342 346
      redirect_to my_account_path
app/models/mailer.rb
405 405
  # The email will be sent to the email address specifiedby recipient if provided.
406 406
  #
407 407
  # Exemple:
408
  #   Mailer.deliver_account_activated(user, token)
409
  #   Mailer.deliver_account_activated(user, token, 'foo@example.net')
408
  #   Mailer.deliver_lost_password(user, token)
409
  #   Mailer.deliver_lost_password(user, token, 'foo@example.net')
410 410
  def self.deliver_lost_password(user, token, recipient=nil)
411 411
    lost_password(user, token, recipient).deliver_later
412 412
  end
......
445 445
    register(user, token).deliver_later
446 446
  end
447 447

  
448
  # Builds a mail to admin about user account has activated.
449
  def account_activated_to_admins(admin, user)
450
    @user = user
451
    @url = url_for(:controller => :users, :action => :edit, :id => user.id, :tab => :memberships)
452
    mail :to => admin,
453
      :subject => l(:mail_subject_account_activated, Setting.app_title)
454
  end
455

  
456
  # Sends notification to admins about user account has activated.
457
  #
458
  # Exemple:
459
  #   Mailer.deliver_activated_to_admins(user)
460
  def self.deliver_activated_to_admins(user)
461
    return unless user.active?
462
    return unless Setting.send_notification_to_admins_when_activated?
463
    # Send the email to all active administrators
464
    admins = User.active.where(:admin => true)
465
    admins.each do |admin|
466
      account_activated_to_admins(admin, user).deliver_later
467
    end
468
  end
469

  
448 470
  # Build a mail to user and the additional recipients given in
449 471
  # options[:recipients] about a security related event made by sender.
450 472
  #
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 %>
app/views/settings/_authentication.html.erb
13 13
                                           [l(:label_registration_manual_activation), "2"],
14 14
                                           [l(:label_registration_automatic_activation), "3"]],
15 15
                      :onchange =>
16
                          "if (this.value != '0') { $('#settings_show_custom_fields_on_registration').removeAttr('disabled'); } else { $('#settings_show_custom_fields_on_registration').attr('disabled', true); }" %></p>
16
                        "$('#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'));"
17
%></p>
17 18

  
18 19
<p><%= setting_check_box :show_custom_fields_on_registration,
19 20
                         :disabled => !Setting.self_registration? %></p>
20 21

  
22
<p><%= setting_check_box :send_notification_to_admins_when_activated,
23
                         :disabled => [0, 2].include?(Setting.self_registration.to_i) %></p>
24

  
21 25
<p><%= setting_text_field :password_min_length, :size => 6 %></p>
22 26

  
23 27
<p><%= setting_multiselect :password_required_char_classes, Setting::PASSWORD_CHAR_CLASSES.keys.collect {|c| [l("label_password_char_class_#{c}"), c]} , :inline => true %></p>
......
38 42
    <p><%= setting_select :session_lifetime, session_lifetime_options %></p>
39 43
    <p><%= setting_select :session_timeout, session_timeout_options %></p>
40 44
  </div>
41
  
45

  
42 46
  <p><em class="info"><%= l(:text_session_expiration_settings) %></em></p>
43 47
</fieldset>
44 48

  
config/locales/en.yml
239 239
  mail_body_account_information: Your account information
240 240
  mail_subject_account_activation_request: "%{value} account activation request"
241 241
  mail_body_account_activation_request: "A new user (%{value}) has registered. The account is pending your approval:"
242
  mail_subject_account_activated: "%{value} account activated"
243
  mail_body_account_activated: "A new user (%{value}) has been activated. Please set the roles of the projects:"
242 244
  mail_subject_reminder: "%{count} issue(s) due in the next %{days} days"
243 245
  mail_body_reminder: "%{count} issue(s) that are assigned to you are due in the next %{days} days:"
244 246
  mail_subject_wiki_content_added: "'%{id}' wiki page has been added"
......
396 398
  setting_login_required: Authentication required
397 399
  setting_self_registration: Self-registration
398 400
  setting_show_custom_fields_on_registration: Show custom fields on registration
401
  setting_send_notification_to_admins_when_activated: Send notification to admins when activated
399 402
  setting_attachment_max_size: Maximum attachment size
400 403
  setting_issues_export_limit: Issues export limit
401 404
  setting_mail_from: Emission email address
config/settings.yml
31 31
  security_notifications: 1
32 32
show_custom_fields_on_registration:
33 33
  default: 1
34
send_notification_to_admins_when_activated:
35
  default: 0
34 36
lost_password:
35 37
  default: 1
36 38
  security_notifications: 1
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
......
300 300
    end
301 301
  end
302 302

  
303
  def test_get_activate_with_token_should_send_notification_to_admins
304
    with_settings :send_notification_to_admins_when_activated => '1' do
305
      user = User.generate!(:status => User::STATUS_REGISTERED)
306
      assert !user.active?
307
      token = Token.create(:user => user, :action => 'register')
308

  
309
      ActionMailer::Base.deliveries.clear
310
      get :activate, :params => {
311
          :token => token.value
312
        }
313
      assert_redirected_to '/login'
314
      user.reload
315
      assert user.active?
316
      assert_equal 1, ActionMailer::Base.deliveries.size
317
      mail = ActionMailer::Base.deliveries.last
318
      assert_match /\saccount\sactivated\z/, mail.subject
319
    end
320
  end
321

  
322
  def test_get_activate_with_token_should_not_send_notification_to_admins
323
    with_settings :send_notification_to_admins_when_activated => '0' do
324
      user = User.generate!(:status => User::STATUS_REGISTERED)
325
      assert !user.active?
326
      token = Token.create(:user => user, :action => 'register')
327

  
328
      ActionMailer::Base.deliveries.clear
329
      get :activate, :params => {
330
          :token => token.value
331
        }
332
      assert_redirected_to '/login'
333
      user.reload
334
      assert user.active?
335
      assert_equal 0, ActionMailer::Base.deliveries.size
336
    end
337
  end
338

  
303 339
  # See integration/account_test.rb for the full test
304 340
  def test_post_register_with_registration_on
305
    with_settings :self_registration => '3' do
341
    with_settings :self_registration => '3', :send_notification_to_admins_when_activated => '1' do
342
      ActionMailer::Base.deliveries.clear
306 343
      assert_difference 'User.count' do
307 344
        post :register, :params => {
308 345
            :user => {
......
312 349
              :firstname => 'John',
313 350
              :lastname => 'Doe',
314 351
              :mail => 'register@example.com'
315

  
316 352
            }
317 353
          }
318 354
        assert_redirected_to '/my/account'
319 355
      end
356
      assert_equal 1, ActionMailer::Base.deliveries.size
357
      mail = ActionMailer::Base.deliveries.last
358
      assert_match /\saccount\sactivated\z/, mail.subject
320 359
      user = User.order('id DESC').first
321 360
      assert_equal 'register', user.login
322 361
      assert_equal 'John', user.firstname
test/unit/mailer_test.rb
637 637
    end
638 638
  end
639 639

  
640
  def test_activated_account_should_send_notification_to_admins
641
    with_settings :send_notification_to_admins_when_activated => '1' do
642
      user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
643
      Mailer.deliver_activated_to_admins(user)
644

  
645
      assert_equal 1, ActionMailer::Base.deliveries.size
646
      mail = last_email
647
      assert_match /\saccount\sactivated\z/, mail.subject
648
      assert_equal [User.find_by_login('admin').mail], mail.bcc
649
      assert_select_email do
650
        assert_select 'p', :text => 'A new user (foobar) has been activated. Please set the roles of the projects:'
651
        url = "http://localhost:3000/users/#{user.id}/edit?tab=memberships"
652
        assert_select 'a[href=?]', url, :text => url
653
      end
654
    end
655
  end
656

  
657
  def test_activated_account_should_not_send_notification_to_admins
658
    with_settings :send_notification_to_admins_when_activated => '0' do
659
      user = User.generate!(:login => 'foobar', :status => User::STATUS_ACTIVE)
660
      Mailer.deliver_activated_to_admins(user)
661

  
662
      assert_equal 0, ActionMailer::Base.deliveries.size
663
    end
664
  end
665

  
640 666
  def test_test_email_later
641 667
    user = User.find(1)
642 668
    assert Mailer.test_email(user).deliver_later