src/Controller/Website/Auth/SecurityController.php line 127

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website\Auth;
  3. use App\Controller\Website\ThemeRenderController;
  4. use App\Entity\Generic\Customer\Customer;
  5. use App\Form\Website\NewPasswordType;
  6. use App\Form\Website\ResetPasswordSmsRequestFormType;
  7. use App\Form\Website\VerifySmsCodeType;
  8. use App\Security\Authenticator\CustomerAuthenticator;
  9. use App\Service\SmsHandler;
  10. use App\Service\WebsiteManager;
  11. use App\Service\Util\Sms;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use LogicException;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  25. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  26. class SecurityController extends ThemeRenderController
  27. {
  28.     #[Route(path'/website-panel/login'name'customer_panel_login')]
  29.     public function login(AuthenticationUtils $authenticationUtilsWebsiteManager $shopManager): Response
  30.     {
  31.         if ($this->getUser()) {
  32.             return $this->redirectToRoute('customer_panel_user_handler');
  33.         }
  34.         $error $authenticationUtils->getLastAuthenticationError();
  35.         $lastUsername $authenticationUtils->getLastUsername();
  36.         return $this->renderUserTemplate('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  37.     }
  38.     #[Route(path'/website-panel/user-handler'name'customer_panel_user_handler')]
  39.     public function userHandler(Security $security): RedirectResponse
  40.     {
  41.         $user $security->getUser();
  42.         if ($user->hasRole('ROLE_ADMIN')) {
  43.             return $this->redirectToRoute('shop_customer_panel_admin_dashboard');
  44.         }
  45.         return $this->redirectToRoute('shop_customer_panel_dashboard');
  46.     }
  47.     #[Route(path'/website-panel/logout'name'shop_owner_logout')]
  48.     public function logout(): void
  49.     {
  50.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  51.     }
  52.     #[Route('/forgot-password/sms'name'app_forgot_password_sms')]
  53.     public function requestSmsReset(
  54.         Request                 $request,
  55.         EntityManagerInterface  $em,
  56.         SmsHandler                     $smsSender,
  57.         TokenGeneratorInterface $tokenGenerator,
  58.     ): Response
  59.     {
  60.         $form $this->createForm(ResetPasswordSmsRequestFormType::class);
  61.         $form->handleRequest($request);
  62.         if ($form->isSubmitted() && $form->isValid()) {
  63.             $phone $form->get('phoneNumber')->getData();
  64.             $user $em->getRepository(Customer::class)->findOneBy(['mobile' => $phone]);
  65.             if ($user) {
  66.                 $code random_int(100000999999);
  67.                 $user->setVerifyCode($code);
  68.                 $user->setPasswordResetAt(new \DateTime());
  69.                 $em->flush();
  70.                 $smsSender->sendCode($phone$code);
  71.             }
  72.             $this->addFlash('success''در صورت ثبت شماره صحیح، کدی برای شما ارسال خواهد شد');
  73.             return $this->redirectToRoute('app_verify_sms_code');
  74.         }
  75.         return $this->renderUserTemplate('security/request_sms_reset.html.twig', [
  76.             'form' => $form->createView(),
  77.         ]);
  78.     }
  79.     #[Route('/forgot-password/sms/verify'name'app_verify_sms_code')]
  80.     public function verifyCode(
  81.         Request                $request,
  82.         EntityManagerInterface $em,
  83.         UrlGeneratorInterface  $urlGenerator,
  84.         SessionInterface       $session
  85.     ): Response
  86.     {
  87.         $form $this->createForm(VerifySmsCodeType::class);
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             $phone $form->get('phoneNumber')->getData();
  91.             $code $form->get('code')->getData();
  92.             $user $em->getRepository(Customer::class)->findOneBy(['mobile' => $phone'verifyCode' => $code]);
  93.             if ($user && $user->getPasswordResetAt() > (new \DateTime('-10 minutes'))) {
  94.                 // ذخیره موقتی کاربر برای مرحله بعدی
  95.                 $session->set('reset_user_id'$user->getId());
  96.                 return $this->redirectToRoute('app_reset_password_form');
  97.             }
  98.             $this->addFlash('danger''کد وارد شده صحیح نیست یا منقضی شده');
  99.         }
  100.         return $this->renderUserTemplate('security/verify_sms_code.html.twig', [
  101.             'form' => $form->createView(),
  102.         ]);
  103.     }
  104.     #[Route('/forgot-password/sms/reset'name'app_reset_password_form')]
  105.     public function resetPassword(
  106.         Request                     $request,
  107.         EntityManagerInterface      $em,
  108.         UserPasswordHasherInterface $hasher,
  109.         SessionInterface            $session
  110.     ): Response
  111.     {
  112.         $userId $session->get('reset_user_id');
  113.         if (!$userId) {
  114.             return $this->redirectToRoute('app_forgot_password_sms');
  115.         }
  116.         $user $em->getRepository(Customer::class)->find($userId);
  117.         if (!$user) {
  118.             return $this->redirectToRoute('app_forgot_password_sms');
  119.         }
  120.         $form $this->createForm(NewPasswordType::class);
  121.         $form->handleRequest($request);
  122.         if ($form->isSubmitted() && $form->isValid()) {
  123.             $password $form->get('newPassword')->getData();
  124.             $user->setPassword($hasher->hashPassword($user$password));
  125.             $user->setVerifyCode(null);
  126.             $user->setPasswordResetAt(null);
  127.             $em->flush();
  128.             $session->remove('reset_user_id');
  129.             $this->addFlash('success''رمز عبور با موفقیت تغییر کرد');
  130.             return $this->redirectToRoute('customer_panel_login');
  131.         }
  132.         return $this->renderUserTemplate('security/reset_password.html.twig', [
  133.             'form' => $form->createView(),
  134.         ]);
  135.     }
  136.     #[Route('/streight-login/{id}'name'app_website_streight_login')]
  137.     public function app_website_streight_login(
  138.         Customer $customer null,
  139.         Request $request,
  140.         UserAuthenticatorInterface $userAuthenticator,
  141.         CustomerAuthenticator $customerAuthenticator,   // همون authenticator خودت
  142.     )
  143.     {
  144.         if ($customer){
  145.             return $userAuthenticator->authenticateUser(
  146.                 $customer,
  147.                 $customerAuthenticator// Authenticator مربوط به فایروال customer_firewall
  148.                 $request
  149.             );
  150.         }else{
  151.             return $this->redirectToRoute('app_shop_site_index');
  152.         }
  153.     }
  154. }