<?php
//------------------------------------------------------------------------------
// src/Security/SocietyGroupCommandVoter.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\SocietyGroup\SocietyGroupCommand;
use App\Entity\Platform\SocietyGroup\SocietyGroupInvoice;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class SocietyGroupCommandVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const ADD = "add_society_group_command";
const VIEW = "view_society_group_command";
const VIEW_PDF = "view_pdf_society_group_command";
const EDIT = "edit_society_group_command";
const EDIT_RECEIVER_SOCIETY = "edit_society_group_command_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 SocietyGroupCommands, there is no need to even show the tab
const LIST = "list_society_group_commands";
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_command_view";
const ACL_PERM_VIEW_PDF = "society_group_command_view_pdf";
const ACL_PERM_EDIT = "society_group_command_edit";
const ACL_PERM_LIST = "society_group_command_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 SocietyGroupCommand || $subject instanceof SocietyGroupInvoice))
{
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->command = null;
$this->emitter = null;
$this->receiver = null;
if ($subject instanceof SocietyGroupCommand)
{
$this->command = $subject;
$this->emitter = $subject->getEmitter();
$this->receiver = $subject->getReceiver();
if ($this->emitter === null || $this->receiver === null)
{
return false;
}
}
switch ($attribute)
{
// canAdd : subject is SocietyGroupInvoice
case self::ADD:
return $this->canAdd($subject);
case self::LIST:
return $this->canList($user, $function);
// for all those below : subject is SocietyGroupCommand
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(SocietyGroupInvoice $invoice)
{
// Only JCAF SocietyGroup can add a SocietyGroupCommand for now
if ($this->currentGroup->isNotJcaf())
{
return false;
}
// Plan.io Task #3229 Commissions
$mission = $invoice->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
$devis = $invoice->getDevis();
if ($devis !== null)
{
if ($devis->isNotJcaf())
{
return false;
}
if (empty($devis->getShareCommission()))
{
return false;
}
// Deny for annulled and refused
if ($devis->isAnnulled() || $devis->isRefused())
{
return false;
}
}
// Do we already have a SocietyGroupCommand for this SocietyGroupInvoice ?
$command = $this->em->getRepository(SocietyGroupCommand::class)
->findOneByInvoice($invoice);
if ($command !== 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_command
// WHERE society_group_command.emitter_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_command tab if at least one command is available
// Test emitter
$command = $this->em->getRepository(SocietyGroupCommand::class)
->findOneByEmitter($this->currentGroup);
if ($command === null)
{
// None available, die hard
return false;
}
// If we are here it means that we have at least one command 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(SocietyGroupCommand $command, Access $user, AccessFunction $function)
{
// Both emitter and receiver can view society group commands
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(SocietyGroupCommand $command, Access $user, AccessFunction $function)
{
// Both emitter and receiver can view society group commands
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 canEdit(SocietyGroupCommand $command, Access $user, AccessFunction $function)
{
// Only JCAF for now
if ($this->currentGroup->isNotJcaf())
{
return false;
}
$mission = $command->getMission();
if ($mission === null)
{
return false;
}
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
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(SocietyGroupCommand $command, Access $user, AccessFunction $function)
{
// For now receiver can only be JCAF
// So no need to actually edit the receiver ;)
return true;
}
}