<?php
//------------------------------------------------------------------------------
// src/Security/EquipmentVoter.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\Equipment\Equipment;
use App\Entity\Equipment\Note;
use App\Entity\HR\AccessFunction;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class EquipmentVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "equipment_is_active";
const ADD = "add_equipment";
const LISTING = "list_equipments";
const LISTING_SOCIETY = "list_equipments_society";
const LISTING_ANY = "list_equipments_any";
const VIEW = "view_equipment";
const EDIT = "edit_equipment";
const DELETE = "delete_equipment";
const ADD_NOTE = "add_equipment_note";
const EDIT_NOTE = "edit_equipment_note";
const DELETE_NOTE = "delete_equipment_note";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::DELETE,
self::ADD_NOTE,
self::EDIT_NOTE,
self::DELETE_NOTE,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "equipment_add";
const ACL_PERM_LISTING = "equipment_list";
const ACL_PERM_LISTING_SOCIETY = "equipment_list_society";
const ACL_PERM_VIEW = "equipment_view";
const ACL_PERM_VIEW_SOCIETY = "equipment_view_society";
const ACL_PERM_EDIT = "equipment_edit";
const ACL_PERM_EDIT_SOCIETY = "equipment_edit_society";
const ACL_PERM_DELETE = "equipment_delete";
const ACL_PERM_DELETE_SOCIETY = "equipment_delete_society";
const ACL_PERM_ADD_NOTE = "equipment_add_note";
const ACL_PERM_ADD_NOTE_SOCIETY = "equipment_add_note_society";
const ACL_PERM_EDIT_NOTE = "equipment_edit_note";
const ACL_PERM_EDIT_NOTE_SOCIETY = "equipment_edit_note_society";
const ACL_PERM_DELETE_NOTE = "equipment_delete_note";
const ACL_PERM_DELETE_NOTE_SOCIETY = "equipment_delete_note_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);
}
// 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 Equipment objects inside this voter
if ($subject !== null && !($subject instanceof Equipment or $subject instanceof Note))
{
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 ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_EQUIPMENT))
{
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;
}
// you know $subject is a Equipment / Note object, thanks to supports
$note = null;
$equipment = null;
if ($subject instanceof Equipment)
{
$equipment = $subject;
}
else
{
if ($subject instanceof Note)
{
$note = $subject;
}
}
switch ($attribute)
{
// Check is done before, in the voteOnAttribute
case self::IS_ACTIVE:
return true;
case self::ADD:
return $this->canAdd($user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
case self::VIEW:
return $this->canView($equipment, $user, $function);
case self::EDIT:
return $this->canEdit($equipment, $user, $function);
case self::DELETE:
return $this->canDelete($equipment, $user, $function);
case self::ADD_NOTE:
return $this->canAddNote($equipment, $user, $function);
case self::EDIT_NOTE:
return $this->canEditNote($note, $user, $function);
case self::DELETE_NOTE:
return $this->canDeleteNote($note, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $object is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety($object, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Equipment
$objectSociety = $object->getSociety();
if ($objectSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $objectSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canAdd(Access $user, 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;
// Since only one acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
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;
}
private function canView(Equipment $equipment, 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($equipment, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Equipment $equipment, Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
$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($equipment, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDelete(Equipment $equipment, Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE_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($equipment, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canAddNote(Equipment $equipment, Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_NOTE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_NOTE_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($equipment, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditNote(Note $note, Access $user, AccessFunction $function)
{
// Allow editing own notes
if ($user->equals($note->getAuthor()))
{
return true;
}
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_NOTE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_NOTE_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($note, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDeleteNote(Note $note, Access $user, AccessFunction $function)
{
// Allow deleting own notes
if ($user->equals($note->getAuthor()))
{
return true;
}
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE_NOTE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE_NOTE_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($note, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
}