<?php
namespace App\Service;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Service\DynamicHostService;
use App\Entity\User;
use App\Enum\Role;
/**
* @author Anthony
*
*/
class SwitchConnexionService
{
private $security;
private $entityManagerInterface;
private $tokenStorage;
private $requestStack;
public function __construct(TokenStorageInterface $tokenStorage, RequestStack $requestStack,Security $security,EntityManagerInterface $entityManagerInterface,DynamicHostService $dynamicHostService)
{
$this->security = $security;
$this->entityManagerInterface = $entityManagerInterface;
$this->tokenStorage = $tokenStorage;
$this->requestStack = $requestStack;
$this->dynamicHostService = $dynamicHostService;
}
public function listOfAllAcountForUser(){
$currentUser = $this->security->getUser();
$originalEmail = $currentUser->getOriginalMailUser();
$parentUser = $currentUser?->getParent();
$linkToConnectAndRole = [];
if (null != $parentUser) {
foreach ($parentUser->getChildren() as $userChild) {
if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER' and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
$company = $userChild?->getCompany();
//create token for connexion
$token = hash('sha256', uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
$userChild->setOneTimeLoginToken($token);
$this->entityManagerInterface->persist($userChild);
$this->entityManagerInterface->flush();
if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
if ($userChild->getPartner() or $userChild->getSalary()) {
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
'user'=>$userChild,
'company'=>$company,
'role'=>$this->getLabelRole($userChild->getRoles()['0'])
];
}
}else{
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
'user'=>$userChild,
'company'=>$company,
'role'=>$this->getLabelRole($userChild->getRoles()['0'])
];
}
}
}
//for parent
$token = hash('sha256', uniqid(preg_replace('/\s/','-',$parentUser->getFullName())));
$parentUser->setOneTimeLoginToken($token);
$this->entityManagerInterface->persist($parentUser);
$this->entityManagerInterface->flush();
if ($originalEmail != $parentUser->getOriginalMailUser() and $parentUser->getRoles()['0'] != 'ROLE_USER'and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
if ($parentUser->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
if ($parentUser->getPartner() or $parentUser->getSalary()) {
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
'user'=>$parentUser,
'company'=>$parentUser?->getCompany(),
'role'=>$this->getLabelRole($parentUser->getRoles()[0])
];
}
}else{
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($parentUser?->getCompany())}/login/{$parentUser->getOneTimeLoginToken()}?role={$parentUser->getOriginalRoles()['0']}",
'user'=>$parentUser,
'company'=>$parentUser?->getCompany(),
'role'=>$this->getLabelRole($parentUser->getRoles()[0])
];
}
}
}else{
foreach ($currentUser->getChildren() as $userChild) {
if ($originalEmail != $userChild->getOriginalMailUser() and $userChild->getRoles()['0'] != 'ROLE_USER'and $userChild->getEnabled() == true and $userChild->getDeleted() == 0) {
$company = $userChild?->getCompany();
//create token for connexion
$token = hash('sha256', uniqid(preg_replace('/\s/','-',$userChild->getFullName())));
$userChild->setOneTimeLoginToken($token);
$this->entityManagerInterface->persist($userChild);
// $this->entityManagerInterface->flush();
if ($userChild->getRoles()['0'] == 'ROLE_SUBCONTRACTOR') {
if ($userChild->getPartner() or $userChild->getSalary()) {
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
'user'=>$userChild,
'company'=>$company,
'role'=>$this->getLabelRole($userChild->getRoles()['0'])
];
}
}else{
$linkToConnectAndRole[] = [
'urlForConnect' => "{$this->dynamicHostService->getBackUrlByCompany($company)}/login/{$userChild->getOneTimeLoginToken()}?role={$userChild->getRoles()['0']}",
'user'=>$userChild,
'company'=>$company,
'role'=>$this->getLabelRole($userChild->getRoles()['0'])
];
}
}
}
}
return $linkToConnectAndRole;
}
public function logoutUser()
{
$this->tokenStorage->setToken(null);
$session = $this->requestStack->getSession();
$session->invalidate();
dd('logout');
}
public function getRoleName(?User $user=null){
if($user == null){
return 'Utilisatateur.';
}
return $this->getLabelRole($user->getRoles()[0]);
}
public function getLabelRole($role){
switch ($role) {
case 'ROLE_ADMIN_AGENCY':
$roleLabel = ROLE::ROLE_ADMIN_AGENCY->label();
break;
case 'ROLE_MANAGER':
$roleLabel = ROLE::ROLE_MANAGER->label();
break;
case 'ROLE_ADMIN':
$roleLabel = ROLE::ROLE_ADMIN->label();
break;
case 'ROLE_SUBCONTRACTOR':
$roleLabel = ROLE::ROLE_SUBCONTRACTOR->label();
break;
case 'ROLE_CLIENT_ADMIN':
$roleLabel = ROLE::ROLE_CLIENT_ADMIN->label();
break;
case 'ROLE_CLIENT':
$roleLabel = ROLE::ROLE_CLIENT->label();
break;
default:
$roleLabel = "$role";
break;
}
return $roleLabel;
}
}