Last active
December 25, 2015 04:28
-
-
Save xphere/6917202 to your computer and use it in GitHub Desktop.
Usage of Ofertix\SecurityExtraBundle annotations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
| use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | |
| class OwnershipVoter implements VoterInterface | |
| { | |
| public function supportsAttribute($attribute) | |
| { | |
| return $attribute === 'ROLE_OWNER'; | |
| } | |
| public function supportsClass($class) | |
| { | |
| return true; | |
| } | |
| public function vote(TokenInterface $token, $object, array $attributes) | |
| { | |
| if (!in_array('ROLE_OWNER', $attributes) || false === is_callable([$object, 'getUser'])) { | |
| return self::ACCESS_ABSTAIN; | |
| } | |
| if ($token->getUser() !== $object->getUser()) { | |
| return VoterInterface::ACCESS_DENIED; | |
| } | |
| return VoterInterface::ACCESS_GRANTED; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Ofertix\SecurityExtraBundle\Annotation as Security; | |
| /** | |
| * @Security\Role("ROLE_USER") | |
| */ | |
| class UserAddressController | |
| { | |
| /** | |
| * @Security\Param("ROLE_OWNER", name="address") | |
| */ | |
| public function removeAddress(Address $address) | |
| { | |
| // Only accessible to validated users who own this Address | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment