<?php
//------------------------------------------------------------------------------
// src/Security/DemandVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
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\Platform\Demand\Demand;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class DemandVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "demand_is_active";
const ADD = "add_demand";
const CONVERT_TO_MISSION = "convert_demand_to_mission";
const LINK_TO_MISSION = "link_demand_to_mission";
const LISTING = "list_demands";
const LISTING_SOCIETY = "list_demands_society";
const LISTING_ANY = "list_demands_any";
const VIEW = "view_demand";
const EDIT = "edit_demand";
const EDIT_SOCIETY = "edit_demand_society";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::CONVERT_TO_MISSION,
self::LINK_TO_MISSION,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::EDIT_SOCIETY,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = 'demand_add';
const ACL_PERM_LIST = 'demand_list';
const ACL_PERM_LIST_SOCIETY = 'demand_list_society';
const ACL_PERM_VIEW = 'demand_view';
const ACL_PERM_VIEW_SOCIETY = 'demand_view_society';
const ACL_PERM_EDIT = 'demand_edit';
const ACL_PERM_EDIT_SOCIETY = 'demand_edit_society';
const ACL_PERM_SOCIETY_EDIT = 'demand_society_edit';
const ACL_PERM_SOCIETY_EDIT_SOCIETY = 'demand_society_edit_society';
//--------------------------------------------------------------------------------
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine, ModuleTools $moduleTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$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 = 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 Demand objects inside this voter
if ($subject !== null && !$subject instanceof Demand)
{
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_DEMAND))
{
return false;
}
// you know $subject is a Demand object, thanks to supports
/** @var Demand $demand */
$demand = $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)
{
// Check is done before, in the voteOnAttribute
case self::IS_ACTIVE:
return true;
case self::ADD:
return $this->canAdd($demand, $user, $function);
case self::CONVERT_TO_MISSION:
return $this->canConvertToMission($demand, $user, $function, $token);
case self::LINK_TO_MISSION:
return $this->canLinkToMission($demand, $user, $function, $token);
case self::VIEW:
return $this->canView($demand, $user, $function);
case self::EDIT:
return $this->canEdit($demand, $user, $function);
case self::EDIT_SOCIETY:
return $this->canEditSociety($demand, $user, $function);
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!');
}
// $access is the user trying to load the resource
// $demand is the resource being loaded
// Check if the Author of the Demand is the Access
private function checkAuthor(Demand $demand, Access $access)
{
// Get the Society of the Demand
$demandAuthor = $demand->getAuthor();
if ($demandAuthor === null)
return false;
if ($access->getId() == $demandAuthor->getId())
return true;
return false;
}
// Check if the Society of the Demand
// belongs to the societies of the $access
private function checkSociety(Demand $demand, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Demand
$demandSociety = $demand->getSociety();
if ($demandSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $demandSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canAdd(Demand $demand = null, Access $user, AccessFunction $function)
{
// Get Acl_Perdemand
$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 canConvertToMission(Demand $demand, Access $user, AccessFunction $function, $token)
{
if ($demand->getClient() === null)
{
// This should not happen
return false;
}
if ($demand->getMission() !== null)
{
// Already attached to a mission
return false;
}
if ($demand->getResultingMission() !== null)
{
// Already converted to a mission
return false;
}
// Request permission to add missions
$canAddMissions = $this->accessDecisionManager->decide($token, ['add_mission']);
// Request permission to edit this demand
$canEditDemand = $this->canEdit($demand, $user, $function);
// 27/10/2022 : For the moment don't allow other demand than jcafLocal
$isJcafLocal = false;
if ($demand->getType() !== null)
{
$isJcafLocal = $demand->getType()->getJcafLocal();
}
if ($canAddMissions && $canEditDemand && $isJcafLocal)
{
return true;
}
return false;
}
private function canLinkToMission(Demand $demand, Access $user, AccessFunction $function, $token)
{
if ($demand->getClient() === null)
{
// This should not happen
return false;
}
if ($demand->getMission() !== null)
{
// Already attached to a mission
return false;
}
if ($demand->getResultingMission() !== null)
{
// Already converted to a mission
return false;
}
// Request permission to edit this demand
$canEditDemand = $this->canEdit($demand, $user, $function);
if ($canEditDemand)
{
return true;
}
return false;
}
private function canView(Demand $demand = null, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($demand, $user))
{
return true;
}
else
{
return $this->checkAuthor($demand, $user);
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Demand $demand = null, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($demand, $user))
{
return true;
}
else
{
return $this->checkAuthor($demand, $user);
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditSociety(Demand $demand = null, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($demand, $user))
{
return true;
}
else
{
return $this->checkAuthor($demand, $user);
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canList(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
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(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_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
return $acl->getValue();
}
private function canListAny(Access $user, AccessFunction $function)
{
// Three Acl_Perdemand may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_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;
}
}