Skip to content

Commit 9ad9daf

Browse files
weaverryanwouterj
authored andcommitted
Additional little check to show how we're assumign the User object is the User entity
1 parent af85d52 commit 9ad9daf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

cookbook/security/voters.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ edit a particular object. Here's an example implementation::
7777
// src/AppBundle/Security/Authorization/Voter/PostVoter.php
7878
namespace AppBundle\Security\Authorization\Voter;
7979

80+
use AppBundle\Entity\User;
8081
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
8182
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8283
use Symfony\Component\Security\Core\User\UserInterface;
@@ -133,6 +134,13 @@ edit a particular object. Here's an example implementation::
133134
return VoterInterface::ACCESS_DENIED;
134135
}
135136

137+
// double-check that the User object is the expected entity.
138+
// It always will be, unless there is some misconfiguration of the
139+
// security system.
140+
if (!$user instanceof User) {
141+
throw new \LogicException('The user is somehow not our User class!');
142+
}
143+
136144
switch($attribute) {
137145
case self::VIEW:
138146
// the data object could have for example a method isPrivate()

0 commit comments

Comments
 (0)