<?php
//------------------------------------------------------------------------------
// src/Security/LogVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\Common\Collections\ArrayCollection;
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;
// use Icod\PlatformBundle\Entity\Log;
class LogVoter extends Voter
{
const LISTING = "list_logs";
const LISTING_SOCIETY = "list_logs_society";
const LISTING_ANY = "list_logs_any";
const IS_GRANTED_CONSTANTS = array(
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_LISTING = "log_list";
const ACL_PERM_LISTING_SOCIETY = "log_list_society";
public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools)
{
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->aclRepository = $this->em->getRepository(Acl::class);
$this->aclPermissionRepository = $this->em->getRepository(AclPermission::class);
}
// 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): 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;
}
// The user must have a function; if not deny access
$function = $user->getFunction();
if ($function === null) return false;
// Plan.io Task #3710 : Get current group
$currentGroup = $user->getSocietyGroup();
if ($currentGroup === null)
return false;
// you know $subject is a Log object, thanks to supports
/** @var Log $log */
$log = $subject;
switch ($attribute)
{
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// Check if one of the societies of the access
// belongs to the societies of the action's group
private function checkSociety(Log $log, Access $access)
{
// Get all the societies of the access
$accessSocieties = $access->getSocieties();
if (count($accessSocieties) < 1)
return false;
// Get the corresponding groups
$accessGroups = new \Doctrine\Common\Collections\ArrayCollection();
foreach ($accessSocieties as $s)
if ($s->getGroup() !== null)
if ($accessGroups->contains($s->getGroup()) == false)
$accessGroups[] = $s->getGroup();
$logAccess = $log->getAccess();
if ($logAccess !== null)
{
// Logs from PLATFORM
// Get the societies for the action's access
$logAccessSocieties = $logAccess->getSocieties();
if (count($logAccessSocieties) < 1)
return false;
// Get the corresponding groups
$logAccessGroups = new \Doctrine\Common\Collections\ArrayCollection();
foreach ($logAccessSocieties as $s)
if ($s->getGroup() !== null)
if ($logAccessGroups->contains($s->getGroup()) == false)
$logAccessGroups[] = $s->getGroup();
foreach ($accessGroups as $as)
{
foreach ($logAccessGroups as $ps)
{
if ($as->getId() == $ps->getId())
{
return true;
}
}
}
return false;
}
return false;
}
private function canList(Access $access, AccessFunction $function)
{
// Restrictions are also applied in the Controller
// But this helps speeding page loading if the access is not even allowed to load the page
// (ie. if it has no list privileges whatsoever)
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one list type can exist for the Logs,
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canListSociety(Access $access, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one acl type can exist
// we can return the result of the acl_permission
// Further filtering is done in the Controller
return $acl->getValue();
}
private function canListAny(Access $access, AccessFunction $function)
{
// Several Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here it means that nothing good has been found
// Load second permission
if ($aclPermSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here, all hope is lost
return false;
}
}