<?php
//------------------------------------------------------------------------------
// src/Security/ProductVoter.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\APIRest\AccessAPI;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Product\Charge;
use App\Entity\Product\Product;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\LogTools;
use App\Services\Config\ModuleTools;
use App\Services\Config\OptionConfigTools;
class ProductVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "product_is_active";
const IS_ACTIVE_ECO_BONUS = "eco_bonus_is_active"; // #4328
const ADD = "add_product";
const LISTING = "list_products";
const VIEW = "view_product";
const EDIT = "edit_product";
const DELETE = "delete_product";
const IMPORT = "import_products";
// Plan.io Task #3605
const EDIT_GHOST_PARAMS = "edit_product_ghost_params";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::IS_ACTIVE_ECO_BONUS,
self::ADD,
self::LISTING,
self::VIEW,
self::EDIT,
self::DELETE,
self::IMPORT,
// Plan.io Task #3605
self::EDIT_GHOST_PARAMS,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "product_add";
const ACL_PERM_LISTING = "product_list";
const ACL_PERM_VIEW = "product_view";
const ACL_PERM_EDIT = "product_edit";
const ACL_PERM_DELETE = "product_delete";
const ACL_PERM_IMPORT = "product_import";
//--------------------------------------------------------------------------------
public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools, OptionConfigTools $optionConfigTools, LogTools $logTools)
{
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->optionConfigTools = $optionConfigTools;
$this->logTools = $logTools;
$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
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// only vote on Product objects inside this voter
if ($subject !== null && (!$subject instanceof Product && !$subject instanceof Charge))
{
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();
}
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;
// Plan.io Task #3605
$this->currentGroup = $currentGroup;
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_PRODUCT))
{
return false;
}
// Check current group affectation
if ($subject !== null)
{
$subjectGroup = $subject->getSocietyGroup();
if ($subjectGroup === null)
return false;
if (!$currentGroup->equals($subjectGroup))
return false;
}
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::IS_ACTIVE_ECO_BONUS:
{
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_ECO_BONUS))
{
return false;
}
return true;
}
case self::IMPORT:
return $this->canImport($user, $function);
case self::ADD:
return $this->canAdd($user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::VIEW:
return $this->canView($user, $function);
case self::EDIT:
return $this->canEdit($subject, $user, $function);
case self::DELETE:
return $this->canDelete($subject, $user, $function);
// Plan.io task #3605
case self::EDIT_GHOST_PARAMS:
{
if ($subject instanceof Charge)
{
return false;
}
return $this->canEditGhostParams($subject, $user, $function);
}
}
throw new \LogicException('This code should not be reached!');
}
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(Access $access, AccessFunction $function)
{
// Get Acl_Permission
$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;
return $acl->getValue();
}
private function canList(Access $access, AccessFunction $function)
{
// Restrictions are also applied in the Controller
// But this helps speeding page loading if the access is not even allowed to load the page
// (ie. if it has no list privileges whatsoever)
// 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 list type can exist for the Products,
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canView(Access $access, AccessFunction $function)
{
// Several Acl_Permission exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
// If all are null, exit
if ($aclPerm === null)
return false;
// Get First one (view all)
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(Product|Charge $subject, Access $access, AccessFunction $function)
{
// Do not allow editing the free product
if ($subject instanceof Product)
{
if ($subject->isFree())
{
return false;
}
}
// Several Acl_Permission exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
// If all are null, exit
if ($aclPerm === null)
return false;
// Get First one (view all)
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;
}
// Plan.io task #3605
private function canEditGhostParams(Product $product, Access $access, AccessFunction $function)
{
// Deny on discounts
if ($product->isDiscount())
{
return false;
}
// Check OptionConfig
if (!$this->optionConfigTools->isActive_GhostInvoicing($this->currentGroup))
{
return false;
}
// Finally check edit permissions
return $this->canEdit($product, $access, $function);
}
private function canDelete(Product|Charge $subject, Access $access, AccessFunction $function)
{
// Do not allow editing the free product
if ($subject instanceof Product)
{
if ($subject->isFree())
{
return false;
}
}
// Several Acl_Permission exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE);
// If all are null, exit
if ($aclPerm === null)
return false;
// Get First one (view all)
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;
}
}