<?php
namespace App\Security\Voter;
use App\Entity\Invoice;
use App\Entity\User;
use App\Repository\InvoiceRepository;
use App\Utils\Commons;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class InvoiceVoter extends Voter
{
const INVOICE_VIEW = 'INVOICE_VIEW';
const INVOICE_SHOW = 'INVOICE_SHOW';
/**
* @var Security
*/
private $security;
/**
* @var Commons
*/
private $commons;
/**
* @var array
*/
private $permissions;
/**
* @var InvoiceRepository
*/
private $invoiceRepository;
/**
* SupportVoter constructor.
* @param Security $security
* @param Commons $commons
* @param InvoiceRepository $invoiceRepository
*/
public function __construct(Security $security, Commons $commons, InvoiceRepository $invoiceRepository)
{
$this->security = $security;
$this->commons = $commons;
$this->invoiceRepository = $invoiceRepository;
$this->permissions = [];
}
/**
* @param string $attribute
* @param mixed $subject
* @return bool
*/
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute,
[self::INVOICE_VIEW, self::INVOICE_SHOW]
)) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
$this->permissions = $this->commons->getUserPermissions($user);
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::INVOICE_VIEW:
return $this->canView();
case self::INVOICE_SHOW:
return ($this->canShow() && $this->isOwner($user, $subject));
}
throw new LogicException('Invalid attribute: ' . $attribute);
}
/**
* @param User|null $user
* @param $subject
* @return bool
*/
private function isOwner(User $user, $subject): bool
{
if (!is_object($subject)) {
$subject = $this->invoiceRepository->findOneBy(["id" => intval($subject)]);
}
if (!$subject instanceof Invoice) {
return false;
}
return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
}
/**
* Return True if have View Permission else return false
* @return bool
*/
private function canView(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('orders', $this->permissions)) {
if (in_array('View', $this->permissions['orders'])) {
return true;
}
}
return false;
}
/**
* Return True if have Show Permission else return false
* @return bool
*/
private function canShow(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('orders', $this->permissions)) {
if (in_array('Show', $this->permissions['orders'])) {
return true;
}
}
return false;
}
}