Buckets:
| namespace App\Controller\Admin; | |
| use App\Entity\User; | |
| use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; | |
| use EasyCorp\Bundle\EasyAdminBundle\Field\Field; | |
| use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; | |
| use Symfony\Component\Form\Extension\Core\Type\PasswordType; | |
| use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; | |
| use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField; | |
| use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField; | |
| use phpDocumentor\Reflection\Types\Integer; | |
| use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; | |
| use Symfony\Component\Security\Http\Attribute\IsGranted; | |
| ('ROLE_ADMIN') | |
| class UserCrudController extends AbstractCrudController | |
| { | |
| public function __construct( | |
| private UserPasswordHasherInterface $passwordHasher | |
| ) {} | |
| public static function getEntityFqcn(): string | |
| { | |
| return User::class; | |
| } | |
| public function configureFields(string $pageName): iterable | |
| { | |
| return [ | |
| TextField::new('email'), | |
| TextField::new('firstname'), | |
| TextField::new('lastname'), | |
| TextField::new('username'), | |
| Field::new('password') | |
| ->onlyOnForms() //empêche le mot de passe d’être affiché en clair dans les listes ou les détails. | |
| ->setFormType(PasswordType::class) | |
| ->setFormTypeOptions([ | |
| 'empty_data' => '', // évite les problèmes si le champ est vide | |
| 'constraints' => [], // <== supprime toutes les contraintes ici | |
| ]) | |
| ->setRequired(false) // pour ne pas obliger à le remplir en modification | |
| // ->setHelp('Message pour donner des indications.'), | |
| , | |
| ChoiceField::new('roles') | |
| ->setChoices([ | |
| 'Utilisateur' => 'ROLE_USER', | |
| 'Administrateur' => 'ROLE_ADMIN', | |
| ]) | |
| ->allowMultipleChoices() | |
| ->renderExpanded(), | |
| ]; | |
| } | |
| public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void | |
| { | |
| if (!$entityInstance instanceof User) return; | |
| $this->handlePassword($entityInstance); | |
| parent::persistEntity($entityManager, $entityInstance); | |
| } | |
| public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void | |
| { | |
| if (!$entityInstance instanceof User) return; | |
| $this->handlePassword($entityInstance); | |
| parent::updateEntity($entityManager, $entityInstance); | |
| } | |
| private function handlePassword(User $user): void | |
| { | |
| if ($plainPassword = $user->getPassword()) { | |
| $hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword); | |
| $user->setPassword($hashedPassword); | |
| } | |
| } | |
| } | |
Xet Storage Details
- Size:
- 2.97 kB
- Xet hash:
- bd8c2f72d39a122fecd296a1c7d8ee6898dc162209856351e31f3c49796fbe34
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.