AdminVoter.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if ($admin instanceof AdminInterface
  43. && $admin->hasRoute('list') && $admin->hasAccess('list')
  44. && $this->request
  45. ) {
  46. $requestCode = $this->request->get('_sonata_admin');
  47. if ($admin->getCode() === $requestCode) {
  48. return true;
  49. }
  50. foreach ($admin->getChildren() as $child) {
  51. if ($child->getBaseCodeRoute() === $requestCode) {
  52. return true;
  53. }
  54. }
  55. }
  56. $route = $item->getExtra('route');
  57. if ($route && $this->request && $route == $this->request->get('_route')) {
  58. return true;
  59. }
  60. return null;
  61. }
  62. }