<?php
//------------------------------------------------------------------------------
// src/Security/TemplateVoter.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\Ikea\TemplateProduct as IkeaTemplateProduct;
use App\Entity\Product\Template;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Devis\DevisProduct;
use App\Entity\Platform\Invoice\InvoiceProduct;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class TemplateVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "template_is_active";
const ADD = "add_template";
const LISTING = "list_templates";
const VIEW = "view_template";
const EDIT = "edit_template";
const DELETE = "delete_template";
const DELETE_WITH_PRODUCTS = "delete_template_and_products";
// Plan.io Task #4561
const CHANGE_LINEAR_METERS_USAGE = "change_linear_meters_usage";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::LISTING,
self::VIEW,
self::EDIT,
self::DELETE,
self::DELETE_WITH_PRODUCTS,
self::CHANGE_LINEAR_METERS_USAGE,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "template_add";
const ACL_PERM_LISTING = "template_list";
const ACL_PERM_VIEW = "template_view";
const ACL_PERM_EDIT = "template_edit";
const ACL_PERM_DELETE = "template_delete";
//--------------------------------------------------------------------------------
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);
}
// 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 Template objects inside this voter
if ($subject !== null && !$subject instanceof Template)
{
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 ?
// The module is Product, it includes the Templates
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_PRODUCT))
{
return false;
}
// you know $subject is a Template object, thanks to supports
/** @var Template $template */
$template = $subject;
// 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::ADD:
return $this->canAdd($user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::VIEW:
return $this->canView($template, $user, $function);
case self::EDIT:
return $this->canEdit($template, $user, $function);
case self::DELETE:
return $this->canDelete($template, $user, $function);
case self::DELETE_WITH_PRODUCTS:
return $this->canDeleteWithProducts($template, $user, $function);
case self::CHANGE_LINEAR_METERS_USAGE:
return $this->canChangeLinearMetersUsage($template, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
private function canChangeLinearMetersUsage(Template $template, Access $access, AccessFunction $function)
{
// If the template has an associated Method Ikea Template => Deny
$ikeaTemplates = $this->em->getRepository(IkeaTemplateProduct::class)
->getIkeaTemplatesForPlatformTemplate($template);
if (!empty($ikeaTemplates))
{
foreach ($ikeaTemplates as $ikeaTemplate)
{
if ($ikeaTemplate->isMethod())
{
return false;
}
}
}
// At least one Devis exists ?
$devis = $this->em->getRepository(Devis::class)->findOneByTemplate($template);
if ($devis !== null)
{
return false;
}
// All looks good => Load normal permissions
return $this->canEdit($template, $access, $function);
}
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 Templates,
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canView(Template $template, 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(Template $template, Access $access, AccessFunction $function)
{
if (!$template->getIsActive())
{
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;
}
private function canDelete(Template $template, Access $access, AccessFunction $function)
{
// Do not allow deleting if in use
$devis = $this->em->getRepository(Devis::class)->findOneByTemplate($template);
if ($devis !== null)
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;
}
private function canDeleteWithProducts(Template $template, Access $access, AccessFunction $function)
{
// Do not allow deleting if in use
$devis = $this->em->getRepository(Devis::class)->findOneByTemplate($template);
if ($devis !== null)
return false;
// Do not allow deleting products if at least one is in use
foreach ($template->getProducts() as $product)
{
$devisProduct = $this->em->getRepository(DevisProduct::class)->findOneByOriginalProduct($product);
if ($devisProduct !== null)
return false;
$invoiceProduct = $this->em->getRepository(InvoiceProduct::class)->findOneByOriginalProduct($product);
if ($invoiceProduct !== null)
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;
}
}