<?php
//------------------------------------------------------------------------------
// src/Security/HumanResourceVoter.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\HR\HumanResource;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
use App\Services\LogTools;
class HumanResourceVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "human_resource_is_active";
const ADD = "add_human_resource";
const LISTING = "list_human_resources";
const LISTING_SOCIETY = "list_human_resources_society";
const LISTING_ANY = "list_human_resources_any";
const VIEW = "view_human_resource";
const EDIT = "edit_human_resource";
const ANONYMIZE = "anonymize_human_resource";
const IMPORT = "import_human_resources";
const LISTING_ATTACHMENTS = "list_human_resource_attachments";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::ANONYMIZE,
self::IMPORT,
self::LISTING_ATTACHMENTS,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_IMPORT = "human_resource_import";
const ACL_PERM_ADD = "human_resource_add";
const ACL_PERM_LISTING = "human_resource_list";
const ACL_PERM_LISTING_SOCIETY = "human_resource_list_society";
const ACL_PERM_VIEW = "human_resource_view";
const ACL_PERM_VIEW_SOCIETY = "human_resource_view_society";
const ACL_PERM_EDIT = "human_resource_edit";
const ACL_PERM_EDIT_SOCIETY = "human_resource_edit_society";
const ACL_PERM_ANONYMIZE = "human_resource_anonymize";
const ACL_PERM_ANONYMIZE_SOCIETY = "human_resource_anonymize_society";
const ACL_PERM_ATTACHMENT_LIST = "rh_attachment_list";
const ACL_PERM_ATTACHMENT_LIST_SOCIETY = "rh_attachment_list_society";
const ACL_PERM_ATTACHMENT_LIST_BASE_SOCIETY = "rh_attachment_list_base_society";
//--------------------------------------------------------------------------------
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, ModuleTools $moduleTools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$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
{
// $this->logTools->ploopLog("[HumanResourceVoter][supports] attribute = $attribute");
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// only vote on HumanResource objects inside this voter
if ($subject !== null && !$subject instanceof HumanResource)
{
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_HUMAN_RESOURCE))
{
return false;
}
if ($attribute == self::EDIT)
{
if ($subject->getAnonymized())
{
return false;
}
}
// you know $subject is a HumanResource object, thanks to supports
/** @var HumanResource $rh */
$rh = $subject;
// Check current group affectation
if ($subject !== null)
{
$subjectSociety = $subject->getSociety();
if ($subjectSociety === null)
return false;
$subjectGroup = $subjectSociety->getGroup();
if ($subjectGroup === null)
return false;
if (!$currentGroup->equals($subjectGroup))
return false;
}
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::IMPORT:
return $this->canImport($user, $function);
case self::ADD:
return $this->canAdd($rh, $user, $function);
case self::LISTING:
return $this->canList($rh, $user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($rh, $user, $function);
case self::LISTING_ANY:
return $this->canListAny($rh, $user, $function);
case self::VIEW:
return $this->canView($rh, $user, $function);
case self::EDIT:
return $this->canEdit($rh, $user, $function);
case self::ANONYMIZE:
return $this->canAnonymize($rh, $user, $function);
case self::LISTING_ATTACHMENTS:
return $this->canListAttachments($rh, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $rh is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety(HumanResource $rh, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the HumanResource
$rhSociety = $rh->getSociety();
if ($rhSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $rhSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
// $access is the user trying to load the resource
// $rh is the resource being loaded
private function checkBaseSociety(HumanResource $rh, Access $access)
{
// Get the base society of the access
$baseSociety = $access->getSociety();
if ($baseSociety === null)
{
return false;
}
// Get the Society of the Human_Resource
$rhSociety = $rh->getSociety();
if ($rhSociety === null)
{
return false;
}
if ($baseSociety->equals($rhSociety))
{
return true;
}
return false;
}
private function canImport(Access $access, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_IMPORT);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
return $acl->getValue();
}
private function canAdd(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD);
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();
}
private function canList(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Get AclPermission
$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 acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canListSociety(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Get AclPermission
$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(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Two AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
// If both 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;
}
private function canView(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Two AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_SOCIETY);
// If both 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($rh, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(HumanResource $rh = null, Access $user, AccessFunction $function)
{
if($rh->getAnonymized())
{
return false;
}
// Two AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
// If both 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($rh, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canAnonymize(HumanResource $rh = null, Access $user, AccessFunction $function)
{
// Two AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ANONYMIZE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ANONYMIZE_SOCIETY);
// If both 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($rh, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canListAttachments(HumanResource $rh, Access $user, AccessFunction $function)
{
// Several Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ATTACHMENT_LIST);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ATTACHMENT_LIST_SOCIETY);
$aclPermBaseSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ATTACHMENT_LIST_BASE_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermBaseSociety === 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($rh, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermBaseSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermBaseSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkBaseSociety
return $this->checkBaseSociety($rh, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
}