<?php
//------------------------------------------------------------------------------
// src/Security/SocietyVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\Common\Collections\ArrayCollection;
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\Society;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class SocietyVoter extends Voter
{
//--------------------------------------------------------------------------
// Plan.io Task #3634
const IS_ACTIVE = "society_is_active";
const DISPLAY_SINGLE_SOCIETY = "display_single_society";
const DISPLAY_MULTIPLE_SOCIETY = "display_multiple_societies";
//--------------------------------------------------------------------------
const ADD = "add_society";
const LISTING = "list_societies";
const LISTING_SOCIETY = "list_societies_society";
const LISTING_ANY = "list_societies_any";
const VIEW = "view_society";
const EDIT = "edit_society";
const DELETE = "delete_society";
const IS_GRANTED_CONSTANTS = array(
self::ADD,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::DELETE,
// Plan.io Task #3634
self::IS_ACTIVE,
self::DISPLAY_SINGLE_SOCIETY,
self::DISPLAY_MULTIPLE_SOCIETY,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "society_add";
const ACL_PERM_LISTING = "society_list";
const ACL_PERM_LISTING_SOCIETY = "society_list_society";
const ACL_PERM_VIEW = "society_view";
const ACL_PERM_VIEW_SOCIETY = "society_view_society";
const ACL_PERM_EDIT = "society_edit";
const ACL_PERM_EDIT_SOCIETY = "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): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// only vote on Society objects inside this voter
if ($subject !== null && !$subject instanceof Society)
{
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;
// you know $subject is a Society object, thanks to supports
/** @var Society $society */
$society = $subject;
// Check current group affectation
if ($subject !== null)
{
$subjectGroup = $subject->getGroup();
if ($subjectGroup === null)
return false;
if (!$currentGroup->equals($subjectGroup))
return false;
}
switch ($attribute)
{
//------------------------------------------------------------------
// Plan.io Task #3634
case self::IS_ACTIVE:
{
return $this->moduleTools->isActiveMultipleSocieties($currentGroup);
}
case self::DISPLAY_SINGLE_SOCIETY:
{
return $this->moduleTools->isActiveSingleSociety($currentGroup);
}
case self::DISPLAY_MULTIPLE_SOCIETY:
{
return $this->moduleTools->isActiveMultipleSocieties($currentGroup);
}
//------------------------------------------------------------------
case self::ADD:
return $this->canAdd($society, $user, $function);
case self::LISTING:
return $this->canList($society, $user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($society, $user, $function);
case self::LISTING_ANY:
return $this->canListAny($society, $user, $function);
case self::VIEW:
return $this->canView($society, $user, $function);
case self::EDIT:
return $this->canEdit($society, $user, $function);
case self::DELETE:
return $this->canDelete($society, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// The access has a list of societies
// Thus, the access has one or more society groups (ASGS)
// The given society has a group SG
// We need to knoe if the group SG is one of ASGS
private function checkSociety(Society $society, Access $access)
{
// Get all the societies of the access
$accessSocieties = $access->getSocieties();
// Get all the groups of the access
$accessGroups = new \Doctrine\Common\Collections\ArrayCollection();
foreach ($accessSocieties as $as)
{
if ($accessGroups->contains($as->getGroup()) == false)
{
$accessGroups[] = $as->getGroup();
}
}
// Get the society group of the Society
$societyGroup = $society->getGroup();
if ($societyGroup === null)
return false;
foreach ($accessGroups as $ag)
{
if ($ag->getId() == $societyGroup->getId())
{
return true;
}
}
return false;
}
private function canAdd(Society $society = null, Access $access, 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;
return $acl->getValue();
}
private function canList(Society $society = null, Access $access, AccessFunction $function)
{
// Restrictions are also applied in the Controller
// But this helps speeding page loading if the access is not even allowed to load the page
// (ie. if it has no list privileges whatsoever)
// 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 list type can exist for the Societys,
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canListSociety(Society $society = null, Access $access, 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(Society $society = null, Access $access, AccessFunction $function)
{
// Several 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(Society $society, Access $access, AccessFunction $function)
{
// Several Acl_Permission exist
$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 (view all)
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 (restricred view)
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($society, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Society $society, Access $access, AccessFunction $function)
{
// Several Acl_Permission 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 (view all)
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 (restricred view)
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($society, $access);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDelete(Society $society, Access $access, AccessFunction $function)
{
// Deny for all (admin is accepted in the calling function)
return false;
}
}