User management - Login and user password hash.
This commit is contained in:
@@ -2,18 +2,25 @@
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Entity\User;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
class UserCrudController extends AbstractCrudController
|
||||
{
|
||||
public function __construct(
|
||||
public UserPasswordHasherInterface $userPasswordHasher
|
||||
) {}
|
||||
|
||||
public static function getEntityFqcn(): string
|
||||
{
|
||||
return User::class;
|
||||
@@ -29,9 +36,12 @@ class UserCrudController extends AbstractCrudController
|
||||
->setChoices($availableRoles)
|
||||
->allowMultipleChoices(true);
|
||||
|
||||
yield FormField::addPanel('Change password')->setIcon('fa fa-key');
|
||||
|
||||
yield Field::new ('password', 'New password')->onlyWhenCreating()->setRequired(true)
|
||||
yield FormField::addPanel('Set password')->setIcon('fa fa-key');
|
||||
|
||||
$fieldPassword = Field::new ('plainPassword', 'New password')
|
||||
->hideOnIndex()
|
||||
->setRequired(false)
|
||||
->setFormType(RepeatedType::class)
|
||||
->setFormTypeOptions([
|
||||
'type' => PasswordType::class,
|
||||
@@ -40,8 +50,41 @@ class UserCrudController extends AbstractCrudController
|
||||
'error_bubbling' => true,
|
||||
'invalid_message' => 'The password fields do not match.',
|
||||
]);
|
||||
if($pageName == Crud::PAGE_NEW) {
|
||||
$fieldPassword->setRequired(true);
|
||||
}
|
||||
|
||||
//TODO: Allow password change on Update.
|
||||
yield $fieldPassword;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param User $entityInstance
|
||||
*/
|
||||
public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
|
||||
{
|
||||
//Hash password on creation.
|
||||
$plaintextPassword = $entityInstance->getPlainPassword();
|
||||
|
||||
$hashedPassword = $this->userPasswordHasher->hashPassword($entityInstance, $plaintextPassword);
|
||||
$entityInstance->setPassword($hashedPassword);
|
||||
|
||||
parent::updateEntity($entityManager, $entityInstance);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $entityInstance
|
||||
*/
|
||||
public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
|
||||
{
|
||||
$plaintextPassword = $entityInstance->getPlainPassword();
|
||||
|
||||
if($plaintextPassword && $plaintextPassword != "") {
|
||||
//New password set. Hash password.
|
||||
$hashedPassword = $this->userPasswordHasher->hashPassword($entityInstance, $plaintextPassword);
|
||||
$entityInstance->setPassword($hashedPassword);
|
||||
}
|
||||
|
||||
parent::updateEntity($entityManager, $entityInstance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user