src/Voter/User/UserCommercantVoter.php line 12
<?php
namespace App\Voter\User;
use App\Entity\User\UserCommercant;
use App\Repository\User\UserStatutRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class UserCommercantVoter extends Voter
{
const INDEX = 'user_user_commercant_index';
const FICHE = 'user_user_commercant_fiche';
const ADD = 'user_user_commercant_add';
const EDIT = 'user_user_commercant_edit';
const DELETE = 'user_user_commercant_delete';
const ATTRIBUTES = [
self::INDEX,
self::FICHE,
self::ADD,
self::EDIT,
self::DELETE,
];
/**
* @var Security
*/
private $theSecurity;
public function __construct(Security $theSecurity)
{
$this->theSecurity = $theSecurity;
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, self::ATTRIBUTES)) {
return false;
}
if (
!$subject instanceof UserCommercant &&
(($attribute != self::INDEX || $attribute != self::ADD) && $subject != null)
) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$theUserCurrent = $token->getUser();
if (!$theUserCurrent instanceof UserInterface) {
return false;
}
/** @var UserCommercant $theUserCommercant */
$theUserCommercant = $subject;
switch ($attribute) {
case self::INDEX:
return $this->accessIndex();
case self::FICHE:
return $this->accessFiche($theUserCommercant);
case self::ADD:
return $this->accessAdd();
case self::EDIT:
return $this->accessEdit($theUserCommercant);
case self::DELETE:
return $this->accessDelete($theUserCommercant);
default:
return false;
}
}
protected function accessUser()
{
return $this->theSecurity->getUser()->getTheStatut()->getId() == UserStatutRepository::DATA['actif'];
}
protected function accessAdmin()
{
return
$this->theSecurity->isGranted('ROLE_ADMIN');
}
protected function accessCommercant(?UserCommercant $theUserCommercant = null)
{
return
$this->accessUser()
&& $this->theSecurity->isGranted('ROLE_COMMERCANT')
&& (
$theUserCommercant != null
&& $theUserCommercant->getId() == $this->theSecurity->getUser()->getId()
);
}
protected function accessWrite(?UserCommercant $theUserCommercant = null)
{
return
$this->accessRead($theUserCommercant);
}
protected function accessRead(?UserCommercant $theUserCommercant = null)
{
return
$this->accessAdmin()
|| $this->accessCommercant($theUserCommercant);
}
protected function accessIndex() : bool
{
return $this->accessRead();
}
protected function accessFiche(UserCommercant $theUserCommercant) : bool
{
return $this->accessRead($theUserCommercant);
}
protected function accessAdd() : bool
{
return $this->accessWrite();
}
protected function accessEdit(UserCommercant $theUserCommercant) : bool
{
return $this->accessWrite($theUserCommercant);
}
protected function accessDelete(UserCommercant $theUserCommercant) : bool
{
return $this->accessWrite($theUserCommercant);
}
}