added password support

This commit is contained in:
tracer 2021-06-12 16:02:27 +02:00
parent fbd4c3c383
commit 45a0060942
1 changed files with 125 additions and 14 deletions

View File

@ -3,23 +3,134 @@
namespace App\Controller\Admin; namespace App\Controller\Admin;
use App\Entity\User; 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\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 class UserCrudController extends AbstractCrudController
{ {
public static function getEntityFqcn(): string private ?string $password;
{
return User::class;
}
/* public static function getEntityFqcn(): string
public function configureFields(string $pageName): iterable {
{ return User::class;
return [ }
IdField::new('id'),
TextField::new('title'), /**
TextEditorField::new('description'), * @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');
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);
}
} }