<?php
//------------------------------------------------------------------------------
// src/Security/AclVoter.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 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;
class AclVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const VIEW = "acl_view";
const EDIT = "acl_edit";
const ACCESS = "acl_view_or_edit";
const IS_GRANTED_CONSTANTS = array(
self::VIEW,
self::EDIT,
self::ACCESS,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_VIEW = "acl_view";
const ACL_PERM_EDIT = "acl_edit";
//--------------------------------------------------------------------------------
public function __construct(ManagerRegistry $doctrine)
{
$this->em = $doctrine->getManager();
$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($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;
// The Admin of the SocietyGroup should always have access to this
if ($user->equals($currentGroup->getAdmin()))
{
return true;
}
// Access to Settings Page (view or edit)
if ($attribute == self::ACCESS)
{
return $this->canDo(self::VIEW, $user, $function) || $this->canDo(self::EDIT, $user, $function);
}
// Edit or View Access
return $this->canDo($attribute, $user, $function);
throw new \LogicException('This code should not be reached!');
}
private function canDo($attribute, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName($attribute);
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
return $acl->getValue();
}
}