src/EventListener/BaseSite/OtpCheckSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\BaseSite;
  3. use App\Entity\Generic\Customer\Customer;
  4. use App\Entity\Generic\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. class OtpCheckSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private Security        $security,
  15.         private RouterInterface $router,
  16.     )
  17.     {
  18.     }
  19.     public function onKernelController(ControllerEvent $event): void
  20.     {
  21. //
  22.         $request $event->getRequest();
  23.         $route $request->attributes->get('_route');
  24.         // فقط مسیرهای مربوط به پنل مشتری
  25.         if (!str_starts_with($request->getPathInfo(), '/shop-owner')) {
  26.             return;
  27.         }
  28.         $user $this->security->getUser();
  29.         if (!$user instanceof User) {
  30.             return;
  31.         }
  32. // مسیرهایی که باید مجاز باشن حتی اگه phone یا otp هنوز تأیید نشده
  33.         $safeRoutes = [
  34.             'app_base_site_auth_profile',
  35.             'app_register',
  36.             'app_verify_email',
  37.             'shop_owner_login',
  38.             'shop_owner_logout'// اگه logout هم داری
  39.             'base_site_enter_phone',
  40.             'base_site_otp_verify',
  41.             'app_logout'// اگه logout هم داری
  42.             'shop_owner_logout'// اگه logout هم داری
  43.         ];
  44.         if (in_array((string)$route$safeRoutestrue)) {
  45.             return;
  46.         }
  47.         if ((string)$route !== 'base_site_enter_phone') {
  48.             if (!$user->getMobile()) {
  49.                 $url $this->router->generate('base_site_enter_phone');
  50.                 $event->setController(fn() => new RedirectResponse($url));
  51.             }
  52.         }
  53.         if ((string)$route !== 'base_site_otp_verify') {
  54.             if (!$user->isVerified()) {
  55.                 $url $this->router->generate('base_site_enter_phone');
  56.                 $event->setController(fn() => new RedirectResponse($url));
  57.             }
  58.         }
  59.     }
  60.     public static function getSubscribedEvents(): array
  61.     {
  62.         return [
  63.             KernelEvents::CONTROLLER => [['onKernelController'0]], // دقیقاً قبل اجرای کنترلر
  64.         ];
  65.     }
  66. }