<?php
//------------------------------------------------------------------------------
// src/Security/HRVarsVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity\Access;
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\Config\OptionConfigTools;
class HRVarsVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = 'hr_vars_is_active';
const ADD_HR_VARIABLE = 'add_hr_variable';
const VIEW_HR_VARIABLE = 'view_hr_variable';
const EDIT_HR_VARIABLE = 'edit_hr_variable';
const EDIT_HR_VARIABLE_STATUS = 'edit_hr_variable_status';
const DELETE_HR_VARIABLE = 'delete_hr_variable';
const ADD_OP_VARIABLE = 'add_op_variable';
const VIEW_OP_VARIABLE = 'view_op_variable';
const EDIT_OP_VARIABLE = 'edit_op_variable';
const EDIT_OP_VARIABLE_STATUS = 'edit_op_variable_status';
const DELETE_OP_VARIABLE = 'delete_op_variable';
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD_HR_VARIABLE,
self::VIEW_HR_VARIABLE,
self::EDIT_HR_VARIABLE,
self::EDIT_HR_VARIABLE_STATUS,
self::DELETE_HR_VARIABLE,
self::ADD_OP_VARIABLE,
self::VIEW_OP_VARIABLE,
self::EDIT_OP_VARIABLE,
self::EDIT_OP_VARIABLE_STATUS,
self::DELETE_OP_VARIABLE,
);
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// acl constants
// HRVariable
const ACL_PERM_HR_VARIABLE_ADD = 'hr_variable_add';
const ACL_PERM_HR_VARIABLE_ADD_SOCIETY = 'hr_variable_add_society';
const ACL_PERM_HR_VARIABLE_VIEW = 'hr_variable_view';
const ACL_PERM_HR_VARIABLE_VIEW_SOCIETY = 'hr_variable_view_society';
const ACL_PERM_HR_VARIABLE_EDIT = 'hr_variable_edit';
const ACL_PERM_HR_VARIABLE_EDIT_SOCIETY = 'hr_variable_edit_society';
const ACL_PERM_HR_VARIABLE_EDIT_STATUS = 'hr_variable_edit_status';
const ACL_PERM_HR_VARIABLE_EDIT_STATUS_SOCIETY = 'hr_variable_edit_status_society';
const ACL_PERM_HR_VARIABLE_DELETE = 'hr_variable_delete';
const ACL_PERM_HR_VARIABLE_DELETE_SOCIETY = 'hr_variable_delete_society';
// OPVariable
const ACL_PERM_OP_VARIABLE_ADD = 'op_variable_add';
const ACL_PERM_OP_VARIABLE_ADD_SOCIETY = 'op_variable_add_society';
const ACL_PERM_OP_VARIABLE_VIEW = 'op_variable_view';
const ACL_PERM_OP_VARIABLE_VIEW_SOCIETY = 'op_variable_view_society';
const ACL_PERM_OP_VARIABLE_EDIT = 'op_variable_edit';
const ACL_PERM_OP_VARIABLE_EDIT_SOCIETY = 'op_variable_edit_society';
const ACL_PERM_OP_VARIABLE_EDIT_STATUS = 'op_variable_edit_status';
const ACL_PERM_OP_VARIABLE_EDIT_STATUS_SOCIETY = 'op_variable_edit_status_society';
const ACL_PERM_OP_VARIABLE_DELETE = 'op_variable_delete';
const ACL_PERM_OP_VARIABLE_DELETE_SOCIETY = 'op_variable_delete_society';
//--------------------------------------------------------------------------------
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;
}
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;
$this->currentGroup = $currentGroup;
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_HUMAN_RESOURCE))
{
return false;
}
// Option activated
if (!$this->optionConfigTools->isActive_HRVars($currentGroup))
{
return false;
}
// 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;
}
/** @var HumanResource $humanResource */
$humanResource = $subject;
if ($humanResource !== null && !$humanResource->hasHrVars())
{
return false;
}
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::ADD_HR_VARIABLE:
return $this->canAddHRVariable($humanResource, $user, $function);
case self::VIEW_HR_VARIABLE:
return $this->canViewHRVariable($humanResource, $user, $function);
case self::EDIT_HR_VARIABLE:
return $this->canEditHRVariable($humanResource, $user, $function);
case self::EDIT_HR_VARIABLE_STATUS:
return $this->canEditStatusHRVariable($humanResource, $user, $function);
case self::DELETE_HR_VARIABLE:
return $this->canDeleteHRVariable($humanResource, $user, $function);
case self::ADD_OP_VARIABLE:
return $this->canAddOPVariable($humanResource, $user, $function);
case self::VIEW_OP_VARIABLE:
return $this->canViewOPVariable($humanResource, $user, $function);
case self::EDIT_OP_VARIABLE:
return $this->canEditOPVariable($humanResource, $user, $function);
case self::EDIT_OP_VARIABLE_STATUS:
return $this->canEditStatusOPVariable($humanResource, $user, $function);
case self::DELETE_OP_VARIABLE:
return $this->canDeleteOPVariable($humanResource, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety(HumanResource $humanResource, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the HumanResource
$hrSociety = $humanResource->getSociety();
if ($hrSociety === null)
{
return false;
}
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $hrSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canAddHRVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_ADD);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_ADD_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewHRVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_VIEW_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditHRVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_EDIT_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditStatusHRVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_EDIT_STATUS);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_EDIT_STATUS_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDeleteHRVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_DELETE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HR_VARIABLE_DELETE_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canAddOPVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_ADD);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_ADD_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewOPVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_VIEW_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditOPVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_EDIT_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditStatusOPVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_EDIT_STATUS);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_EDIT_STATUS_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDeleteOPVariable(HumanResource $humanResource, Access $user, AccessFunction $function)
{
// Get AclPermission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_DELETE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_OP_VARIABLE_DELETE_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())
{
return $this->checkSociety($humanResource, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
}