Skip to content

Instantly share code, notes, and snippets.

@xphere
Last active December 25, 2015 04:28
Show Gist options
  • Select an option

  • Save xphere/6917202 to your computer and use it in GitHub Desktop.

Select an option

Save xphere/6917202 to your computer and use it in GitHub Desktop.
Usage of Ofertix\SecurityExtraBundle annotations
<?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;
}
}
<?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