src/Controller/Backoffice/AdminAccountController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backoffice;
  3. use App\Entity\User;
  4. use App\Entity\Admin;
  5. use App\Entity\SetPassword;
  6. use App\Form\SetPasswordType;
  7. use App\Entity\PasswordUpdate;
  8. use App\Form\AdminProfileType;
  9. use App\Form\PasswordUpdateType;
  10. use App\Repository\UserRepository;
  11. use Symfony\Component\Form\FormError;
  12. use Doctrine\Persistence\ObjectManager;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  23. class AdminAccountController extends AbstractController
  24. {
  25.     private $translator;
  26.     private $request;
  27.     public function __construct(TranslatorInterface $translatorRequestStack $requestStack)
  28.     {
  29.         $this->translator $translator;
  30.         $this->request $requestStack->getCurrentRequest();
  31.     }
  32.     
  33.     private function generateUniqueFileName()
  34.     {
  35.         return md5(uniqid());
  36.     }
  37.     #[Route(path'/connexion'name'admin_login')]
  38.     public function login(AuthenticationUtils $utils)
  39.     {
  40.         $error $utils->getLastAuthenticationError();
  41.         $username $utils->getLastUsername();
  42.         return $this->render('backoffice/account/login.html.twig', [
  43.             'hasError' => $error,
  44.             'username' => $username
  45.         ]);
  46.     }
  47.     #[Route(path'/verification-compte'name'account_check')]
  48.     public function accountCheck(AuthenticationUtils $utils)
  49.     {
  50.         $admin $this->getUser();
  51.         if ($admin->getHasAlreadyLoggedIn()) {
  52.             if (in_array("ROLE_EDITOR",$admin->getRoles()))
  53.             {
  54.                 return $this->redirectToRoute('admin_blog_published_articles');
  55.             }
  56.             
  57.             return $this->redirectToRoute('admin_dashboard');
  58.         }
  59.         else {
  60.             return $this->redirectToRoute('set_password');
  61.         }
  62.     }
  63.     #[Route(path'/securite/configurer-mot-de-passe'name'set_password')]
  64.     public function setPassword(Request $requestUserPasswordHasherInterface $passwordHasher)
  65.     {
  66.         $admin $this->getUser();
  67.         $password = new SetPassword();
  68.         $form $this->createForm(SetPasswordType::class, $password);
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $newPass $password->getNewPassword();
  72.             $hash $passwordHasher->hashPassword($admin$newPass);
  73.             $admin->setPassword($hash)
  74.                  ->setHasAlreadyLoggedIn(true)
  75.                  ->setActivated(true);
  76.             $manager $this->getDoctrine()->getManager();
  77.             $manager->persist($admin);
  78.             $manager->flush();
  79.             $this->addFlash(
  80.                 'success',
  81.                 $this->translator->trans('backoffice.new_password_save')
  82.             );
  83.             if (in_array("ROLE_EDITOR",$admin->getRoles()))
  84.             {
  85.                 return $this->redirectToRoute('admin_blog_published_articles');
  86.             }
  87.             
  88.             return $this->redirectToRoute('admin_dashboard');
  89.         }
  90.         return $this->render('backoffice/user/set-password.html.twig', [
  91.             'form' => $form->createView()
  92.         ]);
  93.     }
  94.     #[Route(path'/deconnexion'name'admin_logout')]
  95.     public function logout(){}
  96.     #[Route(path'/modifier-mon-profil'name'admin_edit_profile')]
  97.     public function adminProfile(Request $requestObjectManager $managerUserPasswordHasherInterface $passwordHasher)
  98.     {
  99.         $admin $this->getUser();
  100.         $oldPhoto $admin->getPhoto();
  101.         $passUpdate = new PasswordUpdate();
  102.         $formPassword $this->createForm(PasswordUpdateType::class, $passUpdate);
  103.         $formProfile $this->createForm(AdminProfileType::class, $admin);
  104.         $formPassword->handleRequest($request);
  105.         $formProfile->handleRequest($request);
  106.         if ($formPassword->isSubmitted() && $formPassword->isValid()) {
  107.             if (!password_verify($passUpdate->getOldPassword(), $admin->getPassword())) {
  108.                 $formPassword->get('oldPassword')->addError(new FormError("Ce n'est pas votre de passe actuel !"));
  109.             }
  110.             else {
  111.                 $newPass $passUpdate->getNewPassword();
  112.                 $hash $passwordHasher->hashPassword($admin$newPass);
  113.                 $admin->setPassword($hash);
  114.                 $manager->persist($admin);
  115.                 $manager->flush();
  116.                 $this->addFlash(
  117.                     'success',
  118.                     $this->translator->trans('backoffice.password_edit')
  119.                 );
  120.                 return $this->redirectToRoute('admin_profile');
  121.             }
  122.         }
  123.         if ($formProfile->isSubmitted() && $formProfile->isValid()) {
  124.             if ($admin->getPhoto() != null) {
  125.                 
  126.                 $avatar $admin->getPhoto();
  127.                 $fileName $this->generateUniqueFileName() . '.' $avatar->guessExtension();
  128.                 $avatar->move(
  129.                     $this->getParameter('avatar_directory'),
  130.                     $fileName
  131.                 );
  132.                 $admin->setPhoto($fileName);
  133.                 if(file_exists($this->getParameter('avatar_directory') . '/' $oldPhoto))
  134.                 {
  135.                     unlink($this->getParameter('avatar_directory') . '/' $oldPhoto);
  136.                 }
  137.             }
  138.             else {
  139.                 $admin->setPhoto($oldPhoto);
  140.             }
  141.             
  142.             $manager->persist($admin);
  143.             $manager->flush();
  144.             $this->addFlash(
  145.                 'success',
  146.                 $this->translator->trans('backoffice.information_modified')
  147.             );
  148.             return $this->redirectToRoute('admin_profile');
  149.         }
  150.         return $this->render('backoffice/account/edit_profile.html.twig', [
  151.             'formProfile' => $formProfile->createView(),
  152.             'formPassword' => $formPassword->createView(),
  153.             'oldPhoto' => $oldPhoto
  154.         ]);
  155.     }
  156.     #[Route(path'/mon-profil'name'admin_profile')]
  157.     public function seeProfile()
  158.     {
  159.         $admin $this->getUser();
  160.         return $this->render('/backoffice/account/see_profile.html.twig', [
  161.             'admin' => $admin
  162.         ]);
  163.     }
  164.     #[Route(path'/change-locale'name'change_locale')]
  165.     public function changeLocale(Request $request)
  166.     {
  167.         $locale $request->getLocale();
  168.         if ($locale == "fr") {
  169.             $this->get('session')->set('_locale''en');
  170.         } else {
  171.             $this->get('session')->set('_locale''fr');
  172.         }
  173.         return $this->redirectToRoute('admin_dashboard');
  174.     }
  175. }