Massaction.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid;
  7. use Magento\Backend\Block\Template\Context;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\AuthorizationInterface;
  10. use Magento\Framework\DataObject;
  11. use Magento\Framework\Json\EncoderInterface;
  12. /**
  13. * Grid widget massaction default block
  14. *
  15. * @api
  16. * @deprecated 100.2.0 in favour of UI component implementation
  17. * @since 100.0.2
  18. */
  19. class Massaction extends \Magento\Backend\Block\Widget\Grid\Massaction\AbstractMassaction
  20. {
  21. /**
  22. * @var AuthorizationInterface
  23. */
  24. private $authorization;
  25. /**
  26. * Map bind item id to a particular acl type
  27. * itemId => acl
  28. *
  29. * @var array
  30. */
  31. private $restrictions = [
  32. 'enable' => 'Magento_Backend::toggling_cache_type',
  33. 'disable' => 'Magento_Backend::toggling_cache_type',
  34. 'refresh' => 'Magento_Backend::refresh_cache_type',
  35. ];
  36. /**
  37. * Massaction constructor.
  38. *
  39. * @param Context $context
  40. * @param EncoderInterface $jsonEncoder
  41. * @param array $data
  42. * @param AuthorizationInterface $authorization
  43. */
  44. public function __construct(
  45. Context $context,
  46. EncoderInterface $jsonEncoder,
  47. array $data = [],
  48. AuthorizationInterface $authorization = null
  49. ) {
  50. $this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
  51. parent::__construct($context, $jsonEncoder, $data);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. *
  56. * @param string $itemId
  57. * @param array|DataObject $item
  58. *
  59. * @return $this
  60. * @since 100.2.3
  61. */
  62. public function addItem($itemId, $item)
  63. {
  64. if (!$this->isRestricted($itemId)) {
  65. parent::addItem($itemId, $item);
  66. }
  67. return $this;
  68. }
  69. /**
  70. * Check if access to action restricted
  71. *
  72. * @param string $itemId
  73. *
  74. * @return bool
  75. */
  76. private function isRestricted(string $itemId): bool
  77. {
  78. if (!key_exists($itemId, $this->restrictions)) {
  79. return false;
  80. }
  81. return !$this->authorization->isAllowed($this->restrictions[$itemId]);
  82. }
  83. }