vendor/stimactiv/admin-light-bundle/src/Controller/SecurityController.php line 102

Open in your IDE?
  1. <?php
  2. namespace Stimactiv\AdminBundle\Controller;
  3. use App\Security\Authenticator\CollaborateurAuthenticator;
  4. use Stimactiv\AdminBundle\Form\Handler\CollaborateurHandler;
  5. use Stimactiv\AdminBundle\Form\Type\Security\PasswordChangeType;
  6. use Stimactiv\AdminBundle\Form\Type\Security\ProfileType;
  7. use Stimactiv\AdminBundle\Form\Type\Security\RequestType;
  8. use Stimactiv\AdminBundle\Form\Type\Security\ResettingType;
  9. use Stimactiv\AdminBundle\Model\CollaborateurManager;
  10. use Stimactiv\ClientBundle\Entity\Collaborateur;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. /**
  19.  * @Route("/tableaudebord", name="backend_")
  20.  */
  21. class SecurityController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/connexion", name="login")
  25.      */
  26.     public function login(AuthenticationUtils $authenticationUtils): Response
  27.     {
  28.         if ($this->getUser()) {
  29.             return $this->redirectToRoute('backend_home');
  30.         }
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         return $this->render('@StimactivAdmin/security/login/index.html.twig', [
  36.             'last_username' => $lastUsername,
  37.             'error' => $error,
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/infos", name="profile", methods={"GET","POST"})
  42.      */
  43.     public function profile(CollaborateurHandler $formHandler): Response
  44.     {
  45.         /**
  46.          * @var Collaborateur $user
  47.          */
  48.         $user $this->getUser();
  49.         $form $this->createForm(ProfileType::class, $user);
  50.         if ($formHandler->update($form$user)) {
  51.             $this->addFlash('success'"Votre profil vient d'être mis à jour avec succès.");
  52.             return $this->redirectToRoute('backend_profile');
  53.         }
  54.         return $this->render('@StimactivAdmin/security/profile/index.html.twig', [
  55.             'user' => $user,
  56.             'form' => $form->createView(),
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/infos/reset", name="profile_reset", methods={"GET","POST"})
  61.      */
  62.     public function reset(CollaborateurHandler $formHandler): Response
  63.     {
  64.         /**
  65.          * @var Collaborateur $user
  66.          */
  67.         $user $this->getUser();
  68.         $form $this->createForm(PasswordChangeType::class, $user);
  69.         if ($formHandler->resetPassword($form)) {
  70.             $this->addFlash('success'"Votre mot de passe vient d'être mis à jour avec succès.");
  71.             return $this->redirectToRoute('backend_profile_reset');
  72.         }
  73.         return $this->render('@StimactivAdmin/security/profile/reset.html.twig', [
  74.             'user' => $user,
  75.             'form' => $form->createView(),
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/logout", name="logout")
  80.      */
  81.     public function logout()
  82.     {
  83.         throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  84.     }
  85.     /**
  86.      * @Route("/{slug}", name="request_reset_password", requirements={"slug"="oubli|premiere-connexion"})
  87.      */
  88.     public function request(string $slugCollaborateurHandler $formHandlerCollaborateurManager $collaborateurManager): Response
  89.     {
  90.         $options = [];
  91.         if ($slug === 'premiere-connexion') {
  92.             $options['block_prefix'] = 'register';
  93.         }
  94.         $form $this->createForm(RequestType::class, null$options);
  95.         if ($formHandler->requestResetPassword($form)) {
  96.             $duration $collaborateurManager::$_resetLinkDuration 60 $collaborateurManager::$_resetLinkDuration ' minutes' : ($collaborateurManager::$_resetLinkDuration 60) . 'h';
  97.             $this->addFlash('success'"Un mail va vous être envoyé <b>dans quelques instants</b> afin que vous puissiez " . ($slug === 'oubli' "renouveler" "définir") . " votre mot de passe.<br>Le lien que vous recevrez sera valide pendant " $duration ".");
  98.         } else {
  99.             foreach ($form->getErrors() as $error) {
  100.                 $this->addFlash('danger'$error->getMessage());
  101.             }
  102.         }
  103.         return $this->render('@StimactivAdmin/security/resetting/request.html.twig', [
  104.             'form' => $form->createView(),
  105.         ]);
  106.     }
  107.     /**
  108.      * @Route("/reset/{id}/{token}", name="resetting_reset_password", requirements={"id"="\d+"})
  109.      */
  110.     public function resetting(Request $requestCollaborateur $userstring $tokenCollaborateurHandler $formHandlerCollaborateurManager $contactManagerUserAuthenticatorInterface $userAuthenticatorCollaborateurAuthenticator $collaborateurAuthenticator): Response
  111.     {
  112.         // interdit l'accès à la page si:
  113.         // le token enregistré en base et le token présent dans l'url ne sont pas égaux
  114.         // le token date de plus de 15 minutes
  115.         if ($token !== $user->getToken() || !$contactManager->isRequestInTime($user)) {
  116.             $this->addFlash('warning''Le lien n\'existe plus.');
  117.             throw new AccessDeniedException();
  118.         }
  119.         $form $this->createForm(ResettingType::class, $user);
  120.         if ($formHandler->resetPassword($form)) {
  121.             $this->addFlash('success'"Votre mot de passe a été renouvelé.");
  122.             $userAuthenticator->authenticateUser(
  123.                 $user,
  124.                 $collaborateurAuthenticator,
  125.                 $request
  126.             );
  127.             return $this->redirectToRoute('backend_home');
  128.         }
  129.         return $this->render('@StimactivAdmin/security/resetting/reset.html.twig', [
  130.             'form' => $form->createView()
  131.         ]);
  132.     }
  133. }