src/Service/SwitchConnexionService.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\Security\Core\Security;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use App\Service\DynamicHostService;
  8. use App\Entity\User;
  9. use App\Enum\Role;
  10. /**
  11.  * @author Anthony 
  12.  * 
  13.  */
  14. class SwitchConnexionService
  15. {
  16.     private $security;
  17.     private $entityManagerInterface;
  18.      private $tokenStorage;
  19.     private $requestStack;
  20.     public function __construct(TokenStorageInterface $tokenStorageRequestStack $requestStack,Security $security,EntityManagerInterface $entityManagerInterface,DynamicHostService $dynamicHostService)
  21.     {
  22.         $this->security $security;
  23.         $this->entityManagerInterface $entityManagerInterface;
  24.         $this->tokenStorage $tokenStorage;
  25.         $this->requestStack $requestStack;
  26.         $this->dynamicHostService $dynamicHostService;
  27.     }
  28.     public function listOfAllAcountForUser(){
  29.          $currentUser $this->security->getUser();
  30.         if($currentUser === null){
  31.             return []; 
  32.         }
  33.         
  34.         $originalEmail $currentUser->getOriginalMailUser();
  35.         $parentUser  $currentUser?->getParent();
  36.         $linkToConnectAndRole = [];
  37.         if (null != $parentUser) {
  38.             foreach ($parentUser->getChildren() as $userChild) {
  39.                 if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER' and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
  40.                     $company $userChild?->getCompany();
  41.                     if (empty($userChild->getOneTimeLoginToken()) or is_null($userChild->getOneTimeLoginToken())) {
  42.                         //create token for connexion
  43.                         $token hash('sha256'uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
  44.                         $userChild->setOneTimeLoginToken($token);
  45.                         $this->entityManagerInterface->persist($userChild);
  46.                         $this->entityManagerInterface->flush();
  47.                     }
  48.                     if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  49.                        
  50.                        if ($userChild->getPartner() or $userChild->getSalary()) {
  51.                            $linkToConnectAndRole[] = [
  52.                                             'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  53.                                             'user'=>$userChild,
  54.                                             'company'=>$company,
  55.                                             'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  56.                                           ];
  57.                        }
  58.                     }else{
  59.                          $linkToConnectAndRole[] = [
  60.                                     'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  61.                                     'user'=>$userChild,
  62.                                     'company'=>$company,
  63.                                     'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  64.                                   ];
  65.                     }
  66.                     
  67.                    
  68.                 }
  69.             }
  70.             if (is_null$parentUser->getOneTimeLoginToken()) or empty($parentUser->getOneTimeLoginToken()) ) {
  71.                 //for parent
  72.                 $token hash('sha256'uniqid(preg_replace('/\s/','-',$parentUser->getFullName())));
  73.                 $parentUser->setOneTimeLoginToken($token);
  74.                 $this->entityManagerInterface->persist($parentUser);
  75.                 $this->entityManagerInterface->flush();
  76.             }
  77.             if ($originalEmail != $parentUser->getOriginalMailUser()  and $parentUser->getRoles()['0'] != 'ROLE_USER'and $parentUser->getEnabled() == true and $parentUser->getDeleted() == 0) {
  78.                
  79.                  if ($parentUser->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  80.                     
  81.                        if ($parentUser->getPartner() or $parentUser->getSalary()) {
  82.                           $linkToConnectAndRole[] = [
  83.                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
  84.                                 'user'=>$parentUser,
  85.                                 'company'=>$parentUser?->getCompany(),
  86.                                 'role'=>$this->getLabelRole($parentUser->getRoles()[0])
  87.                         ];
  88.                        }
  89.                     }else{
  90.                         
  91.                         $linkToConnectAndRole[] = [
  92.                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
  93.                                 'user'=>$parentUser,
  94.                                 'company'=>$parentUser?->getCompany(),
  95.                                 'role'=>$this->getLabelRole($parentUser->getRoles()[0])
  96.                         ];
  97.                     }
  98.             }
  99.         }else{
  100.             foreach ($currentUser->getChildren() as $userChild) {
  101.                 if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER'and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
  102.                     $company $userChild?->getCompany();
  103.                     if (empty($userChild->getOneTimeLoginToken()) or is_null($userChild->getOneTimeLoginToken())) {
  104.                         //create token for connexion
  105.                         $token hash('sha256'uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
  106.                         $userChild->setOneTimeLoginToken($token);
  107.                         $this->entityManagerInterface->persist($userChild);
  108.                         $this->entityManagerInterface->flush();
  109.                     }
  110.                     if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  111.                        
  112.                            if ($userChild->getPartner() or $userChild->getSalary()) {
  113.                                $linkToConnectAndRole[] = [
  114.                                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  115.                                                 'user'=>$userChild,
  116.                                                 'company'=>$company,
  117.                                                 'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  118.                                               ];
  119.                            }
  120.                         }else{
  121.                              $linkToConnectAndRole[] = [
  122.                                         'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  123.                                         'user'=>$userChild,
  124.                                         'company'=>$company,
  125.                                         'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  126.                                       ];
  127.                         }
  128.                 }
  129.             }
  130.         }
  131.         
  132.         return $linkToConnectAndRole;
  133.     }
  134.     public function logoutUser()
  135.     {
  136.         $this->tokenStorage->setToken(null);
  137.         $session $this->requestStack->getSession();
  138.         $session->invalidate();
  139.         dd('logout');
  140.     }
  141.     public function getRoleName(?User $user=null){
  142.         if($user == null){
  143.              return 'Utilisatateur.';
  144.         }
  145.         return  $this->getLabelRole($user->getRoles()[0]);
  146.     }
  147.     public function getLabelRole($role){
  148.         switch ($role) {
  149.              case 'ROLE_ADMIN_AGENCY':
  150.                  $roleLabel ROLE::ROLE_ADMIN_AGENCY->label();
  151.                  break;
  152.              case 'ROLE_MANAGER':
  153.                  $roleLabel ROLE::ROLE_MANAGER->label();
  154.                  break;
  155.              case 'ROLE_ADMIN':
  156.                  $roleLabel ROLE::ROLE_ADMIN->label();
  157.                  break;
  158.              case 'ROLE_SUBCONTRACTOR':
  159.                  $roleLabel ROLE::ROLE_SUBCONTRACTOR->label();
  160.                  break;
  161.              case 'ROLE_CLIENT_ADMIN':
  162.                  $roleLabel ROLE::ROLE_CLIENT_ADMIN->label();
  163.                  break;
  164.             case 'ROLE_CLIENT':
  165.                 $roleLabel ROLE::ROLE_CLIENT->label();
  166.                 break;
  167.              default:
  168.                  $roleLabel "$role";
  169.                  break;
  170.          }
  171.          return $roleLabel;
  172.     }
  173. }