<?php
//------------------------------------------------------------------------------
// src/Security/ExternalMessageVoter.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\ExternalMessage\ExternalMessage;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class ExternalMessageVoter extends Voter
{
// For now the only external messages are SMS
// So in this voter we will check if the SMS module is active
//--------------------------------------------------------------------------------
// is_granted constants
const LISTING = "list_external_messages";
const LISTING_SOCIETY = "list_external_messages_society";
const LISTING_ANY = "list_external_messages_any";
const VIEW = "view_external_message";
const EDIT = "edit_external_message";
const EDIT_SOCIETY = "edit_external_message_society";
const IS_GRANTED_CONSTANTS = array(
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::EDIT_SOCIETY,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_LIST = 'external_message_list';
const ACL_PERM_LIST_SOCIETY = 'external_message_list_society';
const ACL_PERM_VIEW = 'external_message_view';
const ACL_PERM_VIEW_SOCIETY = 'external_message_view_society';
const ACL_PERM_EDIT = 'external_message_edit';
const ACL_PERM_EDIT_SOCIETY = 'external_message_edit_society';
const ACL_PERM_SOCIETY_EDIT = 'external_message_society_edit';
const ACL_PERM_SOCIETY_EDIT_SOCIETY = 'external_message_society_edit_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 = 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 ExternalMessage objects inside this voter
if ($subject !== null && !$subject instanceof ExternalMessage)
{
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_SMS))
{
return false;
}
// you know $subject is a ExternalMessage object, thanks to supports
/** @var ExternalMessage $externalMessage */
$externalMessage = $subject;
// 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;
}
switch ($attribute)
{
case self::VIEW:
return $this->canView($externalMessage, $user, $function);
case self::EDIT:
return $this->canEdit($externalMessage, $user, $function);
case self::EDIT_SOCIETY:
return $this->canEditSociety($externalMessage, $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);
}
throw new \LogicException('This code should not be reached!');
}
// Check if the Society of the ExternalMessage
// belongs to the societies of the $access
private function checkSociety(ExternalMessage $externalMessage, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the ExternalMessage
$externalMessageSociety = $externalMessage->getSociety();
if ($externalMessageSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $externalMessageSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canView(ExternalMessage $externalMessage, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($externalMessage, $user))
{
return true;
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(ExternalMessage $externalMessage, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($externalMessage, $user))
{
return true;
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditSociety(ExternalMessage $externalMessage, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_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())
{
// Check Society : also check author
// Les demands appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($externalMessage, $user))
{
return true;
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canList(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$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();
}
private function canListSociety(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_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
return $acl->getValue();
}
private function canListAny(Access $user, AccessFunction $function)
{
// Three Acl_Perdemand may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_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;
}
}