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