<?php
namespace App\Security\Authorization\Voter;
use App\Entity\AbstractInvoice;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class InvoiceVoter extends Voter
{
const CREATE = 'create';
const READ = 'read';
const UPDATE = 'update';
const DELETE = 'delete';
/**
* {@inheritDoc}
*/
protected function supports($attribute, $subject)
{
$attributes = [
self::CREATE,
self::READ,
self::UPDATE,
self::DELETE,
];
if (!in_array($attribute, $attributes)) {
return false;
}
if (!$subject instanceof AbstractInvoice) {
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if ($user->isSuperAdmin()) {
return true;
}
switch ($attribute) {
case self::CREATE:
case self::UPDATE:
case self::DELETE:
if ($user->isSuperAdmin()) {
return true;
}
break;
case self::READ:
return true;
break;
}
return false;
}
}