AdminVoter.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  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\Menu\Matcher\Voter;
  11. use Knp\Menu\ItemInterface;
  12. use Knp\Menu\Matcher\Voter\VoterInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. /**
  16. * Admin menu voter based on extra `admin`.
  17. *
  18. * @author Samusev Andrey <andrey.simfi@ya.ru>
  19. */
  20. class AdminVoter implements VoterInterface
  21. {
  22. /**
  23. * @var Request
  24. */
  25. private $request = null;
  26. /**
  27. * @param Request $request
  28. *
  29. * @return $this
  30. */
  31. public function setRequest($request)
  32. {
  33. $this->request = $request;
  34. return $this;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function matchItem(ItemInterface $item)
  40. {
  41. $admin = $item->getExtra('admin');
  42. $match = null;
  43. if ($admin instanceof AdminInterface
  44. && $admin->hasRoute('list') && $admin->isGranted('LIST')
  45. && $this->request && $this->request->get('_sonata_admin') == $admin->getCode()
  46. ) {
  47. $match = true;
  48. }
  49. $route = $item->getExtra('route');
  50. if ($route && $this->request && $route == $this->request->get('_route')) {
  51. $match = true;
  52. }
  53. return $match;
  54. }
  55. }