From 98c08c910f8305102f0c2601394f59916b3f46a2 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Tue, 6 Dec 2022 09:37:40 -0500 Subject: [PATCH] ISTWCMS-5983: Code standards with logic to load user using entity_type.manager service. --- src/Form/UwAddUsersForm.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Form/UwAddUsersForm.php b/src/Form/UwAddUsersForm.php index 54204209..cce2b9d2 100644 --- a/src/Form/UwAddUsersForm.php +++ b/src/Form/UwAddUsersForm.php @@ -2,6 +2,7 @@ namespace Drupal\uw_cfg_common\Form; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\path_alias\AliasManager; @@ -24,6 +25,13 @@ class UwAddUsersForm extends FormBase { */ protected UWLdap $uwLdap; + /** + * Entity type interface manager from core. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected EntityTypeManagerInterface $entityTypeManager; + /** * {@inheritdoc} */ @@ -31,16 +39,18 @@ class UwAddUsersForm extends FormBase { // Instantiates this form class. return new static( $container->get('path_alias.manager'), - $container->get('uw_cfg_common.uw_ldap') + $container->get('uw_cfg_common.uw_ldap'), + $container->get('entity_type.manager') ); } /** * Class constructor. */ - public function __construct(AliasManager $pathAliasManager, UWLdap $uwLdap) { + public function __construct(AliasManager $pathAliasManager, UWLdap $uwLdap, EntityTypeManagerInterface $entityTypeManager) { $this->pathAliasManager = $pathAliasManager; $this->uwLdap = $uwLdap; + $this->entityTypeManager = $entityTypeManager; } /** @@ -76,6 +86,7 @@ class UwAddUsersForm extends FormBase { /** * {@inheritdoc} + * @throws \Exception */ public function submitForm(array &$form, FormStateInterface $form_state) { @@ -95,7 +106,7 @@ class UwAddUsersForm extends FormBase { $user = strtolower($user); // Ignore blank lines. - if (!strlen($user)) { + if ($user === '') { continue; } @@ -111,12 +122,17 @@ class UwAddUsersForm extends FormBase { continue; } - /** @var UserInterface $existing_user */ - $existing_user = user_load_by_name($user); + $existing_user = $this->entityTypeManager->getStorage('user')->loadByProperties([ + ['name' => $user], + ]); + + /** @var \Drupal\user\UserInterface|bool $existing_user */ + $existing_user = $existing_user ? reset($existing_user) : FALSE; + if ($existing_user) { $this->messenger()->addError($this->t('The user ID <a href="@link"><em>@user</em></a> already exists and was skipped.', [ - '@link' => $this->pathAliasManager->getAliasByPath('/user/' . $existing_user->id), + '@link' => $this->pathAliasManager->getAliasByPath('/user/' . $existing_user->id()), '@user' => $user, ] )); @@ -130,11 +146,11 @@ class UwAddUsersForm extends FormBase { $sets[] = '0123456789'; $sets[] = '!@#$%^&*()-_=+[]{}?`~"'; $password = ''; - $password_length = rand(16, 32); + $password_length = random_int(16, 32); while (strlen($password) < $password_length) { foreach ($sets as $set) { $password .= $set[array_rand(str_split($set))]; - if (strlen($password) == $password_length) { + if (strlen($password) === $password_length) { break; } } -- GitLab