src/Service/SwitchConnexionService.php line 26

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.         $originalEmail $currentUser->getOriginalMailUser();
  31.         $parentUser  $currentUser?->getParent();
  32.         $linkToConnectAndRole = [];
  33.         if (null != $parentUser) {
  34.             foreach ($parentUser->getChildren() as $userChild) {
  35.                 if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER' and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
  36.                     $company $userChild?->getCompany();
  37.                     if (empty($userChild->getOneTimeLoginToken()) or is_null($userChild->getOneTimeLoginToken())) {
  38.                         //create token for connexion
  39.                         $token hash('sha256'uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
  40.                         $userChild->setOneTimeLoginToken($token);
  41.                         $this->entityManagerInterface->persist($userChild);
  42.                         $this->entityManagerInterface->flush();
  43.                     }
  44.                     if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  45.                        
  46.                        if ($userChild->getPartner() or $userChild->getSalary()) {
  47.                            $linkToConnectAndRole[] = [
  48.                                             'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  49.                                             'user'=>$userChild,
  50.                                             'company'=>$company,
  51.                                             'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  52.                                           ];
  53.                        }
  54.                     }else{
  55.                          $linkToConnectAndRole[] = [
  56.                                     'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  57.                                     'user'=>$userChild,
  58.                                     'company'=>$company,
  59.                                     'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  60.                                   ];
  61.                     }
  62.                     
  63.                    
  64.                 }
  65.             }
  66.             if (is_null$parentUser->getOneTimeLoginToken()) or empty($parentUser->getOneTimeLoginToken()) ) {
  67.                 //for parent
  68.                 $token hash('sha256'uniqid(preg_replace('/\s/','-',$parentUser->getFullName())));
  69.                 $parentUser->setOneTimeLoginToken($token);
  70.                 $this->entityManagerInterface->persist($parentUser);
  71.                 $this->entityManagerInterface->flush();
  72.             }
  73.             if ($originalEmail != $parentUser->getOriginalMailUser()  and $parentUser->getRoles()['0'] != 'ROLE_USER'and $parentUser->getEnabled() == true and $parentUser->getDeleted() == 0) {
  74.                
  75.                  if ($parentUser->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  76.                     
  77.                        if ($parentUser->getPartner() or $parentUser->getSalary()) {
  78.                           $linkToConnectAndRole[] = [
  79.                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
  80.                                 'user'=>$parentUser,
  81.                                 'company'=>$parentUser?->getCompany(),
  82.                                 'role'=>$this->getLabelRole($parentUser->getRoles()[0])
  83.                         ];
  84.                        }
  85.                     }else{
  86.                         
  87.                         $linkToConnectAndRole[] = [
  88.                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
  89.                                 'user'=>$parentUser,
  90.                                 'company'=>$parentUser?->getCompany(),
  91.                                 'role'=>$this->getLabelRole($parentUser->getRoles()[0])
  92.                         ];
  93.                     }
  94.             }
  95.         }else{
  96.             foreach ($currentUser->getChildren() as $userChild) {
  97.                 if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER'and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
  98.                     $company $userChild?->getCompany();
  99.                     if (empty($userChild->getOneTimeLoginToken()) or is_null($userChild->getOneTimeLoginToken())) {
  100.                         //create token for connexion
  101.                         $token hash('sha256'uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
  102.                         $userChild->setOneTimeLoginToken($token);
  103.                         $this->entityManagerInterface->persist($userChild);
  104.                         $this->entityManagerInterface->flush();
  105.                     }
  106.                     if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
  107.                        
  108.                            if ($userChild->getPartner() or $userChild->getSalary()) {
  109.                                $linkToConnectAndRole[] = [
  110.                                                 'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  111.                                                 'user'=>$userChild,
  112.                                                 'company'=>$company,
  113.                                                 'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  114.                                               ];
  115.                            }
  116.                         }else{
  117.                              $linkToConnectAndRole[] = [
  118.                                         'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
  119.                                         'user'=>$userChild,
  120.                                         'company'=>$company,
  121.                                         'role'=>$this->getLabelRole($userChild->getRoles()['0'])
  122.                                       ];
  123.                         }
  124.                 }
  125.             }
  126.         }
  127.         
  128.         return $linkToConnectAndRole;
  129.     }
  130.     public function logoutUser()
  131.     {
  132.         $this->tokenStorage->setToken(null);
  133.         $session $this->requestStack->getSession();
  134.         $session->invalidate();
  135.         dd('logout');
  136.     }
  137.     public function getRoleName(?User $user=null){
  138.         if($user == null){
  139.              return 'Utilisatateur.';
  140.         }
  141.         return  $this->getLabelRole($user->getRoles()[0]);
  142.     }
  143.     public function getLabelRole($role){
  144.         switch ($role) {
  145.              case 'ROLE_ADMIN_AGENCY':
  146.                  $roleLabel ROLE::ROLE_ADMIN_AGENCY->label();
  147.                  break;
  148.              case 'ROLE_MANAGER':
  149.                  $roleLabel ROLE::ROLE_MANAGER->label();
  150.                  break;
  151.              case 'ROLE_ADMIN':
  152.                  $roleLabel ROLE::ROLE_ADMIN->label();
  153.                  break;
  154.              case 'ROLE_SUBCONTRACTOR':
  155.                  $roleLabel ROLE::ROLE_SUBCONTRACTOR->label();
  156.                  break;
  157.              case 'ROLE_CLIENT_ADMIN':
  158.                  $roleLabel ROLE::ROLE_CLIENT_ADMIN->label();
  159.                  break;
  160.             case 'ROLE_CLIENT':
  161.                 $roleLabel ROLE::ROLE_CLIENT->label();
  162.                 break;
  163.              default:
  164.                  $roleLabel "$role";
  165.                  break;
  166.          }
  167.          return $roleLabel;
  168.     }
  169. }