From edada158794173beaf93ddc8cd36be7b22696199 Mon Sep 17 00:00:00 2001 From: Aymeric SERRA Date: Thu, 9 Feb 2023 15:41:12 +0100 Subject: [PATCH] =?UTF-8?q?Mot=20de=20passe=20quand=20on=20cr=C3=A9er=20un?= =?UTF-8?q?=20utilisateur=20depuis=20interface=20admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/UserController.php | 12 +++++- src/Form/NewUserType.php | 71 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Form/NewUserType.php diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 94b2cc8..6e6bad2 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -3,12 +3,14 @@ namespace App\Controller; use App\Entity\User; +use App\Form\NewUserType; use App\Form\UserType; use App\Repository\UserRepository; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Routing\Annotation\Route; #[IsGranted('ROLE_ADMINISTRATEUR')] @@ -24,13 +26,19 @@ class UserController extends AbstractController } #[Route('/new', name: 'app_user_new', methods: ['GET', 'POST'])] - public function new(Request $request, UserRepository $userRepository): Response + public function new(Request $request, UserRepository $userRepository, UserPasswordHasherInterface $userPasswordHasher): Response { $user = new User(); - $form = $this->createForm(UserType::class, $user); + $form = $this->createForm(NewUserType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $user->setPassword( + $userPasswordHasher->hashPassword( + $user, + $form->get('plainPassword')->getData() + ) + ); $userRepository->save($user, true); return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER); diff --git a/src/Form/NewUserType.php b/src/Form/NewUserType.php new file mode 100644 index 0000000..2940762 --- /dev/null +++ b/src/Form/NewUserType.php @@ -0,0 +1,71 @@ +add('email') + ->add('plainPassword', PasswordType::class, [ + // instead of being set onto the object directly, + // this is read and encoded in the controller + 'mapped' => false, + 'attr' => ['autocomplete' => 'new-password'], + 'constraints' => [ + new NotBlank([ + 'message' => 'Please enter a password', + ]), + new Length([ + 'min' => 6, + 'minMessage' => 'Your password should be at least {{ limit }} characters', + // max length allowed by Symfony for security reasons + 'max' => 4096, + ]), + ], + ]) + ->add('roles', ChoiceType::class, [ + 'choices' => [ + 'Apprenti' => 'ROLE_APPRENTI', + 'Instructeur' => 'ROLE_INSTRUCTEUR', + 'Admin' => 'ROLE_ADMINISTRATEUR' + ], + 'required' => true, + 'multiple' => false, + 'expanded' => false, + ]) + ->add('nom') + ->add('prenom'); + + $builder->get('roles') + ->addModelTransformer(new CallbackTransformer( + function ($rolesArray) { + // transform the array to a string + return count($rolesArray) ? $rolesArray[0] : null; + }, + function ($rolesString) { + // transform the string back to an array + return [$rolesString]; + } + )); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => User::class, + ]); + } +}