Skip to content

Commit f8dd30f

Browse files
committed
Additional little check to show how we're assumign the User object is the User entity
1 parent 7e7020d commit f8dd30f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

cookbook/security/voters_data_permission.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ edit a particular object. Here's an example implementation::
6262
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
6363
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6464
use Symfony\Component\Security\Core\User\UserInterface;
65+
use AppBundle\Entity\User;
6566

6667
class PostVoter implements VoterInterface
6768
{
@@ -118,6 +119,13 @@ edit a particular object. Here's an example implementation::
118119
return VoterInterface::ACCESS_DENIED;
119120
}
120121

122+
// double-check that our User object is the entity we expect.
123+
// It always will be, unless we've configured our security system
124+
// in some other way.
125+
if (!$user instanceof User) {
126+
throw new \LogicException('The user is somehow not our User class!');
127+
}
128+
121129
switch($attribute) {
122130
case self::VIEW:
123131
// the data object could have for example a method isPrivate()

0 commit comments

Comments
 (0)