src/EventListener/RoutingListener.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Company;
  4. use App\Entity\LoginSessions;
  5. use App\Entity\Terminology;
  6. use App\Entity\UserProfile;
  7. use App\Utils\LtsUtils;
  8. use App\Utils\TopNavbar;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  17. use Symfony\Component\HttpFoundation\Response;
  18. final class RoutingListener
  19. {
  20.     /**
  21.      * @var ContainerInterface
  22.      */
  23.     private $container;
  24.     /**
  25.      * @var mixed;
  26.      */
  27.     private $topNavbar;
  28.     /**
  29.      * @var LtsUtils;
  30.      */
  31.     private $ltsUtils;
  32.     /**
  33.      * @var TranslatorInterface;
  34.      */
  35.     private $translator;
  36.     /**
  37.      * @var Security;
  38.      */
  39.     private $security;
  40.     /**
  41.      * @var mixed;
  42.      */
  43.     private $parameters;
  44.     private $em;
  45.     private $router;
  46.     private $companyId;
  47.     /**
  48.      * Constructor.
  49.      *
  50.      * @param Container $container
  51.      */
  52.     public function __construct(ContainerInterface $containerSecurity $securityEntityManagerInterface $entityManagerTopNavbar $topNavBarLtsUtils $ltsUtilsTranslatorInterface $translatorRouterInterface $router)
  53.     {
  54.         $this->container $container;
  55.         $this->security $security;
  56.         $this->em $entityManager;
  57.         $this->topNavbar $topNavBar;
  58.         $this->ltsUtils $ltsUtils;
  59.         $this->translator $translator;
  60.         $this->router $router;
  61.         $this->companyId $this->container->getParameter('default_company_id');
  62.     }
  63.     /**
  64.      * Routing Listener event.
  65.      */
  66.     public function onKernelRequest(RequestEvent $event)
  67.     {
  68.         $user $this->security->getUser();
  69.         // Set default data
  70.         $this->setDefaultParams();
  71.         // Set UTC timezone
  72.         $this->setTimeZone();
  73.         if ($user) {
  74.             $this->setUserData($user);
  75.             $this->checkForPlanExpiry($event);
  76.             $this->checkUserPrevilage($user,$event);
  77.             $this->setTwigVariables();
  78.             $this->setCompanyDetails();
  79.             // Set company details
  80.             $this->translator->setLocale($this->parameters['lang']);
  81.             // Set container variables
  82.             $this->container->set('contact'$this);
  83.             // Set Top navigation details
  84.             $this->setTopNavLinks($event$user);
  85.             // Set Demo tour details
  86.             $this->setDemoTourData();
  87.             // some logic to determine the $locale
  88.             // $request->setLocale('Asia/Kolkata');
  89.             // $this->saveLastActiveTime($event);  # last activity time store
  90.         }
  91.     }
  92.     /**
  93.      * This function is to get contact data from contact object.
  94.      *
  95.      * @param string $key
  96.      *
  97.      * @return string
  98.      */
  99.     public function get($key)
  100.     {
  101.         return $this->parameters[$key];
  102.     }
  103.     /**
  104.      * Method setDefaultParams
  105.      * This function is to set default contact data from contact object.
  106.      *
  107.      * @return void
  108.      */
  109.     public function setDefaultParams()
  110.     {
  111.         $this->parameters['id'] = '';
  112.         $this->parameters['userId'] = '';
  113.         $this->parameters['email'] = '';
  114.         $this->parameters['firstName'] = '';
  115.         $this->parameters['surName'] = '';
  116.         $this->parameters['fullName'] = '';
  117.         $this->parameters['companyName'] = '';
  118.         $this->parameters['roles'] = '';
  119.         $this->parameters['isMainAdmin'] = '';
  120.         $this->parameters['profilePic'] = '';
  121.         $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
  122.         $this->parameters['defaultSystemLang'] = 'en';
  123.         $this->parameters['planExpireDate'] = null;
  124.         $this->parameters['plan'] = '';
  125.         $this->parameters['account_status'] = 'active';
  126.         $this->parameters['companyId'] = $this->companyId;
  127.     }
  128.     /**
  129.      * This function is to set contact details in contact object.
  130.      *
  131.      * @param object $user
  132.      */
  133.     public function setUserData($user)
  134.     {
  135.         $userDetails $this->em->getRepository(UserProfile::class)->getUserFullData($user->getId());
  136.         if ($userDetails) {
  137.             $this->parameters['id'] = $userDetails['id'];
  138.             $this->parameters['userId'] = $userDetails['id'];
  139.             $this->parameters['firstName'] = $userDetails['firstName'];
  140.             $this->parameters['email'] = $userDetails['email'];
  141.             $this->parameters['surName'] = $userDetails['lastName'];
  142.             $this->parameters['tier'] = $userDetails['tier'];
  143.             $this->parameters['userProfileImage'] = $userDetails['userProfileImage'];
  144.             $this->parameters['userProfileImageUrl'] = !empty($userDetails['userProfileImage']) ? $this->ltsUtils->getUploadPath('profile_pic'''$userDetails['userProfileImage']) : '';
  145.             $this->parameters['fullName'] = $userDetails['fullName'];
  146.             $this->parameters['companyName'] = $userDetails['companyTitle'];
  147.             $this->parameters['roles'] = $user->getRoles();
  148.             $this->parameters['isMainAdmin'] = $user->isMainAdmin();
  149.             $this->parameters['isSuperAdmin'] = $user->isSuperAdmin();
  150.             $this->parameters['companyId'] = $this->companyId $userDetails['companyId'];
  151.             $this->parameters['encodeCompanyId'] = base64_encode($userDetails['companyId']);
  152.             $this->parameters['userPrivilegeRoles'] = $userDetails['userPrivilegeRoles'];
  153.             $this->parameters['userType'] = $userDetails['userType'];
  154.             $this->parameters['ltsLogo'] = 'data-point-new-logo.png';
  155.             $this->parameters['companyImageUrl'] = !empty($userDetails['companyImage']) ? $this->ltsUtils->getUploadPath('company_logo'$this->parameters['encodeCompanyId'], $userDetails['companyImage']) : '';
  156.             $this->parameters['planExpireDate'] = $userDetails['planExpireDate'];
  157.             $this->parameters['plan'] = $userDetails['plan'];
  158.             $this->parameters['refId'] = $userDetails['refId'];
  159.             $this->parameters['previlageRoles'] = !empty($userDetails['userPrivilegeRoles']) ? $this->ltsUtils->getUserRoles($userDetails['userPrivilegeRoles']) : [];
  160.         }
  161.     }
  162.     /**
  163.      * Set Time zone.
  164.      */
  165.     public function setTimeZone()
  166.     {
  167.         date_default_timezone_set('UTC');
  168.     }
  169.     /**
  170.      * @param type $event
  171.      * @param type $user
  172.      */
  173.     private function setTopNavLinks($event$user)
  174.     {
  175.         $request $event->getRequest();
  176.         $resArray $this->topNavbar->getData($request$user);
  177.         if ($resArray) {
  178.             $this->container->get('twig')->addGlobal('topnav_data'$resArray['topnavData']);
  179.             $this->container->get('twig')->addGlobal('pagetitle_data'$resArray['pageTitleBlock']);
  180.         }
  181.     }
  182.     private function setDemoTourData()
  183.     {
  184.         $this->container->get('twig')->addGlobal('demo_tour_data'$this->ltsUtils->getDemoTourArray());
  185.     }
  186.     /**
  187.      * This function is to set contact details in twig object.
  188.      */
  189.     private function setTwigVariables()
  190.     {
  191.         $this->container->get('twig')->addGlobal('fr_contact'$this->parameters);
  192.         $this->container->get('twig')->addGlobal('ltApp'$this->parameters);
  193.     }
  194.     /**
  195.      * This function is to set contact details in twig object.
  196.      */
  197.     private function setCompanyDetails()
  198.     {
  199.         $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
  200.         $this->parameters['defaultDate'] = 'd-m-Y';
  201.         $this->parameters['defaultTime'] = 'h:i:a';
  202.         $this->parameters['defaultSystemLang'] = 'en';
  203.         $this->parameters['defaultTimezone'] = 'UTC';
  204.         $companyDetails $this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
  205.         $companyDetails['timezone'] = !empty($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
  206.         $this->parameters['lang'] = $companyDetails && !empty($companyDetails['systemLanguage']) ? $companyDetails['systemLanguage'] : 'en';
  207.         $this->parameters['timezone'] = isset($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
  208.         $this->parameters['userRefidPrefix'] = !empty($companyDetails['userRefidPrefix']) ? $companyDetails['userRefidPrefix'] : '';
  209.         $this->parameters['cardRefidPrefix'] =  !empty($companyDetails['cardRefidPrefix']) ? $companyDetails['cardRefidPrefix'] : '';
  210.         $this->parameters['cardRefidStartsFrom'] = !empty($companyDetails['cardRefidStartsFrom']) ? $companyDetails['cardRefidStartsFrom'] : '1';
  211.         $this->parameters['dataTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
  212.         $this->parameters['dataFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
  213.         $this->parameters['timeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
  214.         $this->parameters['companyTerminology'] = $this->ltsUtils->getTerminologyDetails($this->companyId);
  215.         // company plans limit setting
  216.         $this->parameters['companySettings'] = $companyDetails;
  217.         // dateTime formats for twig pages
  218.         $companyDetails['allDateTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
  219.         $companyDetails['allDateFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
  220.         $companyDetails['allTimeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
  221.         // inviduvidal company limitation
  222.         $this->parameters['c1'] = $companyDetails && !empty($companyDetails['c1']) ? $companyDetails['c1'] : 0;
  223.         $this->container->get('twig')->addGlobal('company'$companyDetails);
  224.     }
  225.     /**
  226.      * This function is to save last active time for each login session.
  227.      */
  228.     private function saveLastActiveTime($event)
  229.     {
  230.         $request $event->getRequest();
  231.         $params['routeName'] = $request->get('_route');
  232.         $params['loginUserId'] = $this->get('userId');
  233.         $params['company_id'] = $this->get('companyId');
  234.         if (isset($params['loginUserId']) && $this->em->isOpen()) {
  235.             $this->em->getRepository(LoginSessions::class)->update($params);
  236.         }
  237.     }
  238.     /**
  239.      * @param type $event
  240.      */
  241.     private function checkForPlanExpiry($event)
  242.     {
  243.         if (!empty($this->parameters['planExpireDate']) && !$this->parameters['isSuperAdmin']) {
  244.             if ($this->parameters['planExpireDate']->format('Y-m-d') <= date('Y-m-d') && 'free_trail' == $this->parameters['plan']) {
  245.                 $request $event->getRequest();
  246.                 $requestedUrl $request->getRequestUri();
  247.                 // If public demo accounts after expiring only this url will be allowed.
  248.                 $allowedURLs = ['/settings/accounts''/settings/get-account-settings-data''/settings/get-terminology-data''/settings/logo_upload''/settings/save-account-settings-data''/settings/save-terminology-data'];
  249.                 if (!in_array($requestedUrl$allowedURLs)) {
  250.                     $redirectUrl $this->router->generate('account-settings');
  251.                     $response = new RedirectResponse($redirectUrl);
  252.                     $event->setResponse($response);
  253.                 }
  254.                 $this->parameters['account_status'] = 'in-active';
  255.             }
  256.         }
  257.     }    
  258.     /**
  259.      * In this function, verify if the logged-in user has the necessary privileges to access the route (when entered directly in the browser).
  260.      *
  261.      * @param $user
  262.      *
  263.      * @return void
  264.      */
  265.     private function checkUserPrevilage($user,$event)
  266.     {
  267.         $companyDetails=$this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
  268.         $request $event->getRequest();
  269.         $path $request->getRequestUri();  
  270.         $prev true;
  271.         if( $path == $this->router->generate('product_list')){
  272.             $prev $user in_array('ROLE_PRODUCT_MANAGEMENT_ADMIN'$user->getRoles()) || in_array('ROLE_PRODUCT_MANAGEMENT_VIEW'$user->getRoles()) : false;
  273.         } else if($path == $this->router->generate('product_variant_list')){
  274.             $prev $user in_array('ROLE_PRODUCT_VARIANT_MANAGEMENT_VIEW'$user->getRoles()) || in_array('ROLE_PRODUCT_VARIANT_MANAGEMENT_ADMIN'$user->getRoles()) : false;
  275.         } else if($path == $this->router->generate('customer_list')){
  276.             $prev $user in_array('ROLE_CUSTOMER_MANAGEMENT_VIEW'$user->getRoles()) || in_array('ROLE_CUSTOMER_MANAGEMENT_ADMIN'$user->getRoles()) : false;
  277.         } else if($path == $this->router->generate('boards_list')){
  278.             $prev $user in_array('ROLE_BOARD_SETTINGS_VIEW'$user->getRoles()) || in_array('ROLE_BOARD_SETTINGS_ADMIN'$user->getRoles()) : false;
  279.         } else if($path == $this->router->generate('stock_management_list')){
  280.             $prev $user in_array('ROLE_ASSY_STOCK_MANAGEMENT_VIEW'$user->getRoles()) || in_array('ROLE_ASSY_STOCK_MANAGEMENT_ADMIN'$user->getRoles()) : false;
  281.         } else if($path == $this->router->generate('smt_stock_management_list')){
  282.             $prev $user in_array('ROLE_SMT_STOCK_MANAGEMENT_VIEW'$user->getRoles()) || in_array('ROLE_SMT_STOCK_MANAGEMENT_ADMIN'$user->getRoles()) : false;
  283.         } else if($path == $this->router->generate('running_order_view')){
  284.             $prev $user in_array('ROLE_RUNNING_ORDER_VIEW'$user->getRoles()) || in_array('ROLE_RUNNING_ORDER_ADMIN'$user->getRoles()) : false;
  285.         } else if($path == $this->router->generate('master_production_schedule')){
  286.             $prev $user in_array('ROLE_MASTER_PRODUCTION_SCHEDLE_REPORT_VIEW'$user->getRoles()) || in_array('ROLE_MASTER_PRODUCTION_SCHEDLE_REPORT_ADMIN'$user->getRoles()) : false;
  287.         } else if($path == $this->router->generate('activity_log')){
  288.             $prev $user in_array('ROLE_ACTIVITY_LOG_VIEW'$user->getRoles()) || in_array('ROLE_ACTIVITY_LOG_ADMIN'$user->getRoles()) : false;
  289.         } else if($path == $this->router->generate('shift_assign')){
  290.             $prev $user in_array('ROLE_MANAGE_SHIFT_VIEW'$user->getRoles()) || in_array('ROLE_MANAGE_SHIFT_ADMIN'$user->getRoles()) : false;
  291.         } else if($path == $this->router->generate('oee_list')){
  292.             $prev $user in_array('ROLE_OEE_DAILY_TARGET_VIEW'$user->getRoles()) || in_array('ROLE_OEE_DAILY_TARGET_ADMIN'$user->getRoles()) : false;
  293.         } else if( $path == ($this->router->generate('user'))){
  294.             $prev $user in_array('ROLE_USER_MANAGEMENT_VIEW'$user->getRoles()) || in_array('ROLE_USER_MANAGEMENT_ADMIN'$user->getRoles()) : false;
  295.         }  else if($path == $this->router->generate('user_type')){
  296.             $prev $user in_array('ROLE_USER_PRIVILEGE_VIEW'$user->getRoles()) || in_array('ROLE_USER_PRIVILEGE_ADMIN'$user->getRoles()) : false;
  297.         } else if($path == $this->router->generate('line_manager')){
  298.             $prev $user in_array('ROLE_DAILY_PLAN_VIEW'$user->getRoles()) || in_array('ROLE_DAILY_PLAN_ADMIN'$user->getRoles()) : false;
  299.         } else if($path == $this->router->generate('workorder_details_table')){
  300.             $prev $user in_array('ROLE_WORKORDER_DETAILS_VIEW'$user->getRoles()) || in_array('ROLE_WORKORDER_DETAILS_ADMIN'$user->getRoles())  : false;
  301.         } else if($path == $this->router->generate('downtime_planing')){
  302.             $prev $user in_array('ROLE_DOWNTIME_PLANNING_VIEW'$user->getRoles()) || in_array('ROLE_DOWNTIME_PLANNING_ADMIN'$user->getRoles())  : false;
  303.         } else if($path == $this->router->generate('excel_import_history')){
  304.             $prev $user in_array('ROLE_IMPORT_HISTORY_VIEW'$user->getRoles()) || in_array('ROLE_IMPORT_HISTORY_ADMIN'$user->getRoles())  : false;
  305.         } else if($path == $this->router->generate('production_settings')){
  306.             $prev $user in_array('ROLE_PRODUCTION_SETTINGS_VIEW'$user->getRoles()) || in_array('ROLE_PRODUCTION_SETTINGS_ADMIN'$user->getRoles()) : false;
  307.         }
  308.         
  309.         
  310.         if(!$prev){
  311.             $msg $this->translator->trans('ACCESS_DENIED');
  312.             throw new AccessDeniedHttpException($msg);
  313.         } 
  314.     }
  315. }