diff --git a/core/modules/user/config/install/system.action.user_resend_action.yml b/core/modules/user/config/install/system.action.user_resend_action.yml new file mode 100644 index 0000000000..0549f3ce2f --- /dev/null +++ b/core/modules/user/config/install/system.action.user_resend_action.yml @@ -0,0 +1,10 @@ +langcode: en +status: true +dependencies: + module: + - user +id: user_resend_action +label: 'Resend welcome message the selected user(s)' +type: user +plugin: user_resend_action +configuration: { } diff --git a/core/modules/user/config/schema/user.schema.yml b/core/modules/user/config/schema/user.schema.yml index 2f9bda44f2..426d3cda77 100644 --- a/core/modules/user/config/schema/user.schema.yml +++ b/core/modules/user/config/schema/user.schema.yml @@ -150,6 +150,10 @@ action.configuration.user_remove_role_action: type: string label: 'The ID of the role to remove' +action.configuration.user_resend_action: + type: action_configuration_default + label: 'Resend welcome message to selected users configuration' + action.configuration.user_unblock_user_action: type: action_configuration_default label: 'Unblock the selected users configuration' diff --git a/core/modules/user/src/Plugin/Action/ResendWelcomeMessage.php b/core/modules/user/src/Plugin/Action/ResendWelcomeMessage.php new file mode 100644 index 0000000000..af565b7269 --- /dev/null +++ b/core/modules/user/src/Plugin/Action/ResendWelcomeMessage.php @@ -0,0 +1,112 @@ +languageManager = $language_manager; + $this->userSettings = $config_factory->get('user.settings'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('language_manager'), + $container->get('config.factory') + ); + } + + /** + * {@inheritdoc} + */ + public function execute($account = NULL) { + if (empty($account)) { + return; + } + + 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 UserInterface::REGISTER_ADMINISTRATORS_ONLY: + $op = 'register_admin_created'; + break; + + case UserInterface::REGISTER_VISITORS: + default: + $op = 'register_no_approval_required'; + } + } + + _user_mail_notify($op, $account); + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + /** @var \Drupal\user\UserInterface $object */ + $access = $object->status->access('edit', $account, TRUE) + ->andIf($object->access('update', $account, TRUE)); + + return $return_as_object ? $access : $access->isAllowed(); + } + +} diff --git a/core/modules/user/src/ProfileForm.php b/core/modules/user/src/ProfileForm.php index c856b75da9..dadecc24f1 100644 --- a/core/modules/user/src/ProfileForm.php +++ b/core/modules/user/src/ProfileForm.php @@ -3,6 +3,7 @@ namespace Drupal\user; use Drupal\Core\Form\FormStateInterface; +use Drupal\user\UserInterface; /** * Form handler for the profile forms. @@ -27,6 +28,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() ? $this->t('Resend welcome message') : $this->t('Resend awaiting approval message'); + $element['resend']['#submit'] = ['::editResendSubmit']; + $element['resend']['#access'] = $account->getEmail() && $user->hasPermission('administer users'); + return $element; } @@ -59,4 +65,42 @@ public function editCancelSubmit($form, FormStateInterface $form_state) { ); } + /** + * Provides a submit handler for the 'Re-send welcome message' button. + */ + public function editResendSubmit(array $form, FormStateInterface $form_state) { + $account = $this->entity; + $langcode = $this->languageManager->getCurrentLanguage()->getId(); + + 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 UserInterface::REGISTER_ADMINISTRATORS_ONLY: + $op = 'register_admin_created'; + break; + + case UserInterface::REGISTER_VISITORS: + default: + $op = 'register_no_approval_required'; + } + } + + // Notify the user via email. + $mail = _user_mail_notify($op, $account, $langcode); + + // Log the mail. + if (!empty($mail)) { + \Drupal::logger('user')->notice('Welcome message has been re-sent to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]); + drupal_set_message($this->t('Welcome message has been re-sent to %name at %email', ['%name' => $account->getUsername(), '%email' => $account->getEmail()])); + } + else { + \Drupal::logger('user')->notice('There was an error re-sending welcome message to %name at %email', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]); + drupal_set_message($this->t('There was an error re-sending welcome message to %name at %email', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]), 'error'); + } + } + } diff --git a/core/modules/user/tests/src/Functional/UserAdminTest.php b/core/modules/user/tests/src/Functional/UserAdminTest.php index 4e69346d2e..0355fa01fc 100644 --- a/core/modules/user/tests/src/Functional/UserAdminTest.php +++ b/core/modules/user/tests/src/Functional/UserAdminTest.php @@ -202,4 +202,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(['administer users']); + $this->drupalLogin($admin_user); + + $test_user = $this->drupalCreateUser(); + $test_user = user_load($test_user->id()); + $this->drupalPostForm('user/' . $test_user->id() . '/edit', ['status' => 0], t('Resend welcome message')); + + $test_user = user_load($test_user->id()); + $this->assertMail('to', $test_user->getEmail(), 'Activation mail resend to user'); + } + } diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 0af797a43f..2ccb0d7ce3 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -4,6 +4,7 @@ * @file * Install, update and uninstall functions for the user module. */ +use Drupal\system\Entity\Action; /** * Implements hook_schema(). @@ -98,3 +99,17 @@ function user_update_8100() { $config->set('status_blocked', $mail)->save(TRUE); } } + +/** + * Add an action to resend activation emails to multiple users. + */ +function user_update_8600() { + $action = Action::create([ + 'id' => 'user_resend_action', + 'type' => 'user', + 'label' => t('Resend welcome message the selected user(s)'), + 'configuration' => [], + 'plugin' => 'user_resend_action', + ]); + $action->trustData()->save(); +}