<?php
//------------------------------------------------------------------------------
// src/Security/ClientVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Doctrine\Persistence\ManagerRegistry;
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\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class ClientVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "client_is_active";
const ADD = "add_client";
const LISTING = "list_clients";
const VIEW = "view_client";
const EDIT = "edit_client";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::LISTING,
self::VIEW,
self::EDIT,
);
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine, ModuleTools $moduleTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
}
// 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 Client objects inside this voter
if ($subject !== null && !$subject instanceof Client)
{
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_CLIENT) && $this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_CLIENT_LIGHT))
{
return false;
}
// you know $subject is a Client object, thanks to supports
/** @var Client $client */
$client = $subject;
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::ADD:
return $this->canAdd($client, $token);
case self::LISTING:
return $this->canList();
case self::VIEW:
return $this->canView($client, $token);
case self::EDIT:
return $this->canEdit($client, $token);
}
throw new \LogicException('This code should not be reached!');
}
private function canList()
{
return true;
}
private function canAdd(Client $client = null, $token)
{
if ($client->getIndividual() !== null)
{
return $this->accessDecisionManager->decide($token, ['add_individual']);
}
if ($client->getStore() !== null)
{
return $this->accessDecisionManager->decide($token, ['add_store']);
}
return false;
}
private function canView(Client $client, $token)
{
if ($client->getIndividual() !== null)
{
return $this->accessDecisionManager->decide($token, ['view_individual'], $client->getIndividual());
}
if ($client->getStore() !== null)
{
return $this->accessDecisionManager->decide($token, ['view_store'], $client->getStore());
}
return false;
}
private function canEdit(Client $client, $token)
{
if ($client->getIndividual() !== null)
{
return $this->accessDecisionManager->decide($token, ['edit_individual'], $client->getIndividual());
}
if ($client->getStore() !== null)
{
return $this->accessDecisionManager->decide($token, ['edit_store'], $client->getStore());
}
return false;
}
}