From a5e665cbe43e6b290824c95cfccd56c77aebe727 Mon Sep 17 00:00:00 2001 From: tracer Date: Sun, 13 Jun 2021 15:16:03 +0200 Subject: [PATCH] removed encoder, set hasher --- src/Controller/Admin/UserCrudController.php | 39 +++++++++------------ 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Controller/Admin/UserCrudController.php b/src/Controller/Admin/UserCrudController.php index f896367..4be2ba9 100644 --- a/src/Controller/Admin/UserCrudController.php +++ b/src/Controller/Admin/UserCrudController.php @@ -29,13 +29,13 @@ class UserCrudController extends AbstractCrudController } /** - * @var UserPasswordEncoderInterface + * @var UserPasswordHasherInterface */ - private $passwordHasher; + private UserPasswordHasherInterface $passwordHasher; /** * @var Security */ - private $security; + private Security $security; /** * UserCrudController constructor. @@ -61,7 +61,6 @@ class UserCrudController extends AbstractCrudController } - /** * @param string $pageName * @@ -69,7 +68,6 @@ class UserCrudController extends AbstractCrudController */ public function configureFields(string $pageName): iterable { - //$id = TextField::new('id')-> $username = TextField::new('username'); $email = TextField::new('email'); $firstName = TextField::new('firstName'); @@ -80,34 +78,31 @@ class UserCrudController extends AbstractCrudController $roles = ChoiceField::new('roles', 'Roles') ->allowMultipleChoices() ->autocomplete() - ->setChoices([ 'User' => 'ROLE_USER', - 'Admin' => 'ROLE_ADMIN', - 'SuperAdmin' => 'ROLE_SUPER_ADMIN'] + ->setChoices([ + 'User' => 'ROLE_USER', + 'Admin' => 'ROLE_ADMIN', + 'SuperAdmin' => 'ROLE_SUPER_ADMIN'] ); $password = TextField::new('password') ->setFormType(PasswordType::class) ->setFormTypeOption('empty_data', '') ->setRequired(false) ->setHelp('Leave blank to keep the current password'); - + switch ($pageName) { case Crud::PAGE_INDEX: - return [ $username, $firstName, $lastName, $email, $lastLoginAt ]; - break; + return [$username, $firstName, $lastName, $email, $lastLoginAt]; case Crud::PAGE_DETAIL: - return [ $username, $firstName, $lastName, $email, $lastLoginAt, $createdAt, $roles, $isVerified ]; - break; + return [$username, $firstName, $lastName, $email, $lastLoginAt, $createdAt, $roles, $isVerified]; case Crud::PAGE_NEW: return [ $password, ]; - break; case Crud::PAGE_EDIT: - return [ $username, $password, $firstName, $lastName, $email, $lastLoginAt, $roles, $isVerified ]; - - break; + return [$username, $password, $firstName, $lastName, $email, $lastLoginAt, $roles, $isVerified]; + } - + return []; } /** @@ -117,17 +112,17 @@ class UserCrudController extends AbstractCrudController */ public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void { - // set new password with encoder interface + // set new password with hash interface if (method_exists($entityInstance, 'setPassword')) { $plainPassword = trim($this->get('request_stack')->getCurrentRequest()->request->all('User')['password']); - + // do nothing if no password is entered if (isset($plainPassword) === true && $plainPassword === '') { $entityInstance->setPassword($this->password); } else { $user = new User(); - $encodedPassword = $this->passwordHasher->hashPassword($user, $plainPassword); - $entityInstance->setPassword($encodedPassword); + $hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword); + $entityInstance->setPassword($hashedPassword); } }