src/EventListener/AuthenticationSuccessListener.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\ApiRefreshToken;
  4. use App\Entity\ApiToken;
  5. use App\Entity\Language;
  6. use App\Entity\Permission;
  7. use App\Entity\PermissionType;
  8. use App\Entity\Role;
  9. use App\Entity\User;
  10. use App\Service\ApiService;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  18. class AuthenticationSuccessListener implements AuthenticationSuccessHandlerInterface
  19. {
  20.         private EntityManagerInterface $em;
  21.         private RouterInterface $router;
  22.         private ApiService $api;
  23.         
  24.         public function __construct(EntityManagerInterface $emRouterInterface $routerApiService $api)
  25.         {
  26.                 $this->em $em;
  27.                 $this->router $router;
  28.                 $this->api $api;
  29.         }
  30.         
  31.         public function onAuthenticationSuccess(Request $requestTokenInterface $token): \Symfony\Component\HttpFoundation\Response
  32.         {
  33.                 
  34.                 /* @var $user User */
  35.                 $user $token->getUser();
  36.                 $superAdmin $this->em->getRepository(Role::class)->findOneBy(['keyName'=>'role.super.admin']);
  37.                 
  38.                 $languagesArr = [];
  39.                 $languages $this->em->getRepository(Language::class)->findEnabledAndNotDeleted();
  40.                 foreach ($languages as $language)
  41.                                 $languagesArr[] = $language->getCode();
  42.                 
  43.                 $routePermissionType $this->em->getRepository(PermissionType::class)->findOneBy(['keyName'=>'permission.type.route']);
  44.                 $userRoutePermissions $this->em->getRepository(Permission::class)->findByTypeAndUser($user$routePermissionType);
  45.                 
  46.                 $configuratorPermissionType $this->em->getRepository(PermissionType::class)->findOneBy(['keyName'=>'permission.type.configurator']);
  47.                 $userConfiguratorPermissions $this->em->getRepository(Permission::class)->findByTypeAndUser($user$configuratorPermissionType);
  48.                 
  49.                 
  50.                 $userRoutesArr = [];
  51.                 $userPermissionsArr = [];
  52.                 
  53.                 if($user->getUserRoles()->contains($superAdmin)){
  54.                         $allRoutes $this->router->getRouteCollection();
  55.                         foreach ($allRoutes as $key => $route){
  56.                                 if(!str_contains($route->getPath(), '/test/') AND !str_starts_with($route->getPath(), '/_')){
  57.                                         $a = [];
  58.                                         $a['name'] = $key;
  59.                                         $a['path'] = $route->getPath();
  60.                                         $a['methods'] = $route->getMethods();
  61.                 
  62.                                         $userRoutesArr[$key] = $a;
  63.                                 }
  64.                         }
  65.                         foreach ($this->em->getRepository(Permission::class)->findBy(['type'=>$configuratorPermissionType]) as $configuratorPermission) {
  66.                                 $userPermissionsArr[] = $configuratorPermission->getKeyName();
  67.                         }
  68.                 
  69.                 }
  70.                 else{
  71.                         foreach ($userRoutePermissions as $routePermission) {
  72.                                 foreach ($routePermission->getRoutes() as $route)
  73.                                         if (!is_null($this->router->getRouteCollection()->get($route->getRouteName()))) {
  74.                                                 $a = [];
  75.                                                 $a['name'] = $route->getRouteName();
  76.                                                 $a['path'] = $this->router->getRouteCollection()->get($route->getRouteName())->getPath();
  77.                                                 $a['methods'] = $this->router->getRouteCollection()->get($route->getRouteName())->getMethods();
  78.                                                 
  79.                                                 $userRoutesArr[$route->getRouteName()] = $a;
  80.                                         }
  81.                         }
  82.                         
  83.                         foreach ($userConfiguratorPermissions as $configuratorPermission) {
  84.                                 $userPermissionsArr[] = $configuratorPermission->getKeyName();
  85.                         }
  86.                         
  87.                         //add default routes
  88.                         $defaultRoutes = ['translations_for_locale''product_list''translations''catalogue_categories''catalogue_categories_show',
  89.                                     'catalogue_group_type_show''catalogue_component_show''catalogue_components_list'];
  90.                         foreach ($defaultRoutes as $defaultRoute){
  91.                                 $route $this->router->getRouteCollection()->get($defaultRoute);
  92.                                 if (!is_null($route)) {
  93.                                         $a = [];
  94.                                         $a['name'] = $defaultRoute;
  95.                                         $a['path'] = $route->getPath();
  96.                                         $a['methods'] = $route->getMethods();
  97.                                         
  98.                                         $userRoutesArr[$defaultRoute] = $a;
  99.                                 }
  100.                         }
  101.                 }
  102.                 
  103.                 $userData = [];
  104.                 $userData['accname'] = $user->getName();
  105.                 $userData['company']['id'] = $user->getCompany()->getId();
  106.                 $userData['company']['name'] = $user->getCompany()->getName();
  107.                 $userData['username'] = $user->getEmail();
  108.                 $userData['manufacture'] = false;
  109.                 $userData['workplace'] = $user->getWorker()?->getTeam()?->getWorkplaces()?->first();
  110.                 $data = [
  111.                             'locale' => $user->getLocale(),
  112.                             'languages' => $languagesArr,
  113.                             'roles' => $user->getUserRoles(),
  114.                             'permissions' => [
  115.                                         'configurator' => $userPermissionsArr,
  116.                                         'routes' => $userRoutesArr,
  117.                                         => [
  118.                                                     'key' => 'manager_code',
  119.                                                     'type' => 'field',
  120.                                         ],
  121.                             ],
  122.                             'sidebar' => [[
  123.                                     '_name' => 'CSidebarNav',
  124.                                     '_children' => [[
  125.                                                 '_name' => 'CSidebarNavItem',
  126.                                                 'name' => 'Managers',
  127.                                                 'to' => '/managers',
  128.                                                 'fontIcon' => 'fa fa-users',
  129.                                                 'exact' => false,
  130.                                     ], [
  131.                                                 '_name' => 'CSidebarNavItem',
  132.                                                 'name' => 'Customers',
  133.                                                 'to' => '/customers',
  134.                                                 'fontIcon' => 'fa fa-cubes',
  135.                                                 'exact' => false,
  136.                                     ],
  137.                                     ],
  138.                         ]]
  139.                 ];
  140.                 
  141.                 $token $user->getValidApiToken();
  142.                 if (is_null($token)){
  143.                         $token = new ApiToken($user);
  144.                         $this->em->persist($token);
  145.                         $token $token->getToken();
  146.                 }
  147.                 $apiRefreshToken $user->getValidApiRefreshToken();
  148.                 $expiresAt $apiRefreshToken?->getExpiresAt()->getTimestamp();
  149.                 $refreshToken $apiRefreshToken?->getToken();
  150.                 
  151.                 if (is_null($apiRefreshToken)){
  152.                         $apiRefreshToken = new ApiRefreshToken($user);
  153.                         $this->em->persist($apiRefreshToken);
  154.                         $refreshToken $apiRefreshToken->getToken();
  155.                         $expiresAt $apiRefreshToken->getExpiresAt()->getTimestamp();
  156.                 }
  157.  
  158.                 $this->em->flush();
  159.                 $tokens = [];
  160.                 
  161.                 $tokens['token'] = $token;
  162.                 $tokens['refresh'] = $refreshToken;
  163.                 $tokens['expiresAt'] = $expiresAt;
  164.                 return $this->api->json(['token'=>$tokens'user' => $userData,'data' => $data'redirect'=>'/'],'');
  165.         }
  166. }