<?php
//------------------------------------------------------------------------------
// src/Security/UserSpecificVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class UserSpecificVoter extends Voter
{
const TEST_ITASKS = "test_itasks";
const TEST_SEND_EMAIL_PROSPECT = "test_send_email_prospect";
const IS_GRANTED_CONSTANTS = array(
self::TEST_ITASKS,
self::TEST_SEND_EMAIL_PROSPECT,
);
const JOHANN = array(
'johann',
'jg.johanngilbert@gmail.com',
'jg.johann.gilbert@gmail.com',
'johann.gilbert@sofacile.fr',
);
const DEVS = array(
'oanalivia',
'alexandre',
'dylan',
'perrier',
);
const PSEUDO_DEVS = array(
'o.peyrat@dvlpr.fr',
'a.peyrat@dvlpr.fr',
'oanalivia@gmail.com',
'dylan.bernard@unilim.fr',
);
public function __construct(Security $security, ManagerRegistry $doctrine)
{
$this->security = $security;
$this->em = $doctrine->getManager();
}
// Plan.io Task #4453 [See AccessVoter for details]
public function supportsAttribute(string $attribute): bool
{
return in_array($attribute, self::IS_GRANTED_CONSTANTS, true);
}
protected function supports(string $attribute, $subject = null): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Access)
{
// the user must be logged in; if not, deny access
return false;
}
$impersonatorUser = null;
$token = $this->security->getToken();
if ($token instanceof SwitchUserToken)
{
$impersonatorUser = $token->getOriginalToken()->getUser();
}
switch ($attribute)
{
case self::TEST_ITASKS:
return $this->canTestITasks($user, $impersonatorUser);
case self::TEST_SEND_EMAIL_PROSPECT:
return $this->canTestSEndEmailProspect($user, $impersonatorUser);
}
throw new \LogicException('This code should not be reached!');
}
private function canTestITasks(Access $access, $impersonatorUser)
{
if (in_array($access->getUserIdentifier(), self::JOHANN))
{
return true;
}
if (in_array($access->getUserIdentifier(), self::DEVS))
{
return true;
}
if (in_array($access->getUserIdentifier(), self::PSEUDO_DEVS))
{
return true;
}
if ($impersonatorUser !== null)
{
if (in_array($impersonatorUser->getUserIdentifier(), self::JOHANN))
{
return true;
}
if (in_array($impersonatorUser->getUserIdentifier(), self::DEVS))
{
return true;
}
if (in_array($impersonatorUser->getUserIdentifier(), self::PSEUDO_DEVS))
{
return true;
}
}
return false;
}
private function canTestSEndEmailProspect(Access $access, $impersonatorUser)
{
if (in_array($access->getUserIdentifier(), self::JOHANN))
{
return true;
}
if (in_array($access->getUserIdentifier(), self::DEVS))
{
return true;
}
if (in_array($access->getUserIdentifier(), self::PSEUDO_DEVS))
{
return true;
}
if ($impersonatorUser !== null)
{
if (in_array($impersonatorUser->getUserIdentifier(), self::JOHANN))
{
return true;
}
if (in_array($impersonatorUser->getUserIdentifier(), self::DEVS))
{
return true;
}
if (in_array($impersonatorUser->getUserIdentifier(), self::PSEUDO_DEVS))
{
return true;
}
}
return false;
}
}