diff -u b/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php --- b/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -3,6 +3,7 @@ namespace Drupal\user; use Drupal\Component\Datetime\TimeInterface; +use Drupal\Core\Database\Query\Condition; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityConstraintViolationListInterface; use Drupal\Core\Entity\EntityRepositoryInterface; @@ -16,7 +17,7 @@ use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser; use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\Database\Database; +use Drupal\Core\Database\Connection; /** * Form controller for the user account forms. @@ -31,6 +32,13 @@ protected $languageManager; /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** * Constructs a new EntityForm object. * * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository @@ -41,10 +49,13 @@ * The entity type bundle service. * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. + * @param \Drupal\Core\Database\Connection $database + * The database connection. */ - public function __construct(EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + public function __construct(EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, Connection $database) { parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->languageManager = $language_manager; + $this->database = $database; } /** @@ -55,7 +66,8 @@ $container->get('entity.repository'), $container->get('language_manager'), $container->get('entity_type.bundle.info'), - $container->get('datetime.time') + $container->get('datetime.time'), + $container->get('database') ); } @@ -69,7 +81,7 @@ $config = \Drupal::config('user.settings'); $form['#cache']['tags'] = $config->getCacheTags(); - $language_interface = \Drupal::languageManager()->getCurrentLanguage(); + $language_interface = $this->languageManager->getCurrentLanguage(); // Check for new account. $register = $account->isAnonymous(); @@ -449,8 +461,7 @@ $account = $this->entity; $name = $form_state->getValue('name'); $mail = $form_state->getValue('mail'); - // Get a list of files in the database for this directory. - $database = Database::getConnection(); + $database = $this->database; // For new registrations, make sure the username does not conflict with // an existing user's email address. @@ -460,8 +471,8 @@ // For existing users whose username matches another user's email address // are not forced to update their username. if ($account->isAuthenticated()) { - $name_taken = (bool) $database->select('users_field_data', 'ufd') - ->condition('ufd.name', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') + $name_taken = (bool) $this->database->select('users_field_data', 'ufd') + ->condition('ufd.name', $database->escapeLike($name), 'LIKE') ->condition('ufd.uid', $account->id(), '<>') ->range(0, 1) ->countQuery() @@ -469,11 +480,11 @@ ->fetchField(); } else { - $name_taken = (bool) $database->select('users_field_data', 'ufd') + $name_taken = (bool) $this->database->select('users_field_data', 'ufd') ->condition( - db_or() - ->condition('ufd.name', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') - ->condition('ufd.mail', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') + $condition = (new Condition('OR')) + ->condition('ufd.name', $database->escapeLike($name), 'LIKE') + ->condition('ufd.mail', $database->escapeLike($name), 'LIKE') ) ->condition('ufd.status', 1) ->range(0, 1) @@ -495,8 +506,8 @@ // For existing users whose email matches another user's username are not // forced to update their email address. if ($account->isAuthenticated()) { - $mail_taken = (bool) $database->select('users_field_data', 'ufd') - ->condition('ufd.mail', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') + $mail_taken = (bool) \Drupal::database()->select('users_field_data', 'ufd') + ->condition('ufd.mail', $database->escapeLike($mail), 'LIKE') ->condition('ufd.uid', $account->id(), '<>') ->range(0, 1) ->countQuery() @@ -504,11 +515,11 @@ ->fetchField(); } else { - $mail_taken = (bool) $database->select('users_field_data', 'ufd') + $mail_taken = (bool) \Drupal::database()->select('users_field_data', 'ufd') ->condition( - db_or() - ->condition('ufd.name', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') - ->condition('ufd.mail', '%' . $database->escapeLike($search_phrase) . '%', 'LIKE') + $condition = (new Condition('OR')) + ->condition('ufd.mail', $database->escapeLike($mail), 'LIKE') + ->condition('ufd.name', $database->escapeLike($mail), 'LIKE') ) ->condition('ufd.status', 1) ->range(0, 1) diff -u b/core/modules/user/src/Form/UserPasswordForm.php b/core/modules/user/src/Form/UserPasswordForm.php --- b/core/modules/user/src/Form/UserPasswordForm.php +++ b/core/modules/user/src/Form/UserPasswordForm.php @@ -16,8 +16,6 @@ use Drupal\Core\Site\Settings; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Url; -use Drupal\user\UserInterface; -use Drupal\Core\Messenger\MessengerInterface; /** * Provides a user password reset form. @@ -128,7 +126,8 @@ '#type' => 'textfield', '#title' => $this->t('Username or email address'), '#size' => 60, - '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH), + '#maxlength' => max(USERNAME_MAX_LENGTH, + Email::EMAIL_MAX_LENGTH), '#required' => TRUE, '#attributes' => [ 'autocorrect' => 'off', @@ -248,7 +247,7 @@ * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $language_interface = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE); + $language_interface = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE); if ($form_state->getValue('step') == 1) { $accounts = $form_state->getValue('accounts'); @@ -277,14 +276,14 @@ } if (isset($account)) { // Mail one-time login URL and instructions using current language. - $mail = _user_mail_notify('password_reset', $account); + $mail = _user_mail_notify('password_reset', $account, $language_interface->getId()); if (!empty($mail)) { $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', [ ' %name' => $account->name, '%email' => $account->mail, ]); - \Drupal::messenger()->addMessage($this->t('Further instructions have been sent to your e-mail address.')); + $this->messenger($this->t('Further instructions have been sent to your e-mail address.')); } $form_state->setRedirectUrl(Url::fromRoute('user.page')); diff -u b/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php --- b/core/modules/user/tests/src/Functional/UserEditTest.php +++ b/core/modules/user/tests/src/Functional/UserEditTest.php @@ -9,6 +9,8 @@ * Tests user edit page. * * @group user + * @method drupalPostForm(string $string, array $array, \Drupal\Core\StringTranslation\TranslatableMarkup $t) + * @method assertText(\Drupal\Core\StringTranslation\TranslatableMarkup $t, string $string) */ class UserEditTest extends BrowserTestBase { @@ -93,7 +95,7 @@ $this->assertSame(1, (int) \Drupal::database()->select('sessions', 's')->countQuery()->execute()->fetchField()); // Make sure the changed timestamp is updated. - $this->assertEquals(REQUEST_TIME, $user1->getChangedTime(), 'Changing a user sets "changed" timestamp.'); + $this->assertEquals(\Drupal::time()->getRequestTime(), $user1->getChangedTime(), 'Changing a user sets "changed" timestamp.'); // Make sure the user can log in with their new password. $this->drupalLogout(); @@ -265,14 +267,14 @@ // Test that the first user can save their account with no errors. $this->drupalLogin($user_with_email); - $this->submitForm("user/" . $user_with_email->id() . "/edit", [], t('Save')); - $this->assertSession()->responseContains(t("The changes have been saved."), "The user does not need to change their username if it matches another user's email address."); + $this->drupalPostForm("user/" . $user_with_email->id() . "/edit", [], t('Save')); + $this->assertText(t("The changes have been saved."), "The user does not need to change their username if it matches another user's email address."); $this->drupalLogout(); // Test that the second user can save their account with no errors. $this->drupalLogin($user_with_name); - $this->submitForm("user/" . $user_with_name->id() . "/edit", [], t('Save')); - $this->assertSession()->responseContains(t("The changes have been saved."), "The user does not need to change their email address if it matches another user's username."); + $this->drupalPostForm("user/" . $user_with_name->id() . "/edit", [], t('Save')); + $this->assertText(t("The changes have been saved."), "The user does not need to change their email address if it matches another user's username."); } } diff -u b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php --- b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php +++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php @@ -16,6 +16,12 @@ * Ensure that password reset methods work as expected. * * @group user + * @method drupalPostForm(string $string, array $edit, \Drupal\Core\StringTranslation\TranslatableMarkup $t) + * @method assertField(string $string, string $string1) + * @method assertNoText(string|null $getEmail, string $string) + * @method assertText(\Drupal\Core\StringTranslation\TranslatableMarkup $t, string $string) + * @method assertEqual(int|void $count, int $int, string $string) + * @method assertNoField(string $string, string $string1) */ class UserPasswordResetTest extends BrowserTestBase { @@ -73,7 +79,7 @@ // Set the last login time that is used to generate the one-time link so // that it is definitely over a second ago. - $account->login = REQUEST_TIME - mt_rand(10, 100000); + $account->login = \Drupal::time()->getRequestTime() - mt_rand(10, 100000); Database::getConnection()->update('users_field_data') ->fields(['login' => $account->getLastLoginTime()]) ->condition('uid', $account->id()) @@ -179,7 +185,7 @@ // request time was 60 seconds older than // the allowed limit. $timeout = $this->config('user.settings')->get('password_reset_timeout'); - $bogus_timestamp = REQUEST_TIME - $timeout - 60; + $bogus_timestamp = \Drupal::time()->getRequestTime() - $timeout - 60; $_uid = $this->account->id(); $this->drupalGet("user/reset/$_uid/$bogus_timestamp/" . user_pass_rehash($this->account, $bogus_timestamp)); $this->assertSession()->pageTextContains('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'); @@ -187,7 +193,7 @@ $this->assertSession()->pageTextContains('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'); // Create a user, block the account, and verify that a login link is denied. - $timestamp = REQUEST_TIME - 1; + $timestamp = \Drupal::time()->getRequestTime() - 1; $blocked_account = $this->drupalCreateUser()->block(); $blocked_account->save(); $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp)); @@ -229,7 +235,7 @@ // Ensure blocked and deleted accounts can't access the user.reset.login // route. $this->drupalLogout(); - $timestamp = REQUEST_TIME - 1; + $timestamp = \Drupal::time()->getRequestTime() - 1; $blocked_account = $this->drupalCreateUser()->block(); $blocked_account->save(); $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp) . '/login'); @@ -250,7 +256,7 @@ $user_settings->set('verify_mail', FALSE)->save(); // Don't require admin approval for new accounts. - $user_settings->set('register', REGISTER_VISITORS)->save(); + $user_settings->set('register', USER_REGISTER_VISITORS)->save(); // Create two users. $user_with_email = $this->drupalCreateUser(); $user_with_name = $this->drupalCreateUser(); @@ -263,26 +269,26 @@ // Try and reset based on the duplicated email. $edit = []; $edit['name'] = $user_with_email->getEmail(); - $this->submitForm('user/password', $edit, t('Submit')); + $this->drupalPostForm('user/password', $edit, t('Submit')); // There should be a field prompting the user to pick and account. - $this->assertSession()->fieldExists('choose_account', 'User is prompted to pick an account when email matches two accounts.'); + $this->assertField('choose_account', 'User is prompted to pick an account when email matches two accounts.'); // We should be sure to not expose another user's email to the user. - $this->assertSession()->pageTextNotContains($user_with_name->getEmail(), "Duplicated user's email is not exposed to the other user."); + $this->assertNoText($user_with_name->getEmail(), "Duplicated user's email is not exposed to the other user."); // Select the account with the username matching the entered email. $edit = []; $edit['choose_account'] = Crypt::hashBase64(Settings::getHashSalt() . $user_with_email->id()); - $this->submitForm(NULL, $edit, t('Submit')); - $this->assertSession()->pageTextContains(t('Further instructions have been sent to your e-mail address.'), 'User is notified that password reset was sent.'); + $this->drupalPostForm(NULL, $edit, t('Submit')); + $this->assertText(t('Further instructions have been sent to your e-mail address.'), 'User is notified that password reset was sent.'); // Make sure that right user was sent a reset email. - $this->assertEquals(count($this->drupalGetMails([ + $this->assertEqual(count($this->drupalGetMails([ 'key' => 'password_reset', 'to' => $user_with_email->getEmail(), ] )), 1, 'The right user was sent a password reset mail.'); // Make sure that the other user was not sent an email. - $this->assertEquals(count($this->drupalGetMails([ + $this->assertEqual(count($this->drupalGetMails([ 'key' => 'password_reset', 'to' => $user_with_name->getEmail(), ])), 0, 'The other user was not sent a password reset mail.'); @@ -292,11 +298,11 @@ $this->drupalLogin($user_with_name); $this->drupalGet('user/password'); // There should not be a form element for name. - $this->assertSession()->fieldNotExists('name', 'Duplicate user is not asked for a name when resetting password while logged in.'); - $this->submitForm(NULL, [], t('Submit')); + $this->assertNoField('name', 'Duplicate user is not asked for a name when resetting password while logged in.'); + $this->drupalPostForm(NULL, [], t('Submit')); // Make sure the user with the matching username was sent an email. - $this->assertSession()->pageTextContains(t('Further instructions have been sent to your e-mail address.'), 'User is notified that password reset was sent when logged in.'); - $this->assertEquals(count($this->drupalGetMails( + $this->assertText(t('Further instructions have been sent to your e-mail address.'), 'User is notified that password reset was sent when logged in.'); + $this->assertEqual(count($this->drupalGetMails( [ 'key' => 'password_reset', 'to' => $user_with_name->getEmail(), @@ -304,7 +310,7 @@ // Make sure that the user with the matching // email address was not sent an email. // (An email was already sent to this user earlier.) - $this->assertEquals(count($this->drupalGetMails( + $this->assertEqual(count($this->drupalGetMails( [ 'key' => 'password_reset', 'to' => $user_with_email->getEmail(), @@ -441,7 +447,7 @@ // Logged in users should not be able to access the user.reset.login or the // user.reset.form routes. - $timestamp = REQUEST_TIME - 1; + $timestamp = \Drupal::time()->getRequestTime() - 1; $this->drupalGet("user/reset/" . $this->account->id() . "/$timestamp/" . user_pass_rehash($this->account, $timestamp) . '/login'); $this->assertSession()->statusCodeEquals(403); $this->drupalGet("user/reset/" . $this->account->id()); diff -u b/core/modules/user/tests/src/Functional/UserRegistrationTest.php b/core/modules/user/tests/src/Functional/UserRegistrationTest.php --- b/core/modules/user/tests/src/Functional/UserRegistrationTest.php +++ b/core/modules/user/tests/src/Functional/UserRegistrationTest.php @@ -13,6 +13,8 @@ * Tests registration of user under different configurations. * * @group user + * @method assertText(\Drupal\Core\StringTranslation\TranslatableMarkup $t, string $string) + * @method drupalPostForm(string $string, array $edit, \Drupal\Core\StringTranslation\TranslatableMarkup $t) */ class UserRegistrationTest extends BrowserTestBase { @@ -29,9 +31,7 @@ protected $defaultTheme = 'stark'; /** - * Tests registration of user with email verification. - * - * @group user + * {@inheritdoc} */ public function testRegistrationWithEmailVerification() { $config = $this->config('user.settings'); @@ -76,9 +76,7 @@ } /** - * Tests registration of user without email verification. - * - * @group user + * {@inheritdoc} */ public function testRegistrationWithoutEmailVerification() { $config = $this->config('user.settings'); @@ -153,9 +151,7 @@ } /** - * Tests registration of user with email duplicates. - * - * @group user + * {@inheritdoc} */ public function testRegistrationEmailDuplicates() { // Don't require email verification and allow registration by site visitors @@ -177,7 +173,8 @@ $this->submitForm($edit, 'Create new account'); $this->assertSession()->pageTextContains('The email address ' . $duplicate_user->getEmail() . ' is already taken.'); - // Attempt to bypass duplicate email registration validation by adding spaces. + // Attempt to bypass duplicate email registration validation + // by adding spaces. $edit['mail'] = ' ' . $duplicate_user->getEmail() . ' '; $this->drupalGet('user/register'); @@ -256,9 +253,7 @@ } /** - * Tests registration of user with default values. - * - * @group user + * {@inheritdoc} */ public function testRegistrationDefaultValues() { // Don't require email verification and allow registration by site visitors @@ -292,7 +287,7 @@ $this->assertEquals($name, $new_user->getAccountName(), 'Username matches.'); $this->assertEquals($mail, $new_user->getEmail(), 'Email address matches.'); // Verify that the creation time is correct. - $this->assertGreaterThan(REQUEST_TIME - 20, $new_user->getCreatedTime()); + $this->assertGreaterThan(\Drupal::time()->getRequestTime() - 20, $new_user->getCreatedTime()); $this->assertEquals($config_user_settings->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, $new_user->isActive(), 'Correct status field.'); $this->assertEquals($config_system_date->get('timezone.default'), $new_user->getTimezone(), 'Correct time zone field.'); $this->assertEquals(\Drupal::languageManager()->getDefaultLanguage()->getId(), $new_user->langcode->value, 'Correct language field.'); @@ -440,7 +435,7 @@ $user_settings->set('verify_mail', FALSE)->save(); // Don't require admin approval for new accounts. - $user_settings->set('register', REGISTER_VISITORS)->save(); + $user_settings->set('register', USER_REGISTER_VISITORS)->save(); // Set up a user to check for duplicates. $duplicate_user = $this->drupalCreateUser(); @@ -453,8 +448,8 @@ // Attempt to create a new account using a username that matches an // existing email. - $this->submitForm('user/register', $edit, t('Create new account')); - $this->assertSession()->pageTextContains(t('The name @name is already taken.', ['@name' => $edit['name']]), "A user cannot be created when their username matches an existing user's email address."); + $this->drupalPostForm('user/register', $edit, t('Create new account')); + $this->assertText(t('The name @name is already taken.', ['@name' => $edit['name']]), "A user cannot be created when their username matches an existing user's email address."); // Change the username to an email address. $duplicate_user->name = $name = $this->randomMachineName() . '@example.com'; @@ -466,8 +461,8 @@ // Attempt to create a new account using an email that matches an existing // username. - $this->submitForm('user/register', $edit, t('Create new account')); - $this->assertSession()->pageTextContains(t('The email address @email is already registered.', ['@email' => $edit['mail']]), "A user cannot be created when their email address matches an existing username."); + $this->drupalPostForm('user/register', $edit, t('Create new account')); + $this->assertText(t('The email address @email is already registered.', ['@email' => $edit['mail']]), "A user cannot be created when their email address matches an existing username."); } } diff -u b/core/modules/user/tests/src/FunctionalJavascript/UserPasswordResetTest.php b/core/modules/user/tests/src/FunctionalJavascript/UserPasswordResetTest.php --- b/core/modules/user/tests/src/FunctionalJavascript/UserPasswordResetTest.php +++ b/core/modules/user/tests/src/FunctionalJavascript/UserPasswordResetTest.php @@ -65,7 +65,7 @@ // Set the last login time that is used to generate the one-time link so // that it is definitely over a second ago. - $account->login = REQUEST_TIME - mt_rand(10, 100000); + $account->login = \Drupal::time()->getRequestTime() - mt_rand(10, 100000); Database::getConnection()->update('users_field_data') ->fields(['login' => $account->getLastLoginTime()]) ->condition('uid', $account->id()) @@ -91,7 +91,7 @@ $resetURL = $this->getResetUrl(); $this->drupalGet($resetURL); - // Login + // Login. $this->submitForm([], 'Log in'); // Generate file. only in patch2: unchanged: --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -52,7 +52,10 @@ function user_help($route_name, RouteMatchInterface $route_match) { $output .= '
' . t("This web page allows administrators to register new users. Users' email addresses and usernames must be unique.") . '
'; case 'user.admin_permissions': - return '' . t('Permissions let you control what users can do and see on your site. You can define a specific set of permissions for each role. (See the Roles page to create a role.) Any permissions granted to the Authenticated user role will be given to any user who is logged in to your site. From the Account settings page, you can make any role into an Administrator role for the site, meaning that role will be granted all new permissions automatically. You should be careful to ensure that only trusted users are given this access and level of control of your site.', [':role' => Url::fromRoute('entity.user_role.collection')->toString(), ':settings' => Url::fromRoute('entity.user.admin_form')->toString()]) . '
'; + return '' . t('Permissions let you control what users can do and see on your site. You can define a specific set of permissions for each role. (See the Roles page to create a role.) Any permissions granted to the Authenticated user role will be given to any user who is logged in to your site. From the Account settings page, you can make any role into an Administrator role for the site, meaning that role will be granted all new permissions automatically. You should be careful to ensure that only trusted users are given this access and level of control of your site.', [ + ':role' => Url::fromRoute('entity.user_role.collection')->toString(), + ':settings' => Url::fromRoute('entity.user.admin_form')->toString(), + ]) . '
'; case 'entity.user_role.collection': return '' . t('A role defines a group of users that have certain privileges. These privileges are defined on the Permissions page. Here, you can define the names and the display sort order of the roles on your site. It is recommended to order roles from least permissive (for example, Anonymous user) to most permissive (for example, Administrator user). Users who are not logged in have the Anonymous user role. Users who are logged in have the Authenticated user role, plus any other roles granted to their user account.', [':permissions' => Url::fromRoute('user.admin_permissions')->toString()]) . '
'; @@ -85,7 +91,11 @@ function user_theme() { 'render element' => 'elements', ], 'username' => [ - 'variables' => ['account' => NULL, 'attributes' => [], 'link_options' => []], + 'variables' => [ + 'account' => NULL, + 'attributes' => [], + 'link_options' => [], + ], ], ]; } @@ -254,7 +264,7 @@ function user_role_permissions(array $roles) { * An array indexed by role ID. Each value is an array of permission strings * for the given role. */ -function _user_role_permissions_update($roles) { +function _user_role_permissions_update(array $roles) { $role_permissions = []; foreach ($roles as $rid) { $role_permissions[$rid] = \Drupal::config("user.role.$rid")->get('permissions') ?: []; @@ -357,7 +367,7 @@ function user_template_preprocess_default_variables_alter(&$variables) { * An associative array containing: * - account: The user account (\Drupal\Core\Session\AccountInterface). */ -function template_preprocess_username(&$variables) { +function template_preprocess_username(array &$variables) { $account = $variables['account'] ?: new AnonymousUserSession(); $variables['extra'] = ''; @@ -429,7 +439,7 @@ function template_preprocess_username(&$variables) { * * The current user is replaced with the passed in account. * - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\Entity\UserInterface $account * The account to log in. * * @see hook_user_login() @@ -490,7 +500,7 @@ function user_user_logout(AccountInterface $account) { /** * Generates a unique URL for a user to log in and reset their password. * - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\Entity\UserInterface $account * An object containing the user account. * @param array $options * (optional) A keyed array of settings. Supported options are: @@ -501,7 +511,7 @@ function user_user_logout(AccountInterface $account) { * A unique URL that provides a one-time log in for the user, from which * they can change their password. */ -function user_pass_reset_url($account, $options = []) { +function user_pass_reset_url(UserInterface $account, array $options = []) { $timestamp = \Drupal::time()->getRequestTime(); $langcode = $options['langcode'] ?? $account->getPreferredLangcode(); return Url::fromRoute('user.reset', @@ -520,7 +530,7 @@ function user_pass_reset_url($account, $options = []) { /** * Generates a URL to confirm an account cancellation request. * - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\Entity\UserInterface $account * The user account object. * @param array $options * (optional) A keyed array of settings. Supported options are: @@ -534,10 +544,13 @@ function user_pass_reset_url($account, $options = []) { * @see user_mail_tokens() * @see \Drupal\user\Controller\UserController::confirmCancel() */ -function user_cancel_url(UserInterface $account, $options = []) { +function user_cancel_url(UserInterface $account, array $options = []) { $timestamp = \Drupal::time()->getRequestTime(); $langcode = $options['langcode'] ?? $account->getPreferredLangcode(); - $url_options = ['absolute' => TRUE, 'language' => \Drupal::languageManager()->getLanguage($langcode)]; + $url_options = [ + 'absolute' => TRUE, + 'language' => \Drupal::languageManager()->getLanguage($langcode), + ]; return Url::fromRoute('user.cancel_confirm', [ 'user' => $account->id(), 'timestamp' => $timestamp, @@ -557,7 +570,7 @@ function user_cancel_url(UserInterface $account, $options = []) { * For a usage example, see user_cancel_url() and * \Drupal\user\Controller\UserController::confirmCancel(). * - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\Entity\UserInterface $account * An object containing the user account. * @param int $timestamp * A UNIX timestamp, typically \Drupal::time()->getRequestTime(). @@ -589,7 +602,7 @@ function user_pass_rehash(UserInterface $account, $timestamp) { * * @see _user_cancel() */ -function user_cancel($edit, $uid, $method) { +function user_cancel(array $edit, $uid, $method) { $account = User::load($uid); if (!$account) { @@ -609,7 +622,9 @@ function user_cancel($edit, $uid, $method) { // account deletion. if ($method != 'user_cancel_delete') { // Allow modules to add further sets to this batch. - \Drupal::moduleHandler()->invokeAll('user_cancel', [$edit, $account, $method]); + \Drupal::moduleHandler()->invokeAll('user_cancel', [ + $edit, $account, $method, + ]); } // Finish the batch and actually cancel the account. @@ -637,16 +652,17 @@ function user_cancel($edit, $uid, $method) { * * Since batch and session API require a valid user account, the actual * cancellation of a user account needs to happen last. + * * @param array $edit * An array of submitted form values. - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\Entity\UserInterface $account * The user ID of the user account to cancel. * @param string $method * The account cancellation method to use. * * @see user_cancel() */ -function _user_cancel($edit, $account, $method) { +function _user_cancel(array $edit, $account, $method) { $logger = \Drupal::logger('user'); switch ($method) { @@ -660,7 +676,10 @@ function _user_cancel($edit, $account, $method) { $account->block(); $account->save(); \Drupal::messenger()->addStatus(t('Account %name has been disabled.', ['%name' => $account->getDisplayName()])); - $logger->notice('Blocked user: %name %email.', ['%name' => $account->getAccountName(), '%email' => '<' . $account->getEmail() . '>']); + $logger->notice('Blocked user: %name %email.', [ + '%name' => $account->getAccountName(), + '%email' => '<' . $account->getEmail() . '>', + ]); break; case 'user_cancel_reassign': @@ -671,7 +690,10 @@ function _user_cancel($edit, $account, $method) { } $account->delete(); \Drupal::messenger()->addStatus(t('Account %name has been deleted.', ['%name' => $account->getDisplayName()])); - $logger->notice('Deleted user: %name %email.', ['%name' => $account->getAccountName(), '%email' => '<' . $account->getEmail() . '>']); + $logger->notice('Deleted user: %name %email.', [ + '%name' => $account->getAccountName(), + '%email' => '<' . $account->getEmail() . '>', + ]); break; } @@ -767,7 +789,11 @@ function user_mail($key, &$message, $params) { $language_manager->setConfigOverrideLanguage($language); $mail_config = \Drupal::config('user.mail'); - $token_options = ['langcode' => $langcode, 'callback' => 'user_mail_tokens', 'clear' => TRUE]; + $token_options = [ + 'langcode' => $langcode, + 'callback' => 'user_mail_tokens', + 'clear' => TRUE, + ]; $message['subject'] .= PlainTextOutput::renderFromHtml($token_service->replace($mail_config->get($key . '.subject'), $variables, $token_options)); $message['body'][] = $token_service->replace($mail_config->get($key . '.body'), $variables, $token_options); @@ -795,7 +821,7 @@ function user_mail($key, &$message, $params) { * A keyed array of settings and flags to control the token replacement * process. See \Drupal\Core\Utility\Token::replace(). */ -function user_mail_tokens(&$replacements, $data, $options) { +function user_mail_tokens(array &$replacements, array $data, array $options) { if (isset($data['user'])) { $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user'], $options); $replacements['[user:cancel-url]'] = user_cancel_url($data['user'], $options); @@ -828,7 +854,9 @@ function user_role_names($membersonly = FALSE, $permission = NULL) { */ function user_user_role_insert(RoleInterface $role) { // Ignore the authenticated and anonymous roles or the role is being synced. - if (in_array($role->id(), [RoleInterface::AUTHENTICATED_ID, RoleInterface::ANONYMOUS_ID]) || $role->isSyncing()) { + if (in_array($role->id(), [ + RoleInterface::AUTHENTICATED_ID, RoleInterface::ANONYMOUS_ID, + ]) || $role->isSyncing()) { return; } @@ -871,7 +899,9 @@ function user_user_role_delete(RoleInterface $role) { $user_storage->deleteRoleReferences([$role->id()]); // Ignore the authenticated and anonymous roles or the role is being synced. - if (in_array($role->id(), [RoleInterface::AUTHENTICATED_ID, RoleInterface::ANONYMOUS_ID]) || $role->isSyncing()) { + if (in_array($role->id(), [ + RoleInterface::AUTHENTICATED_ID, RoleInterface::ANONYMOUS_ID, + ]) || $role->isSyncing()) { return; } @@ -999,8 +1029,7 @@ function user_role_revoke_permissions($rid, array $permissions = []) { } /** - * Conditionally create and send a notification email when a certain - * operation happens on the given user account. + * Conditionally create and send a notification email. * * @param string $op * The operation being performed on the account. Possible values: @@ -1223,7 +1252,7 @@ function user_logout() { * profile being viewed. * - attributes: HTML attributes for the containing element. */ -function template_preprocess_user(&$variables) { +function template_preprocess_user(array &$variables) { $variables['user'] = $variables['elements']['#user']; // Helpful $content variable for templates. foreach (Element::children($variables['elements']) as $key) {