<?php
//------------------------------------------------------------------------------
// src/Security/EInvoicingVoter.php
// Plan.io Task #4634 — E-invoicing module ACL
//------------------------------------------------------------------------------
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\HR\AccessFunction;
use App\Entity\APIRest\AccessAPI;
use App\Entity\Config\Module;
use App\Entity\EInvoicing\EInvoicingInboundInvoice;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class EInvoicingVoter extends Voter
{
const IS_ACTIVE = 'einvoicing_is_active';
// Plan.io Task #4634 — inbound invoices received from a PDP
const VIEW_INBOUND = 'view_inbound_einvoice';
const EDIT_INBOUND = 'edit_inbound_einvoice';
const LISTING_INBOUND = "list_inbound_einvoices";
const LISTING_INBOUND_SOCIETY = "list_inbound_einvoices_society";
const LISTING_INBOUND_ANY = "list_inbound_einvoices_any";
const IS_GRANTED_CONSTANTS = [
self::IS_ACTIVE,
self::VIEW_INBOUND,
self::EDIT_INBOUND,
self::LISTING_INBOUND,
self::LISTING_INBOUND_SOCIETY,
self::LISTING_INBOUND_ANY,
];
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_LISTING = "einvoicing_list";
const ACL_PERM_LISTING_SOCIETY = "einvoicing_list_society";
const ACL_PERM_LISTING_ANY = "einvoicing_list_any";
const ACL_PERM_VIEW = "einvoicing_view";
const ACL_PERM_VIEW_SOCIETY = "einvoicing_view_society";
const ACL_PERM_EDIT = "einvoicing_edit";
const ACL_PERM_EDIT_SOCIETY = "einvoicing_edit_society";
public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools)
{
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->aclRepository = $this->em->getRepository(Acl::class);
$this->aclPermissionRepository = $this->em->getRepository(AclPermission::class);
}
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;
}
// only vote on Invoice objects inside this voter
if ($subject !== null && !($subject instanceof EInvoicingInboundInvoice))
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// Plan.io Task #3707
if ($user instanceof AccessAPI)
{
if ($user->getAccess() === null)
{
return false;
}
$user = $user->getAccess();
}
// Plan.io Task #3707
// At this point $user is an object of Access type
// even if the $token->getUser() is AccessAPI
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;
}
$currentGroup = $user->getSocietyGroup();
if ($currentGroup === null)
{
return false;
}
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_EINVOICING))
{
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;
}
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::LISTING_INBOUND:
return $this->canList($user, $function);
case self::LISTING_INBOUND_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_INBOUND_ANY:
return $this->canListAny($user, $function);
case self::VIEW_INBOUND:
return $this->canView($subject, $user, $function);
case self::EDIT_INBOUND:
return $this->canEdit($subject, $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(EInvoicingInboundInvoice $eInvoice, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Cost
$eInvoiceSociety = $eInvoice->getSociety();
if ($eInvoiceSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $eInvoiceSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canView(EInvoicingInboundInvoice $eInvoice, Access $user, AccessFunction $function)
{
// Get Acl_Permissions
$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())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($eInvoice, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(EInvoicingInboundInvoice $eInvoice, Access $user, AccessFunction $function)
{
// Get Acl_Permissions
$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())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($eInvoice, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canList(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$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(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$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(Access $user, AccessFunction $function)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_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;
}
}