diff --git a/core/modules/user/src/ProfileForm.php b/core/modules/user/src/ProfileForm.php index 343d4d0e06..15c2bc3cac 100644 --- a/core/modules/user/src/ProfileForm.php +++ b/core/modules/user/src/ProfileForm.php @@ -25,6 +25,11 @@ protected function actions(array $form, FormStateInterface $form_state) { $element['delete']['#submit'] = ['::editCancelSubmit']; $element['delete']['#access'] = $account->id() > 1 && (($account->id() == $user->id() && $user->hasPermission('cancel account')) || $user->hasPermission('administer users')); + $element['resend']['#type'] = 'submit'; + $element['resend']['#value'] = $account->isActive() ? t('Resend welcome message') : t('Resend awaiting approval message'); + $element['resend']['#submit'] = array(array($this, 'editResendSubmit')); + $element['resend']['#access'] = $account->getEmail() && $user->hasPermission('administer users'); + return $element; } @@ -57,4 +62,42 @@ public function editCancelSubmit($form, FormStateInterface $form_state) { ); } + /** + * Provides a submit handler for the 'Re-send welcome message' button. + */ + public function editResendSubmit($form, &$form_state) { + global $language; + $account = $this->entity; + + if (!$account->isActive()) { + $op = 'register_pending_approval'; + } + else { + // Determine the user approval method. + $config = \Drupal::getContainer()->get('config.factory')->get('user.settings'); + switch ($config->get('register')) { + case USER_REGISTER_ADMINISTRATORS_ONLY: + $op = 'register_admin_created'; + break; + + case USER_REGISTER_VISITORS: + default: + $op = 'register_no_approval_required'; + } + } + + // Notify the user via email. + $mail = _user_mail_notify($op, $account, $language); + + // Log the mail. + if (!empty($mail)) { + \Drupal::logger('user')->notice('Welcome message has been re-sent to %name at %email.', array('%name' => $account->getUsername(), '%email' => $account->getEmail())); + drupal_set_message(t('Welcome message has been re-sent to %name at %email', array('%name' => $account->getUsername(), '%email' => $account->getEmail()))); + } + else { + \Drupal::logger('user')->notice('There was an error re-sending welcome message to %name at %email', array('%name' => $account->getUsername(), '%email' => $account->getEmail())); + drupal_set_message(t('There was an error re-sending welcome message to %name at %email', array('%name' => $account->getUsername(), '%email' => $account->getEmail())), 'error'); + } + } + } diff --git a/core/modules/user/src/Tests/UserAdminTest.php b/core/modules/user/src/Tests/UserAdminTest.php index f3ee85a97b..c0020cdfb4 100644 --- a/core/modules/user/src/Tests/UserAdminTest.php +++ b/core/modules/user/src/Tests/UserAdminTest.php @@ -197,4 +197,19 @@ public function testNotificationEmailAddress() { $this->assertTrue(count($user_mail), 'New user mail to user is sent from configured Notification Email address'); } + /** + * Tests the resending of an e-mail notification. + */ + public function testResendEmailNotification() { + $admin_user = $this->drupalCreateUser(array('administer users')); + $this->drupalLogin($admin_user); + + $test_user = $this->drupalCreateUser(); + $test_user = user_load($test_user->id()); + $this->drupalPostForm('user/' . $test_user->id() . '/edit', array('status' => 0), t('Resend welcome message')); + + $test_user = user_load($test_user->id()); + $this->assertMail('to', $test_user->getEmail(), 'Activation mail resend to user'); + } + }