From 45a006094275f01e608b91e19582e52745a7ee54 Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 12 Jun 2021 16:02:27 +0200 Subject: [PATCH] added password support --- src/Controller/Admin/UserCrudController.php | 139 ++++++++++++++++++-- 1 file changed, 125 insertions(+), 14 deletions(-) diff --git a/src/Controller/Admin/UserCrudController.php b/src/Controller/Admin/UserCrudController.php index 1575931..f896367 100644 --- a/src/Controller/Admin/UserCrudController.php +++ b/src/Controller/Admin/UserCrudController.php @@ -3,23 +3,134 @@ namespace App\Controller\Admin; use App\Entity\User; +use Doctrine\ORM\EntityManagerInterface; +use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; +use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; +use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; +use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; +use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField; +use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; +use Symfony\Component\Form\Extension\Core\Type\PasswordType; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; +use Symfony\Component\Security\Core\Security; +/** + * Class UserCrudController + * @package App\Controller\Admin + */ class UserCrudController extends AbstractCrudController { - public static function getEntityFqcn(): string - { - return User::class; - } + private ?string $password; + + public static function getEntityFqcn(): string + { + return User::class; + } + + /** + * @var UserPasswordEncoderInterface + */ + private $passwordHasher; + /** + * @var Security + */ + private $security; + + /** + * UserCrudController constructor. + * + * @param \Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $passwordHasher + * @param Security $security + */ + public function __construct(UserPasswordHasherInterface $passwordHasher, Security $security) + { + $this->passwordHasher = $passwordHasher; + $this->security = $security; + + // get the user id from the logged in user + if (null !== $this->security->getUser()) { + $this->password = $this->security->getUser()->getPassword(); + } + } + + public function configureActions(Actions $actions): Actions + { + return $actions + ->add(Crud::PAGE_INDEX, Crud::PAGE_DETAIL); + } + + + + /** + * @param string $pageName + * + * @return iterable + */ + public function configureFields(string $pageName): iterable + { + //$id = TextField::new('id')-> + $username = TextField::new('username'); + $email = TextField::new('email'); + $firstName = TextField::new('firstName'); + $lastName = TextField::new('lastName'); + $createdAt = DateTimeField::new('createdAt'); + $lastLoginAt = DateTimeField::new('lastLoginAt'); + $isVerified = BooleanField::new('isVerified'); + $roles = ChoiceField::new('roles', 'Roles') + ->allowMultipleChoices() + ->autocomplete() + ->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'); - /* - public function configureFields(string $pageName): iterable - { - return [ - IdField::new('id'), - TextField::new('title'), - TextEditorField::new('description'), - ]; - } - */ + switch ($pageName) { + case Crud::PAGE_INDEX: + return [ $username, $firstName, $lastName, $email, $lastLoginAt ]; + break; + case Crud::PAGE_DETAIL: + return [ $username, $firstName, $lastName, $email, $lastLoginAt, $createdAt, $roles, $isVerified ]; + break; + case Crud::PAGE_NEW: + return [ + $password, + ]; + break; + case Crud::PAGE_EDIT: + return [ $username, $password, $firstName, $lastName, $email, $lastLoginAt, $roles, $isVerified ]; + + break; + } + + } + + /** + * + * @param EntityManagerInterface $entityManager + * @param $entityInstance + */ + public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void + { + // set new password with encoder 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); + } + } + + parent::updateEntity($entityManager, $entityInstance); + } }