src/Controller/SecurityController.php line 48

  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Ivory\CKEditorBundle\Exception\Exception;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Doctrine\ORM\EntityManager;
  16. use App\Manager\UserManager;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. use App\Entity\Users;
  20. use BeSimple\I18nRoutingBundle\Routing\Annotation\I18nRoute;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class SecurityController extends AbstractController
  23. {
  24.     public TranslatorInterface $translator;
  25.     public function __construct(TranslatorInterface $translator, private ManagerRegistry $managerRegistry)
  26.     {
  27.         $this->translator $translator;
  28.     }
  29.     /**
  30.      * @param Request $request
  31.      * @param AuthenticationUtils $authUtils
  32.      * @param TokenStorageInterface $tokenStorage
  33.      * @return Response|null
  34.      * @throws \Exception
  35.      */
  36.     #[Route(path'/login'name'login'methods: ['GET''POST'])]
  37.     public function loginAction(Request $requestAuthenticationUtils $authUtilsTokenStorageInterface $tokenStorageRouterInterface $router)
  38.     {
  39.         // get the login error if there is one
  40.         $error $authUtils->getLastAuthenticationError();
  41.         $socialConnectionEmailExistsOtherAccount $request->getSession()->get('socialConnectionEmailExistsOtherAccount'0);
  42.         if ($socialConnectionEmailExistsOtherAccount) {
  43.             $this->container->get('request_stack')->getSession()->remove('socialConnectionEmailExistsOtherAccount');
  44.             $this->addFlashMessage('error'$this->trans('social_login.alert.email_already_exists', [], 'messages'));
  45.         }
  46.         $socialOfferConnection $request->getSession()->get('socialOfferConnection'0);
  47.         if ($socialOfferConnection) {
  48.             // pokud stranka pro potvrzeni, tak presmeruj pote na homepage
  49.             $request->getSession()->set('_security.main.target_path'$router->generate('homepage'));
  50.             $request->getSession()->set('socialOfferConnection'0);
  51.             return $this->redirectToRoute('confirm_social_login');
  52.         }
  53.         if ($disabled $request->getSession()->get('disabled'false)) {
  54.             $request->getSession()->remove('disabled');
  55.         }
  56.         return $this->render('security/login.html.twig', array(
  57. //            'last_username' => $lastUsername,
  58.               'error'         => $error,
  59.               'disabled'      => $disabled
  60.         ));
  61.     }
  62.     /**
  63.      * @param Request $request
  64.      * @return Response|null
  65.      * @throws \Exception
  66.      */
  67.     #[Route(path'/reset-password'name'reset-password'methods: ['GET''POST'])]
  68.     public function resetPasswordAction(Request $requestUserManager $userManager)
  69.     {
  70.         $em $this->managerRegistry->getManager();
  71.         $submit $request->get('reset-submit'false);
  72.         if ($submit) {
  73.             $email $request->get('password-reset-email''');
  74.             $user $em->getRepository(Users::class)->findOneBy(['email' => $email'isDeleted' => false]);
  75.             if (!$user) {
  76.                 $user $em->getRepository(Users::class)->findOneBy(['email' => $email]);
  77.             }
  78.             if ($user && $user->isIsDeleted() == false) {
  79.                 $restoreUrl $this->generateUrl('set-password', array('token' => '__TOKEN__'), UrlGeneratorInterface::ABSOLUTE_URL);
  80.                 $userManager->sendResetLinkTo($user$restoreUrl);
  81.                 if ($request->isXmlHttpRequest()) {
  82.                     return new JsonResponse(array('message' => $this->trans('reset_password.email_sent', [], 'login')));
  83.                 } else {
  84.                     $this->addFlashMessage('notice''reset_password.email_sent', [], 'login');
  85.                 }
  86.             } elseif ($user && $user->isIsDeleted() == true){
  87.                 if ($request->isXmlHttpRequest()) {
  88.                     return new JsonResponse(array('message' => $this->trans('reset_password.user_disabled', [], 'login')));
  89.                 } else {
  90.                     $this->addFlashMessage('notice''reset_password.user_disabled', [], 'login');
  91.                 }
  92.             } else {
  93.                 if ($request->isXmlHttpRequest()) {
  94.                     return new JsonResponse(array('message' => $this->trans('reset_password.user_not_found', [], 'login')));
  95.                 } else {
  96.                     $this->addFlashMessage('notice''reset_password.user_not_found', [], 'login');
  97.                 }
  98.             }
  99.         }
  100.         return $this->render('security/reset_password.html.twig', array(
  101.         ));
  102.     }
  103.     /**
  104.      * @param Request $request
  105.      * @param UserManager $userManager
  106.      * @return Response|null
  107.      * @throws \Exception
  108.      */
  109.     #[Route(path'/set-password'name'set-password'methods: ['GET''POST'])]
  110.     public function setPasswordAction(Request $requestUserManager $userManager): Response
  111.     {
  112.         $em $this->managerRegistry->getManager();
  113.         $passwordUpdated false;
  114.         $token $request->get('token''');
  115.         $user $em->getRepository(Users::class)->findOneBy(['passwordResetHash' => $token]);
  116.         if (!$user) {
  117.             throw new NotFoundHttpException($this->trans('set_password.user_not_found', [], 'login'));
  118.         }
  119.         // check hash valid
  120.         $today = new \DateTime();
  121.         $tokenExpiration $user->getHashValidUntil();
  122.         if ($today $tokenExpiration) {
  123.             // token expired
  124.             $this->addFlashMessage('error'$this->trans('set_password.alert.token_expired', [], 'login'));
  125.         }
  126.         if ($request->getMethod() == 'POST') {
  127.             $formData $request->request->all();
  128.             $password $formData['new_password'];
  129.             if (!$password || $formData['new_password'] != $formData['repeat_password']) {
  130.                 $this->addFlashMessage('error'$this->trans('set_password.alert.not_match', [], 'login'));
  131.             } else {
  132.                 $checkResult $userManager->isPasswordStrength($password$user->getUserName(), $user->getEmail(), $user->getFirstName().$user->getLastName());
  133.                 if (!$checkResult) {
  134.                     $this->addFlashMessage('error''profile_weak_psswd', [], 'controller');
  135.                 } else {
  136.                     $this->updatePassword($user$password$userManager$em);
  137.                     $this->addFlashMessage('success'$this->trans('set_password.new_set', [], 'login'));
  138.                     $passwordUpdated true;
  139.                 }
  140.             }
  141.         }
  142.         return $this->render('security/set_password.html.twig', array(
  143.             'token' => $token,
  144.             'passwordUpdated' => $passwordUpdated,
  145.             'user' => $user,
  146.         ));
  147.     }
  148.     /**
  149.      * @param Request $request
  150.      */
  151.     #[Route(path'/logout'name'logout')]
  152.     public function logoutAction()
  153.     {
  154.     }
  155.     /**
  156.      * Methods enable translation of error|notice flash messages.
  157.      *
  158.      * @param $key
  159.      * @param array $params
  160.      * @param null $domain
  161.      * @return mixed
  162.      */
  163.     public function trans($key, array $params = [], $domain null)
  164.     {
  165.         return $this->translator->trans($key$params$domain);
  166.     }
  167.     public function addFlashMessage($type$key, array $params = [], $domain null){
  168.         $this->addFlash($type$this->trans($key$params$domain));
  169.     }
  170.     /**
  171.      * @param Users $user
  172.      * @param string $password
  173.      * @param UserManager $userManager
  174.      */
  175.     protected function updatePassword(Users $user$passwordUserManager $userManager) {
  176.         $em $this->managerRegistry->getManager();
  177.         $userManager->setUserPassword($user$password);
  178.         $user->setHashValidUntil(null);
  179.         $user->setPasswordResetHash('');
  180.         $em->flush();
  181.     }
  182. }