45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
use App\Entity\User;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
class UserVoter extends Voter
|
|
{
|
|
public const EDIT = 'CAN_EDIT_USER';
|
|
|
|
public function __construct(private readonly Security $security)
|
|
{
|
|
}
|
|
|
|
protected function supports(string $attribute, mixed $subject): bool
|
|
{
|
|
return self::EDIT === $attribute && $subject instanceof User;
|
|
}
|
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
|
{
|
|
$currentUser = $token->getUser();
|
|
$targetUser = $subject;
|
|
|
|
if (!$currentUser instanceof User || !$targetUser instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
// editing your own roles or super admin flag is not allowed
|
|
if ($currentUser === $targetUser) {
|
|
return false;
|
|
}
|
|
|
|
// if the current user is impersonating, do not grant access
|
|
if ($this->security->isGranted('IS_IMPERSONATOR')) {
|
|
return false;
|
|
}
|
|
|
|
return $currentUser->isSuperAdmin();
|
|
}
|
|
}
|