diff --git a/src/Controller/ProfilsController.php b/src/Controller/ProfilsController.php new file mode 100644 index 0000000..9e14738 --- /dev/null +++ b/src/Controller/ProfilsController.php @@ -0,0 +1,87 @@ + false], methods: ['POST','GET'])] + public function notificationSub(Request $request,UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $entityManager,Mailer $mailer): Response + { + if(!$this->isGranted('ROLE_USER')) + return $this->render('error/forbidden.twig',[ + 'no_index' =>true + ]); + [$formProfils,$response] = $this->handleProfils($this->getUser(),$request,$entityManager,$mailer); + if(!is_null($response)){ + return $response; + } + [$formPassword,$response] = $this->handlePassword($this->getUser(),$request,$userPasswordHasher,$entityManager,$mailer); + if(!is_null($response)){ + return $response; + } + return $this->render('profis/index.twig', [ + 'formProfils' => $formProfils, + 'formPassword' => $formPassword, + 'formDelete' => '', + 'no_index' + ]); + } + + private function handleProfils(?\Symfony\Component\Security\Core\User\UserInterface $getUser, Request $request, EntityManagerInterface $entityManager, Mailer $mailer) + { + $oldEmail = $getUser->getEmail(); + $dto = new DtoProfils($getUser); + $form = $this->createForm(DtoForm::class,$dto); + $form->handleRequest($request); + if($form->isSubmitted() && $form->isValid()){ + $getUser->setEmail($dto->getEmail()); + $getUser->setUsername($dto->getUsername()); + $entityManager->persist($getUser); + $entityManager->flush(); + return [$form->createView(),$this->redirectToRoute('app_profile',['updateProfils'=>true])]; + } + return [$form->createView(),null]; + } + + private function handlePassword(?\Symfony\Component\Security\Core\User\UserInterface $getUser, Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Mailer $mailer) + { + $dto = new DtoPassword(); + $form = $this->createForm(DtoPasswordForm::class,$dto); + $form->handleRequest($request); + if($form->isSubmitted() && $form->isValid()){ + $getUser->setPassword($userPasswordHasher->hashPassword($dto->getPassword(), $getUser)); + $entityManager->persist($getUser); + $entityManager->flush(); + return [$form->createView(),$this->redirectToRoute('app_profile',['updatePassword'=>true])]; + } + return [$form->createView(),null]; + } + +} + diff --git a/src/Dto/Profils/DtoForm.php b/src/Dto/Profils/DtoForm.php new file mode 100644 index 0000000..3ef4165 --- /dev/null +++ b/src/Dto/Profils/DtoForm.php @@ -0,0 +1,34 @@ +add('email',EmailType::class,[ + 'label' => 'profils.email.label', + 'attr' => [ + 'placeholder' => 'profils.email.placeholder', + ] + ]) + ->add('username',TextType::class,[ + 'label' => 'profils.username.label', + 'attr' => [ + 'placeholder' => 'profils.username.placeholder', + ] + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefault('data_class',DtoProfils::class); + } +} diff --git a/src/Dto/Profils/DtoPassword.php b/src/Dto/Profils/DtoPassword.php new file mode 100644 index 0000000..33d1646 --- /dev/null +++ b/src/Dto/Profils/DtoPassword.php @@ -0,0 +1,24 @@ +password; + } + + /** + * @param string $password + */ + public function setPassword(string $password): void + { + $this->password = $password; + } +} diff --git a/src/Dto/Profils/DtoPasswordForm.php b/src/Dto/Profils/DtoPasswordForm.php new file mode 100644 index 0000000..9542148 --- /dev/null +++ b/src/Dto/Profils/DtoPasswordForm.php @@ -0,0 +1,27 @@ +add('password',RepeatedType::class,[ + 'type' => PasswordType::class, + 'first_options' => ['label' => 'profils.password.1','attr'=>['placeholder'=>'profils.password_holder.1']], + 'second_options' => ['label' => 'profils.password.2','attr'=>['placeholder'=>'profils.password_holder.2']], + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefault('data_class',DtoPassword::class); + } +} diff --git a/src/Dto/Profils/DtoProfils.php b/src/Dto/Profils/DtoProfils.php new file mode 100644 index 0000000..d499bc1 --- /dev/null +++ b/src/Dto/Profils/DtoProfils.php @@ -0,0 +1,50 @@ +username = $account->getUsername(); + $this->email = $account->getEmail(); + } + + /** + * @return string|null + */ + public function getEmail(): ?string + { + return $this->email; + } + + /** + * @return string|null + */ + public function getUsername(): ?string + { + return $this->username; + } + + + /** + * @param string|null $email + */ + public function setEmail(?string $email): void + { + $this->email = $email; + } + + /** + * @param string|null $username + */ + public function setUsername(?string $username): void + { + $this->username = $username; + } +} diff --git a/templates/base.twig b/templates/base.twig index 283fc9f..b9d8eab 100644 --- a/templates/base.twig +++ b/templates/base.twig @@ -123,9 +123,18 @@