AclSecurityHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Security\Handler;
  11. use Symfony\Component\Security\Core\SecurityContextInterface;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. class AclSecurityHandler implements SecurityHandlerInterface
  15. {
  16. public function __construct(SecurityContextInterface $securityContext)
  17. {
  18. $this->securityContext = $securityContext;
  19. }
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function isGranted($attributes, $object = null)
  24. {
  25. try {
  26. return $this->securityContext->isGranted($attributes, $object);
  27. } catch (AuthenticationCredentialsNotFoundException $e) {
  28. return false;
  29. } catch (\Exception $e) {
  30. throw $e;
  31. }
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function buildSecurityInformation(AdminInterface $admin)
  37. {
  38. $baseRole = 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  39. $results = array();
  40. foreach ($admin->getSecurityInformation() as $name => $permissions) {
  41. $results[sprintf($baseRole, $name)] = $permissions;
  42. }
  43. return $results;
  44. }
  45. }