<?php
//------------------------------------------------------------------------------
// src/Security/WikiVoter.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\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Entity\SocietyGroup;
use App\Entity\Wiki\Page;
use App\Services\Config\ModuleTools;
class WikiVoter extends Voter
{
const VIEW_PAGE = "view_wiki_page";
const IS_GRANTED_CONSTANTS = array(
self::VIEW_PAGE,
);
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 Template objects inside this voter
if ($subject !== null && !$subject instanceof Page)
{
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 Page object, thanks to supports
/** @var Page $page */
$page = $subject;
switch ($attribute)
{
case self::VIEW_PAGE:
return $this->canViewPage($page, $user, $function, $currentGroup);
}
throw new \LogicException('This code should not be reached!');
}
private function canViewPage(Page $page, Access $access, AccessFunction $function, SocietyGroup $currentGroup)
{
// Is this page available for all ?
if ($page->isAvailableToAll())
{
return true;
}
// If we are here it means the page is not available to all society groups
// Check to see if it is available to this particular society group
if ($page->isAvailableTo($currentGroup))
{
return true;
}
// All hope is lost
return false;
}
}