for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* (c) Lukasz D. Tulikowski <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Security\Voter\User;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class UpdateUserVoter extends Voter
{
public const CAN_UPDATE_USER = 'CAN_UPDATE_USER';
/**
* {@inheritdoc}
protected function supports($attribute, $subject)
// you only want to vote if the attribute and subject are what you expect
return self::CAN_UPDATE_USER === $attribute && ($subject instanceof User || null === $subject);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
// our previous business logic indicates that mods and admins can do it regardless
foreach ($token->getRoles() as $role) {
if (\in_array($role->getRole(), ['ROLE_MODERATOR', 'ROLE_ADMIN'])) {
return true;
// allow controller handle not found subject
if (null === $subject) {
$user = $token->getUser();
// allow user to update account
if ($user instanceof User) {
return $subject->getId() === $user->getId();
return false;