src/Controller/Auth/SecurityController.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Auth;
  3. use App\Entity\Generic\User;
  4. use App\Repository\Generic\UserRepository;
  5. use App\Service\Util\NotificationService;
  6. use DateTime;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Exception;
  10. use LogicException;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class SecurityController extends AbstractController
  18. {
  19.     #[Route(path'/redirect'name'app_auth_handler')]
  20.     #[IsGranted('ROLE_USER')]
  21.     public function app_auth_handler(ManagerRegistry $managerRegistryNotificationService $notificationService,EntityManagerInterface $entityManager,  UserRepository $userRepository): Response
  22.     {
  23.         /**
  24.          * @var User $user
  25.          */
  26.         $user $this->getUser();
  27.         if ($user){
  28.             $user->setLastLogin(new \DateTime());
  29.             $this->getDoctrine()->getManager()->flush();
  30.         }
  31. //        if (!$user->isVerified()){
  32. //            return  $this->redirectToRoute('app_auth_verify_account');
  33. //        }
  34.         try {
  35.             foreach ($userRepository->findAll() as $user) {
  36.                 $array = [];
  37.                 $targetUser $user;
  38.                 start:
  39.                 if ($targetUser->getPresenter()) {
  40.                     $array[] = $targetUser->getEmail();
  41.                     $targetUser $targetUser->getPresenter();
  42.                     goto start;
  43.                 } else {
  44.                     $array[] = $targetUser->getEmail();
  45.                 }
  46.                 $user->setUserTrace(array_reverse($array));
  47.             }
  48.             $this->getDoctrine()->getManager()->flush();
  49.         } catch (Exception $exception) {
  50.             $notificationService->exception($exception);
  51.             $haveError true;
  52.         }
  53.         if (!$this->getUser()) {
  54.             return $this->redirectToRoute('app_login');
  55.         }
  56.         /**
  57.          * @var User $user
  58.          */
  59.         $user $this->getUser();
  60. //        if (!$user->isActive()){
  61. //            $this->addFlash('error'  , 'حساب شما مسدود شده است');
  62. //            return  $this->redirectToRoute('app_logout');
  63. //        }
  64.         $user->setLastLogin(new DateTime());
  65.         $managerRegistry->getManager()->flush();
  66.         $port $user->getUniquePresenterCode();
  67.         while ($port == null) {
  68.             try {
  69.                 $tempPort random_int(1000065000);
  70.             } catch (\Exception $exception) {
  71.                 $notificationService->exception($exception);
  72.             }
  73.             if ($managerRegistry->getRepository(User::class)->findOneBy(['uniquePresenterCode' => $tempPort]) === null) {
  74.                 $port $tempPort;
  75.             }
  76.         }
  77.         $user->setUniquePresenterCode($port);
  78.         $managerRegistry->getManager()->flush();
  79.         if ($this->getUser()->hasRole('ROLE_ADMIN')) {
  80.             return $this->redirectToRoute('app_admin_dashboard');
  81.         }
  82. //        die('سیستم در حال بروزرسانی می باشد');
  83.         return $this->redirectToRoute('app_user_dashboard');
  84.     }
  85.     #[Route(path'/login'name'app_login')]
  86.     public function login(AuthenticationUtils $authenticationUtils Request $request): Response
  87.     {
  88.         if (str_contains($request->getHost(),'tehran')) {
  89.             $isStoreSite true;
  90.         }else{
  91.             $isStoreSite false;
  92.         }
  93.         if ($this->getUser()) {
  94.             return $this->redirectToRoute('app_auth_handler');
  95.         }
  96.         // get the login error if there is one
  97.         $error $authenticationUtils->getLastAuthenticationError();
  98.         // last username entered by the user
  99.         $lastUsername $authenticationUtils->getLastUsername();
  100.         return $this->render('auth/security/login.html.twig', [
  101.             'last_username' => $lastUsername,
  102.             'error' => $error,
  103.             'isStoreSite' => $isStoreSite
  104.         ]);
  105.     }
  106.     #[Route(path'/logout'name'app_logout')]
  107.     public function logout(): void
  108.     {
  109.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  110.     }
  111. }