<?php
//------------------------------------------------------------------------------
// src/Security/NoteVoter.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 Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use App\Entity\Access;
use App\Entity\Client\Client;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Mission\Mission;
use App\Entity\Platform\Note;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\AccessClient\AccessClientTools;
use App\Services\Config\ModuleTools;
class NoteVoter extends Voter
{
const ADD = "add_note";
const ADD_CLIENT_VISIBLE = "add_note_client_visible";
const EDIT = "edit_note";
const HIDE = "hide_note";
const DELETE = "delete_note";
const IS_GRANTED_CONSTANTS = array(
self::ADD,
self::ADD_CLIENT_VISIBLE,
self::EDIT,
self::HIDE,
self::DELETE,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "note_add";
const ACL_PERM_ADD_SOCIETY = "note_add_society";
const ACL_PERM_ADD_MANAGER = "note_add_manager";
const ACL_PERM_EDIT = "note_edit";
const ACL_PERM_EDIT_SOCIETY = "note_edit_society";
const ACL_PERM_EDIT_MANAGER = "note_edit_manager";
const ACL_PERM_HIDE = "note_hide";
const ACL_PERM_HIDE_SOCIETY = "note_hide_society";
const ACL_PERM_HIDE_MANAGER = "note_hide_manager";
const ACL_PERM_DELETE = "note_delete";
const ACL_PERM_DELETE_SOCIETY = "note_delete_society";
const ACL_PERM_DELETE_MANAGER = "note_delete_manager";
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine, AccessClientTools $accessClientTools, ModuleTools $moduleTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$this->em = $doctrine->getManager();
$this->accessClientTools = $accessClientTools;
$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 Note objects or Client objects inside this voter
if ($subject !== null && !($subject instanceof Note || $subject instanceof Client || $subject instanceof Mission))
{
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;
// For this voter, the subject cannot be null
if ($subject === null)
return false;
// Check current group affectation
// This also includes mission sharing
// mission.society.group = currentGroup
// OR
// mission.getSocietyGroupOwner = currentGroup
$societyGroupOwner = null;
if ($subject !== null)
{
$subjectGroup = null;
if ($subject instanceof Note)
{
$subjectGroup = $subject->getSocietyGroup();
// Try to fecth the societyGroupOwner from the mission
if ($subject->getMission() !== null)
{
$societyGroupOwner = $subject->getMission()->getSocietyGroupOwner();
}
}
else
{
if ($subject instanceof Mission)
{
$client = $subject->getReceiver();
if ($client === null)
return false;
$subjectGroup = $client->getSocietyGroup();
// Try to fecth the societyGroupOwner from the mission
$societyGroupOwner = $subject->getSocietyGroupOwner();
}
else
{
$client = $subject;
if ($client === null)
return false;
$subjectGroup = $client->getSocietyGroup();
// Try to fetch the societyGroupOwner from the client
$societyGroupOwner = null;
}
}
if ($subjectGroup === null)
{
return false;
}
if ($subjectGroup === null)
return false;
// Checking ...
if ($societyGroupOwner !== null)
{
if (!$currentGroup->equals($subjectGroup) && !$currentGroup->equals($societyGroupOwner))
{
return false;
}
}
else
{
if (!$currentGroup->equals($subjectGroup))
{
return false;
}
}
}
switch ($attribute)
{
case self::ADD:
{
// $subject should be instanceof Client or Mission
return $this->canAdd($subject, $user, $function);
}
case self::ADD_CLIENT_VISIBLE:
{
// $subject should be instanceof Mission
if ($subject instanceof Mission)
{
return $this->canAddClientVisible($subject, $user, $function);
}
else
{
return false;
}
}
case self::EDIT:
{
// $subject should be instanceof Note
$note = $subject;
return $this->canEdit($note, $user, $function);
}
case self::HIDE:
{
// $subject should be instanceof Note
$note = $subject;
return $this->canHide($note, $user, $function);
}
case self::DELETE:
{
// $subject should be instanceof Note
$note = $subject;
return $this->canDelete($note, $user, $function, $token);
}
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $client is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety(Client $client, Access $access)
{
if ($client === null)
return false;
$individual = $client->getIndividual();
if ($individual === null)
return false;
$clientSociety = $individual->getSociety();
if ($clientSociety === null)
return false;
// Get all the societies of the access
$societies = $access->getSocieties();
foreach ($societies as $society)
{
if ($society->equals($clientSociety))
{
return true;
}
}
return false;
}
// $access is the user trying to load the resource
// $client is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkManager(Client $client, Access $access)
{
if ($client === null)
return false;
$individual = $client->getIndividual();
if ($individual === null)
return false;
$clientManager = $individual->getManager();
if ($clientManager === null)
return false;
if ($clientManager->equals($access))
return true;
return false;
}
private function canAdd($subject, Access $access, AccessFunction $function)
{
if ($subject instanceof Mission)
{
// Deny edit on archivedRefused objects
if ($subject->isArchivedRefused())
{
return false;
}
}
// Three AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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 ($subject instanceof Mission)
$client = $subject->getReceiver();
else
if ($subject instanceof Client)
$client = $subject;
else
return false;
// 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($client, $access);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkManager
return $this->checkManager($client, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canAddClientVisible(Mission $mission, Access $access, AccessFunction $function)
{
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
$receiver = $mission->getReceiver();
if ($receiver === null)
{
// This should not happen
return false;
}
// Deny if mission.receiver has not activated its account
// Plan.io Task #4327 : Add Client Account, and remove JCAF
// Does the Receiver have an AccessClientRecord ?
$accessClientIsActive = $this->accessClientTools->accessClientIsActiveForClient($receiver);
if (!$accessClientIsActive)
{
return false;
}
// Three AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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;
}
}
}
$client = $mission->getReceiver();
// 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($client, $access);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkManager
return $this->checkManager($client, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Note $note, Access $access, AccessFunction $function)
{
// Deny edit on archivedRefused objects
if ($note->isArchivedRefused())
{
return false;
}
if ($note->isReadonly())
{
return false;
}
// Plan.io Task #4071 : Deny parent mission actions on child item
if ($note->getMission() !== null && $note->getMission()->getParent() !== null)
{
if ($note->belongsToChildOf($note->getMission()->getParent()))
{
return false;
}
}
// Deny edit on notes that have been successfully sent to Jcaf
if (!empty($note->getRemoteJcafId()))
{
return false;
}
// Deny edit on notes that have been successfully sent to Rekto
if (!empty($note->getRemoteRektoId()))
{
return false;
}
// Allways allow authors to edit their own notes
if ($note->getAuthor() !== null && $note->getAuthor()->equals($access))
return true;
$client = $note->getClient();
if ($client === null)
{
$mission = $note->getMission();
if ($mission === null)
return false;
$client = $mission->getReceiver();
if ($client === null)
return null;
}
// Three AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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($client, $access);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkManager
return $this->checkManager($client, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canHide(Note $note, Access $access, AccessFunction $function)
{
// Allways allow authors to edit their own notes
if ($note->getAuthor() !== null && $note->getAuthor()->equals($access))
return true;
$client = $note->getClient();
if ($client === null)
{
$mission = $note->getMission();
if ($mission === null)
return false;
$client = $mission->getReceiver();
if ($client === null)
return null;
}
// Three AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HIDE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HIDE_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_HIDE_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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($client, $access);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkManager
return $this->checkManager($client, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDelete(Note $note, Access $access, AccessFunction $function, $token)
{
// Plan.io Task #4383, modified by #4453
// Temporarly allow admins to delete any Note
// if ($this->security->isGranted('rekapp_admin'))
if ($this->accessDecisionManager->decide($token, ['rekapp_admin']))
{
return true;
}
// Deny edit on archivedRefused objects
if ($note->isArchivedRefused())
{
return false;
}
if ($note->isReadonly())
{
return false;
}
// Plan.io Task #4071 : Deny parent mission actions on child item
if ($note->getMission() !== null && $note->getMission()->getParent() !== null)
{
if ($note->belongsToChildOf($note->getMission()->getParent()))
{
return false;
}
}
// Deny delete on notes that have been successfully sent to Jcaf
if (!empty($note->getRemoteJcafId()))
{
return false;
}
// Deny delete on notes that have been successfully sent to Rekto
if (!empty($note->getRemoteRektoId()))
{
return false;
}
// Allways allow authors to edit their own notes
if ($note->getAuthor() !== null && $note->getAuthor()->equals($access))
return true;
$client = $note->getClient();
if ($client === null)
{
$mission = $note->getMission();
if ($mission === null)
return false;
$client = $mission->getReceiver();
if ($client === null)
return null;
}
// Three AclPermission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_DELETE_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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($client, $access);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkManager
return $this->checkManager($client, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
}