<?php
//------------------------------------------------------------------------------
// src/Security/CommonVoter.php
// OK @ Plan.io Task #3710
//------------------------------------------------------------------------------
namespace App\Security\ClientPlatform;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\AccessClient\AccessClient;
use App\Entity\Common\Attachment;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Invoice\Invoice;
use App\Entity\ProjectManager\ProjectNotebook;
use App\Entity\Webapp\Document;
use App\Services\AccessClient\AccessClientTools;
use App\Services\LogTools;
class CommonVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const VIEW_ATTACHMENT = "client_platform_view_attachment";
const VIEW_DEVIS_PDF = "client_platform_view_devis_pdf";
const VIEW_DOCUMENT_PDF = "client_platform_view_document_pdf";
const VIEW_INVOICE_PDF = "client_platform_view_invoice_pdf";
// Plan.io Task #4624 #4654
const VIEW_PROJECT_NOTEBOOK_PDF = "client_platform_view_project_notebook_pdf";
const IS_GRANTED_CONSTANTS = array(
self::VIEW_ATTACHMENT,
self::VIEW_DEVIS_PDF,
self::VIEW_DOCUMENT_PDF,
self::VIEW_INVOICE_PDF,
self::VIEW_PROJECT_NOTEBOOK_PDF,
);
//--------------------------------------------------------------------------------
// acl constants
//--------------------------------------------------------------------------------
public function __construct(Security $security, ManagerRegistry $doctrine, AccessClientTools $accessClientTools, LogTools $logTools)
{
$this->security = $security;
$this->em = $doctrine->getManager();
$this->accessClientTools = $accessClientTools;
$this->logTools = $logTools;
}
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 Attachment|Devis|Document|Invoice|ProjectNotebook objects inside this voter
if ($subject !== null)
{
if (!($subject instanceof Attachment) &&
!($subject instanceof Devis) &&
!($subject instanceof Document) &&
!($subject instanceof Invoice) &&
!($subject instanceof ProjectNotebook))
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
// Only AccessClient with ROLE_CLIENT
if (!$this->security->isGranted('ROLE_CLIENT'))
return false;
$user = $token->getUser();
if (!$user instanceof AccessClient)
{
// the user must be logged in; if not, deny access
return false;
}
// Check AccessClient affectation
if ($subject !== null)
{
$receiver = $subject->getReceiver();
if ($receiver === null)
{
$mission = $subject->getMission();
if ($mission === null)
{
return false;
}
$receiver = $mission->getReceiver();
if ($receiver === null)
{
return false;
}
}
if (!$this->accessClientTools->areLinked($receiver, $user))
{
return false;
}
}
switch ($attribute)
{
case self::VIEW_ATTACHMENT:
return true;
case self::VIEW_DOCUMENT_PDF:
return true;
case self::VIEW_INVOICE_PDF:
return true;
case self::VIEW_DEVIS_PDF:
return true;
case self::VIEW_PROJECT_NOTEBOOK_PDF:
return true;
}
throw new \LogicException('This code should not be reached!');
}
}