Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Security/Voter/UserVoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Security\Voter;

use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

use function in_array;

/**
* Grants USER_EDIT only on the account of the authenticated user.
*
* Implements VoterInterface instead of extending Voter, whose abstract
* voteOnAttribute() signature is not the same across the Symfony versions
* covered by the branches of this repository.
*/
final class UserVoter implements VoterInterface
{
public const EDIT = 'USER_EDIT';

/**
* @param mixed[] $attributes
*/
public function vote(TokenInterface $token, mixed $subject, array $attributes, mixed ...$args): int
{
if (!in_array(self::EDIT, $attributes, true) || !$subject instanceof User) {
return self::ACCESS_ABSTAIN;
}

$user = $token->getUser();

return $user instanceof User && $user->getUserIdentifier() === $subject->getUserIdentifier()
? self::ACCESS_GRANTED
: self::ACCESS_DENIED;
}
}
5 changes: 5 additions & 0 deletions tests/Functional/DoctrineCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function seeNumRecords(FunctionalTester $I)
$I->seeNumRecords(1, User::class);
}

public function seeDoctrineSchemaIsValid(FunctionalTester $I): void
{
$I->seeDoctrineSchemaIsValid();
}

public function queryCountAssertions(FunctionalTester $I): void
{
$I->amOnPage('/run-queries');
Expand Down
25 changes: 25 additions & 0 deletions tests/Functional/SecurityCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Tests\Functional;

use App\Entity\User;
use App\Security\Voter\UserVoter;
use App\Tests\Support\FunctionalTester;

final class SecurityCest
Expand Down Expand Up @@ -70,6 +71,30 @@ public function seeUserHasRoles(FunctionalTester $I)
$I->seeUserHasRoles(['ROLE_USER', 'ROLE_CUSTOMER']);
}

public function seeUserIsGranted(FunctionalTester $I): void
{
$user = $I->grabEntityFromRepository(User::class, [
'email' => 'john_doe@gmail.com',
]);
$I->amLoggedInAs($user);
$I->amOnPage('/');

$I->seeUserIsGranted('ROLE_CUSTOMER');
$I->seeUserIsGranted(UserVoter::EDIT, $user);
}

public function dontSeeUserIsGranted(FunctionalTester $I): void
{
$user = $I->grabEntityFromRepository(User::class, [
'email' => 'john_doe@gmail.com',
]);
$I->amLoggedInAs($user);
$I->amOnPage('/');

$I->dontSeeUserIsGranted('ROLE_ADMIN');
$I->dontSeeUserIsGranted(UserVoter::EDIT, User::create('jane_doe@gmail.com', '123456'));
}

public function seeUserPasswordDoesNotNeedRehash(FunctionalTester $I)
{
$user = $I->grabEntityFromRepository(User::class, [
Expand Down
Loading