UserAclVoter.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Sonata\UserBundle\Security\Authorization\Voter;
  3. use FOS\UserBundle\Model\UserInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Acl\Voter\AclVoter;
  6. class UserAclVoter extends AclVoter
  7. {
  8. /**
  9. * {@InheritDoc}
  10. */
  11. public function supportsClass($class)
  12. {
  13. // support the Object-Scope ACL
  14. return is_subclass_of($class, 'FOS\UserBundle\Model\UserInterface');
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function supportsAttribute($attribute)
  20. {
  21. return $attribute === 'EDIT' || $attribute === 'DELETE';
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function vote(TokenInterface $token, $object, array $attributes)
  27. {
  28. if (!$this->supportsClass(get_class($object))) {
  29. return self::ACCESS_ABSTAIN;
  30. }
  31. foreach ($attributes as $attribute) {
  32. if ($this->supportsAttribute($attribute) && $object instanceof UserInterface) {
  33. if ($object->isSuperAdmin() && !$token->getUser()->isSuperAdmin()) {
  34. // deny a non super admin user to edit or delete a super admin user
  35. return self::ACCESS_DENIED;
  36. }
  37. }
  38. }
  39. // leave the permission voting to the AclVoter that is using the default permission map
  40. return self::ACCESS_ABSTAIN;
  41. }
  42. }