<?php
//------------------------------------------------------------------------------
// src/Security/SocietyGroupDirectoryVoter.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\SocietyGroup;
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 App\Services\Config\OptionConfigTools;
class SocietyGroupDirectoryVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
// Plan.io Task #4652 : Added IS_ACTIVE_HANDLE_FAV
const VIEW = "view_society_group_directory";
const EDIT = "edit_society_group_directory";
const IS_ACTIVE_HANDLE_FAV = "handle_society_group_fav_is_active";
const IS_GRANTED_CONSTANTS = array(
self::VIEW,
self::EDIT,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_VIEW = "society_group_directory_view";
const ACL_PERM_EDIT = "society_group_directory_edit";
//--------------------------------------------------------------------------------
public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools, OptionConfigTools $optionConfigTools)
{
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->optionConfigTools = $optionConfigTools;
$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 = null): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// Only vote on SocietyGroup objects inside this voter
if ($subject !== null && !$subject instanceof SocietyGroup)
{
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;
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_MISSION_PLUS))
{
return false;
}
// you know $subject is a SocietyGroup object, thanks to supports
/** @var SocietyGroup $societyGroup */
$societyGroup = $subject;
switch ($attribute)
{
case self::IS_ACTIVE_HANDLE_FAV:
{
return $this->optionConfigTools->isActive_HandleSocietyGroupFav($societyGroup);
}
case self::VIEW:
{
return $this->canView($user, $function, $societyGroup);
}
case self::EDIT:
{
return $this->canEdit($user, $function);
}
}
throw new \LogicException('This code should not be reached!');
}
private function canView(Access $access, AccessFunction $function, $societyGroup = null)
{
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
// If all are null, exit
if ($aclPerm === 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, all hope is lost
return false;
}
private function canEdit(Access $access, AccessFunction $function)
{
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
// If all are null, exit
if ($aclPerm === 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, all hope is lost
return false;
}
}