src/Voter/User/UserAdminVoter.php line 12
<?php
namespace App\Voter\User;
use App\Entity\User\UserAdmin;
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 UserAdminVoter extends Voter
{
const INDEX = 'user_user_admin_index';
const FICHE = 'user_user_admin_fiche';
const ADD = 'user_user_admin_add';
const EDIT = 'user_user_admin_edit';
const DELETE = 'user_user_admin_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 UserAdmin &&
(($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 UserAdmin $theUserAdmin */
$theUserAdmin = $subject;
switch ($attribute) {
case self::INDEX:
return $this->accessIndex();
case self::FICHE:
return $this->accessFiche($theUserAdmin);
case self::ADD:
return $this->accessAdd();
case self::EDIT:
return $this->accessEdit($theUserAdmin);
case self::DELETE:
return $this->accessDelete($theUserAdmin);
default:
return false;
}
}
protected function accessUser()
{
return $this->theSecurity->getUser()->getTheStatut()->getId() == UserStatutRepository::DATA['actif'];
}
protected function accessSuperAdmin()
{
return $this->theSecurity->isGranted('ROLE_SUPER_ADMIN');
}
protected function accessAdmin(?UserAdmin $theUserAdmin = null)
{
return
$this->accessUser()
&& $this->theSecurity->isGranted('ROLE_ADMIN')
&& (
$theUserAdmin == null
|| !$theUserAdmin->hasRole('ROLE_SUPER_ADMIN')
);
}
protected function accessWrite(?UserAdmin $theUserAdmin = null)
{
return
$this->accessRead($theUserAdmin);
}
protected function accessRead(?UserAdmin $theUserAdmin = null)
{
return
$this->accessSuperAdmin()
|| $this->accessAdmin($theUserAdmin);
}
protected function accessIndex() : bool
{
return $this->accessRead();
}
protected function accessFiche(UserAdmin $theUserAdmin) : bool
{
return $this->accessRead($theUserAdmin);
}
protected function accessAdd() : bool
{
return $this->accessWrite();
}
protected function accessEdit(UserAdmin $theUserAdmin) : bool
{
return $this->accessWrite($theUserAdmin);
}
protected function accessDelete(UserAdmin $theUserAdmin) : bool
{
return $this->accessWrite($theUserAdmin);
}
}