<?php
//------------------------------------------------------------------------------
// src/Security/SocietyGroupInvoiceVoter.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\Platform\Devis\Devis;
use App\Entity\Platform\SocietyGroup\SocietyGroupInvoice;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class SocietyGroupInvoiceVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const ADD = "add_society_group_invoice";
const VIEW = "view_society_group_invoice";
const VIEW_PDF = "view_pdf_society_group_invoice";
const EDIT = "edit_society_group_invoice";
const EDIT_RECEIVER_SOCIETY = "edit_society_group_invoice_receiver_society";
// This one is tricky
// We don't actually care for permissions here
// We just want to know if there is anything to display
// If the SocietyGroup has no SocietyGroupInvoices, there is no need to even show the tab
const LIST = "list_society_group_invoices";
const IS_GRANTED_CONSTANTS = array(
self::ADD,
self::VIEW,
self::VIEW_PDF,
self::EDIT,
self::EDIT_RECEIVER_SOCIETY,
self::LIST,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_VIEW = "society_group_invoice_view";
const ACL_PERM_VIEW_PDF = "society_group_invoice_view_pdf";
const ACL_PERM_EDIT = "society_group_invoice_edit";
const ACL_PERM_LIST = "society_group_invoice_list";
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 = 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 Invoice / Devis objects inside this voter
if ($subject !== null && !($subject instanceof SocietyGroupInvoice || $subject instanceof Devis))
{
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;
$this->devis = null;
$this->invoice = null;
$this->emitter = null;
$this->receiver = null;
if ($subject instanceof SocietyGroupInvoice)
{
$this->invoice = $subject;
$this->emitter = $subject->getEmitter();
$this->receiver = $subject->getReceiver();
if ($this->emitter === null || $this->receiver === null)
{
return false;
}
}
switch ($attribute)
{
// canAdd : subject is Devis
case self::ADD:
return $this->canAdd($subject);
case self::LIST:
return $this->canList($user, $function);
// for all those below : subject is Invoice
case self::VIEW:
return $this->canView($subject, $user, $function);
case self::VIEW_PDF:
return $this->canViewPdf($subject, $user, $function);
case self::EDIT:
return $this->canEdit($subject, $user, $function);
case self::EDIT_RECEIVER_SOCIETY:
return $this->canEditReceiverSociety($subject, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
private function canAdd(Devis $devis)
{
// Deny for annulled and refused
if ($devis->isAnnulled() || $devis->isRefused())
{
return false;
}
// Only JCAF SocietyGroup can add a SocietyGroupInvoice for now
if ($this->currentGroup->isNotJcaf())
{
return false;
}
// Only for JCAF Devis with commission
// Plan.io Task #3229 Commissions
$mission = $devis->getMission();
if ($mission === null)
{
return false;
}
// Deny actions on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// Only for JCAF Devis with commission
// Plan.io Task #3229 Commissions
// Restrictions on Devis
if ($devis !== null)
{
if ($devis->isNotJcaf())
{
return false;
}
if (empty($devis->getShareCommission()))
{
return false;
}
}
// Do we already have a SocietyGroupInvoice for this Devis ?
$invoice = $this->em->getRepository(SocietyGroupInvoice::class)->findOneByDevis($devis);
if ($invoice !== null)
{
return false;
}
// Restrictions on Mission
if ($mission !== null)
{
if ($mission->getSocietyGroupAuthor() === null)
{
return false;
}
if ($mission->getSocietyGroupAuthor()->isNotJcaf())
{
return false;
}
if (empty($mission->getCommissionAmount()) && empty($mission->getCommissionPercentage()))
{
return false;
}
}
// If we are here all went well
return true;
}
private function canList(Access $user, AccessFunction $function)
{
// Always show tab for JCAF SocietyGroup
if ($this->currentGroup->isJcaf())
{
return true;
}
// if ($this->currentGroup->isNotJcaf())
// {
// $sql = "SELECT COUNT(*) as nb
// FROM society_group_invoice
// WHERE society_group_invoice.receiver_society_group_id = ".$this->currentGroup->getId();
// $stmt = $this->em->getConnection()->prepare($sql);
// $stmt->execute();
// $result = $stmt->fetch()['nb'];
// if ($result < 1)
// {
// return false;
// }
// }
// If the current society group is not JCAF
// only show the society_group_invoice tab if at least one invoice is available
// Test receiver
$invoice = $this->em->getRepository(SocietyGroupInvoice::class)->findOneByReceiver($this->currentGroup);
if ($invoice === null)
{
// None available, die hard
return false;
}
// If we are here it means that we have at least one invoice to show
// Test permissions
// Get Acl_Permission
$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();
// All hope is lost
return false;
}
private function canView(SocietyGroupInvoice $invoice, Access $user, AccessFunction $function)
{
// Both emitter and receiver can view society group invoices
if (!($this->currentGroup->equals($this->emitter) || $this->currentGroup->equals($this->receiver)))
{
return false;
}
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
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();
// All hope is lost
return false;
}
private function canViewPdf(SocietyGroupInvoice $invoice, Access $user, AccessFunction $function)
{
// Both emitter and receiver can view society group invoices
if (!($this->currentGroup->equals($this->emitter) || $this->currentGroup->equals($this->receiver)))
{
return false;
}
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF);
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();
// All hope is lost
return false;
}
private function canEdit(SocietyGroupInvoice $invoice, Access $user, AccessFunction $function)
{
// Only the emitter of a SocietyGroupInvoice can edit it
// For now the emitter is always Jcaf ;)
if (!$this->currentGroup->equals($this->emitter))
{
return false;
}
// Plan.io Task #3229 Commissions
// Only JCAF SocietyGroup can edit a SocietyGroupInvoice for now
if ($this->currentGroup->isNotJcaf())
{
return false;
}
$mission = $invoice->getMission();
if ($mission === null)
{
return false;
}
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// No edditing on annulled invoices
if ($invoice->isAnnulled())
{
return false;
}
// No edditing on invoices that have been "annuled" (with credit)
// $credit = $this->em->getRepository('IcodPlatformBundle:Invoice')
// ->findOneByCreditInvoice($invoice);
// if ($credit !== null)
// return false;
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
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();
// All hope is lost
return false;
}
private function canEditReceiverSociety(SocietyGroupInvoice $invoice, Access $user, AccessFunction $function)
{
// Plan.io Task #3229 Commissions
// Only the receiver of a SocietyGroupInvoice can edit its receiver
if (!$this->currentGroup->equals($this->receiver))
{
return false;
}
$mission = $invoice->getMission();
if ($mission === null)
{
return false;
}
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// No edditing on annulled invoices
if ($invoice->isAnnulled())
{
return false;
}
// No edditing on invoices that have been "annuled" (with credit)
// $credit = $this->em->getRepository('IcodPlatformBundle:Invoice')
// ->findOneByCreditInvoice($invoice);
// if ($credit !== null)
// return false;
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
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();
// All hope is lost
return false;
}
}