<?php
//------------------------------------------------------------------------------
// src/Security/DevVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use App\Entity\Access;
use App\Entity\Security\Acl;
class DevVoter extends Voter
{
// Icon to use : <i class="las la-ghost"></i>
const OPEN_SESAME = "open_sesame";
const OPEN_SESAME_OANALIVIA = "open_sesame_oanalivia";
const OPEN_SESAME_ALEX = "open_sesame_alex";
const OPEN_SESAME_DYLAN = "open_sesame_dylan";
const UNDER_CONSTRUCTION = "under_construction";
// This includes Johann
// Icon to use : <i class="fa fa-asterisk"></i>
const REKAPP_ADMIN = "rekapp_admin";
const TYPES = array(
self::OPEN_SESAME,
self::OPEN_SESAME_OANALIVIA,
self::OPEN_SESAME_ALEX,
self::OPEN_SESAME_DYLAN,
self::UNDER_CONSTRUCTION,
self::REKAPP_ADMIN, // This includes Johann
);
const DEVS = array(
'oanalivia',
'alexandre',
'dylan',
'perrier',
);
const DEV_ALEX = array(
'alexandre',
'a.peyrat@dvlpr.fr',
);
const DEV_OANALIVIA = array(
'oanalivia',
'o.peyrat@dvlpr.fr',
'oanalivia@gmail.com',
);
const DEV_DYLAN = array(
'dylan',
'dylan.bernard@unilim.fr',
);
// TODO
// 12/12/2022 : For docker local
const LOCAL_DEV = array(
'superadmin'
);
const PSEUDO_DEVS = array(
'o.peyrat@dvlpr.fr',
'a.peyrat@dvlpr.fr',
'oanalivia@gmail.com',
'dylan.bernard@unilim.fr',
);
const UNDER_CONSTRUCTION_WORKERS = array(
'o.peyrat@dvlpr.fr',
'a.peyrat@dvlpr.fr',
'oanalivia@gmail.com',
'oanalivia',
'dylan'
);
const REKAPP_ADMIN_USERS = array(
'oanalivia',
'alexandre',
'dylan',
'johann',
'perrier',
);
public function __construct(ManagerRegistry $doctrine)
{
$this->em = $doctrine->getManager();
}
// Plan.io Task #4453 [See AccessVoter for details]
public function supportsAttribute(string $attribute): bool
{
return in_array($attribute, self::TYPES, true);
}
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::TYPES))
{
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;
if ($token instanceof SwitchUserToken)
{
$impersonatorUser = $token->getOriginalToken()->getUser();
}
$userToUse = $user;
if ($impersonatorUser !== null and $impersonatorUser instanceof Access)
{
$userToUse = $impersonatorUser;
}
switch ($attribute)
{
case self::OPEN_SESAME:
return $this->openSesame($userToUse);
case self::OPEN_SESAME_OANALIVIA:
return $this->openSesameOanaLivia($userToUse);
case self::OPEN_SESAME_ALEX:
return $this->openSesameAlex($userToUse);
case self::OPEN_SESAME_DYLAN:
return $this->openSesameDylan($userToUse);
case self::UNDER_CONSTRUCTION:
return $this->underConstruction($userToUse);
case self::REKAPP_ADMIN:
return $this->rekappAdmin($userToUse);
}
throw new \LogicException('This code should not be reached!');
}
private function openSesame(Access $access)
{
if (in_array($access->getUsername(), self::DEVS))
{
return true;
}
if (in_array($access->getUsername(), self::PSEUDO_DEVS))
{
return true;
}
if (in_array($access->getUsername(), self::DEV_ALEX))
{
return true;
}
if (in_array($access->getUsername(), self::DEV_OANALIVIA))
{
return true;
}
if (in_array($access->getUsername(), self::DEV_DYLAN))
{
return true;
}
// TODO
// if (in_array($access->getUsername(), self::LOCAL_DEV))
// {
// if ($this->localRealm == 1)
// {
// return true;
// }
// }
return false;
}
private function openSesameOanaLivia(Access $access)
{
if (in_array($access->getUsername(), self::DEV_OANALIVIA))
{
return true;
}
return false;
}
private function openSesameAlex(Access $access)
{
if (in_array($access->getUsername(), self::DEV_ALEX))
{
return true;
}
return false;
}
private function openSesameDylan(Access $access)
{
if (in_array($access->getUsername(), self::DEV_DYLAN))
{
return true;
}
return false;
}
private function underConstruction(Access $access)
{
if (in_array($access->getUsername(), self::UNDER_CONSTRUCTION_WORKERS))
{
return true;
}
return false;
}
private function rekappAdmin(Access $access)
{
if (in_array($access->getUsername(), self::REKAPP_ADMIN_USERS))
{
return true;
}
return false;
}
}