From ad4138ced97c672d5a2ed7a761ab214cb7849b6d Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Tue, 3 Nov 2020 17:33:37 +0200 Subject: [PATCH 1/5] bulk auth origin --- .../Controller/Adminhtml/Bulk/Details.php | 34 +++-- .../Controller/Adminhtml/Bulk/Retry.php | 21 +-- .../Controller/Adminhtml/Index/Index.php | 42 +++--- .../Adminhtml/Notification/Dismiss.php | 18 ++- .../Model/AccessManager.php | 137 ++++++++++++++++++ .../Model/AccessValidator.php | 3 +- .../Model/BulkNotificationManagement.php | 6 + .../Model/BulkOperationsStatus.php | 8 + .../Model/BulkStatus.php | 25 ++++ .../Collection/Synchronized/Plugin.php | 77 ++++++---- .../Test/Unit/Model/AccessManagerTest.php | 94 ++++++++++++ .../Collection/Synchronized/PluginTest.php | 16 +- .../AdminNotification/PluginTest.php | 1 + .../Ui/Component/AdminNotification/Plugin.php | 22 +-- .../DataProvider/Bulk/DataProvider.php | 95 ++++++++++++ .../Component/DataProvider/SearchResult.php | 30 ++-- .../AsynchronousOperations/etc/acl.xml | 11 +- .../etc/adminhtml/menu.xml | 4 +- .../Magento/AsynchronousOperations/etc/di.xml | 3 - .../etc/extension_attributes.xml | 2 +- .../AsynchronousOperations/etc/webapi.xml | 8 +- .../adminhtml/ui_component/bulk_listing.xml | 4 +- .../ui_component/failed_operation_listing.xml | 2 +- .../failed_operation_modal_listing.xml | 2 +- .../retriable_operation_listing.xml | 2 +- .../retriable_operation_modal_listing.xml | 2 +- .../Framework/Bulk/BulkStatusInterface.php | 11 ++ 27 files changed, 547 insertions(+), 133 deletions(-) create mode 100644 app/code/Magento/AsynchronousOperations/Model/AccessManager.php create mode 100644 app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php create mode 100644 app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php index a450187dd094b..b159e7fbd3e42 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php @@ -5,20 +5,26 @@ */ namespace Magento\AsynchronousOperations\Controller\Adminhtml\Bulk; +use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Framework\View\Result\PageFactory; +use Magento\Backend\App\Action\Context; +use Magento\Backend\App\Action; +use Magento\Framework\App\Action\HttpGetActionInterface; + /** * Class View Operation Details Controller */ -class Details extends \Magento\Backend\App\Action implements \Magento\Framework\App\Action\HttpGetActionInterface +class Details extends Action implements HttpGetActionInterface { /** - * @var \Magento\Framework\View\Result\PageFactory + * @var PageFactory */ private $resultPageFactory; /** - * @var \Magento\AsynchronousOperations\Model\AccessValidator + * @var AccessManager */ - private $accessValidator; + private $accessManager; /** * @var string @@ -27,19 +33,20 @@ class Details extends \Magento\Backend\App\Action implements \Magento\Framework\ /** * Details constructor. - * @param \Magento\Backend\App\Action\Context $context - * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory - * @param \Magento\AsynchronousOperations\Model\AccessValidator $accessValidator + * + * @param Context $context + * @param PageFactory $resultPageFactory + * @param AccessManager $accessManager * @param string $menuId */ public function __construct( - \Magento\Backend\App\Action\Context $context, - \Magento\Framework\View\Result\PageFactory $resultPageFactory, - \Magento\AsynchronousOperations\Model\AccessValidator $accessValidator, + Context $context, + PageFactory $resultPageFactory, + AccessManager $accessManager, $menuId = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations' ) { $this->resultPageFactory = $resultPageFactory; - $this->accessValidator = $accessValidator; + $this->accessManager = $accessManager; $this->menuId = $menuId; parent::__construct($context); } @@ -49,10 +56,9 @@ public function __construct( */ protected function _isAllowed() { - return $this->_authorization->isAllowed('Magento_Logging::system_magento_logging_bulk_operations') - && $this->accessValidator->isAllowed($this->getRequest()->getParam('uuid')); + return $this->accessManager->isAllowedForBulkUuid($this->getRequest()->getParam('uuid')); } - + /** * Bulk details action * diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php index 62e6b9ba4551b..484af0dbe32eb 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php @@ -10,13 +10,14 @@ use Magento\Backend\App\Action\Context; use Magento\Backend\Model\View\Result\Redirect; use Magento\Backend\App\Action; -use Magento\AsynchronousOperations\Model\AccessValidator; +use Magento\AsynchronousOperations\Model\AccessManager; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\App\Action\HttpPostActionInterface; /** * Class Bulk Retry Controller */ -class Retry extends Action +class Retry extends Action implements HttpPostActionInterface { /** * @var BulkManagement @@ -29,27 +30,28 @@ class Retry extends Action private $notificationManagement; /** - * @var \Magento\AsynchronousOperations\Model\AccessValidator + * @var AccessManager */ - private $accessValidator; + private $accessManager; /** * Retry constructor. + * * @param Context $context * @param BulkManagement $bulkManagement * @param BulkNotificationManagement $notificationManagement - * @param AccessValidator $accessValidator + * @param AccessManager $accessManager */ public function __construct( Context $context, BulkManagement $bulkManagement, BulkNotificationManagement $notificationManagement, - AccessValidator $accessValidator + AccessManager $accessManager ) { parent::__construct($context); $this->bulkManagement = $bulkManagement; $this->notificationManagement = $notificationManagement; - $this->accessValidator = $accessValidator; + $this->accessManager = $accessManager; } /** @@ -57,12 +59,11 @@ public function __construct( */ protected function _isAllowed() { - return $this->_authorization->isAllowed('Magento_Logging::system_magento_logging_bulk_operations') - && $this->accessValidator->isAllowed($this->getRequest()->getParam('uuid')); + return $this->accessManager->isAllowedForBulkUuid($this->getRequest()->getParam('uuid')); } /** - * {@inheritdoc} + * @inheritdoc */ public function execute() { diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php index 5a2b9c0a34e64..292f9a95594af 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php @@ -6,38 +6,40 @@ namespace Magento\AsynchronousOperations\Controller\Adminhtml\Index; -class Index extends \Magento\Backend\App\Action +use Magento\Backend\App\Action\Context; +use Magento\Framework\View\Result\PageFactory; +use Magento\Framework\View\Result\Page; +use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Backend\App\Action; +use Magento\Framework\App\Action\HttpGetActionInterface; + +class Index extends Action implements HttpGetActionInterface { - /** - * Authorization level of a basic admin session - * - * @see _isAllowed() - */ - const ADMIN_RESOURCE = 'Magento_Logging::system_magento_logging_bulk_operations'; + public const BULK_OPERATIONS_MENU_ID = "Magento_AsynchronousOperations::system_magento_logging_bulk_operations"; /** - * @var \Magento\Framework\View\Result\PageFactory + * @var PageFactory */ private $resultPageFactory; /** - * @var string + * @var AccessManager */ - private $menuId; + private $accessManager; /** * Details constructor. - * @param \Magento\Backend\App\Action\Context $context - * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory - * @param string $menuId + * @param Context $context + * @param PageFactory $resultPageFactory + * @param AccessManager $accessManager */ public function __construct( - \Magento\Backend\App\Action\Context $context, - \Magento\Framework\View\Result\PageFactory $resultPageFactory, - $menuId = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations' + Context $context, + PageFactory $resultPageFactory, + AccessManager $accessManager ) { $this->resultPageFactory = $resultPageFactory; - $this->menuId = $menuId; + $this->accessManager = $accessManager; parent::__construct($context); } @@ -46,19 +48,19 @@ public function __construct( */ protected function _isAllowed() { - return parent::_isAllowed(); + return $this->accessManager->isOwnActionsAllowed(); } /** * Bulk list action * - * @return \Magento\Framework\View\Result\Page + * @return Page */ public function execute() { $resultPage = $this->resultPageFactory->create(); $resultPage->initLayout(); - $this->_setActiveMenu($this->menuId); + $this->_setActiveMenu(self::BULK_OPERATIONS_MENU_ID); $resultPage->getConfig()->getTitle()->prepend(__('Bulk Actions Log')); return $resultPage; } diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php index 0a71c130fb20a..bb38733546ecd 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php @@ -9,29 +9,39 @@ use Magento\Backend\App\Action\Context; use Magento\Backend\App\Action; use Magento\Framework\Controller\ResultFactory; +use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Framework\App\Action\HttpGetActionInterface; /** * Class Bulk Notification Dismiss Controller */ -class Dismiss extends Action +class Dismiss extends Action implements HttpGetActionInterface { /** * @var BulkNotificationManagement */ private $notificationManagement; + /** + * @var AccessManager + */ + private $accessManager; + /** * Class constructor. * * @param Context $context * @param BulkNotificationManagement $notificationManagement + * @param AccessManager $accessManager */ public function __construct( Context $context, - BulkNotificationManagement $notificationManagement + BulkNotificationManagement $notificationManagement, + AccessManager $accessManager ) { parent::__construct($context); $this->notificationManagement = $notificationManagement; + $this->accessManager = $accessManager; } /** @@ -39,11 +49,11 @@ public function __construct( */ protected function _isAllowed() { - return $this->_authorization->isAllowed('Magento_Logging::system_magento_logging_bulk_operations'); + return $this->accessManager->isOwnActionsAllowed(); } /** - * {@inheritdoc} + * @inheritdoc */ public function execute() { diff --git a/app/code/Magento/AsynchronousOperations/Model/AccessManager.php b/app/code/Magento/AsynchronousOperations/Model/AccessManager.php new file mode 100644 index 0000000000000..7b420f10f03ab --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Model/AccessManager.php @@ -0,0 +1,137 @@ +userContext = $userContext; + $this->entityManager = $entityManager; + $this->bulkSummaryFactory = $bulkSummaryFactory; + $this->authorization = $authorization; + $this->allowedUserTypes = $this->getGlobalAllowedUserTypes(); + } + + /** + * Check if content allowed for current use depends from assigned user roles and bulkUuid + * + * @param int $bulkUuid + * @return bool + */ + public function isAllowedForBulkUuid($bulkUuid) + { + + /** @var BulkSummaryInterface $bulkSummary */ + $bulkSummary = $this->entityManager->load( + $this->bulkSummaryFactory->create(), + $bulkUuid + ); + + if (in_array($bulkSummary->getUserType(), $this->allowedUserTypes)) { + return true; + } + + if ($bulkSummary->getUserType() === $this->userContext->getUserType() + && $bulkSummary->getUserId() === $this->userContext->getUserId()) { + return true; + } + + return false; + } + + /** + * Get Allowed user types for current user + * + * @return array + */ + public function getGlobalAllowedUserTypes() + { + $userTypes = [ + self::BULK_LOGGING_ACL_GUESTS => UserContextInterface::USER_TYPE_GUEST, + self::BULK_LOGGING_ACL_INTEGRATIONS => UserContextInterface::USER_TYPE_INTEGRATION, + self::BULK_LOGGING_ACL_ADMIN => UserContextInterface::USER_TYPE_ADMIN, + self::BULK_LOGGING_ACL_CUSTOMERS => UserContextInterface::USER_TYPE_CUSTOMER + ]; + + $allowedUserTypes = []; + foreach ($userTypes as $resourceId => $userTypeId) { + if ($this->authorization->isAllowed($resourceId)) { + $allowedUserTypes[] = $userTypeId; + } + } + + return $allowedUserTypes; + } + + /** + * Check if it allowed to see own bulk operations. + * + * @return bool + */ + public function isOwnActionsAllowed() + { + return $this->authorization->isAllowed(self::BULK_LOGGING_ACL); + } +} diff --git a/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php b/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php index a14ec254cf897..8a5830f6f73a4 100644 --- a/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php +++ b/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php @@ -7,7 +7,8 @@ namespace Magento\AsynchronousOperations\Model; /** - * Class AccessValidator + * Class AccessValidator. Used to validate if user has an access to Bulk Operation + * @deprecated 100.3.0, use Magento\AsynchronousOperations\Model\AccessManager instead */ class AccessValidator { diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php b/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php index 2ba7f7fe5e3ee..d0dd965980100 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php @@ -10,6 +10,7 @@ use Magento\Framework\EntityManager\MetadataPool; use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory as BulkCollectionFactory; use Magento\Framework\Data\Collection; +use \Magento\Authorization\Model\UserContextInterface; /** * Class for bulk notification manager @@ -58,10 +59,12 @@ public function __construct( /** * Mark given bulks as acknowledged. + * * Notifications related to these bulks will not appear in notification area. * * @param array $bulkUuids * @return bool true on success or false on failure + * @throws \Exception */ public function acknowledgeBulks(array $bulkUuids) { @@ -83,6 +86,7 @@ public function acknowledgeBulks(array $bulkUuids) /** * Remove given bulks from acknowledged list. + * * Notifications related to these bulks will appear again in notification area. * * @param array $bulkUuids @@ -119,6 +123,7 @@ public function getAcknowledgedBulksByUser($userId) 'main_table.uuid = acknowledged_bulk.bulk_uuid', [] )->addFieldToFilter('user_id', $userId) + ->addFieldToFilter('user_type', UserContextInterface::USER_TYPE_ADMIN) ->addOrder('start_time', Collection::SORT_ORDER_DESC) ->getItems(); @@ -141,6 +146,7 @@ public function getIgnoredBulksByUser($userId) ['acknowledged_bulk.bulk_uuid'] ); $bulks = $bulkCollection->addFieldToFilter('user_id', $userId) + ->addFieldToFilter('user_type', UserContextInterface::USER_TYPE_ADMIN) ->addFieldToFilter('acknowledged_bulk.bulk_uuid', ['null' => true]) ->addOrder('start_time', Collection::SORT_ORDER_DESC) ->getItems(); diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php b/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php index 14ac44fabd305..76e43d47eda28 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php @@ -95,6 +95,14 @@ public function getBulksByUser($userId) return $this->bulkStatus->getBulksByUser($userId); } + /** + * @inheritDoc + */ + public function getBulksByUserAndType($userId, $userTypeId) + { + return $this->bulkStatus->getBulksByUser($userId, $userTypeId); + } + /** * @inheritDoc */ diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php b/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php index be907f114a41e..fc95135ff6e3b 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php @@ -134,6 +134,31 @@ public function getBulksByUser($userId) return $collection->getItems(); } + /** + * @inheritDoc + */ + public function getBulksByUserAndType($userId, $userTypeId) + { + /** @var ResourceModel\Bulk\Collection $collection */ + $collection = $this->bulkCollectionFactory->create(); + $operationTableName = $this->resourceConnection->getTableName('magento_operation'); + $statusesArray = [ + OperationInterface::STATUS_TYPE_RETRIABLY_FAILED, + OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED, + BulkSummaryInterface::NOT_STARTED, + OperationInterface::STATUS_TYPE_OPEN, + OperationInterface::STATUS_TYPE_COMPLETE + ]; + $select = $collection->getSelect(); + $select->columns(['status' => $this->calculatedStatusSql->get($operationTableName)]) + ->order(new \Zend_Db_Expr('FIELD(status, ' . implode(',', $statusesArray) . ')')); + $collection->addFieldToFilter('user_id', $userId) + ->addFieldToFilter('user_type', $userTypeId) + ->addOrder('start_time'); + + return $collection->getItems(); + } + /** * @inheritDoc */ diff --git a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php index 8457a641ed9a9..bb5c555f42c98 100644 --- a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php @@ -5,92 +5,111 @@ */ namespace Magento\AsynchronousOperations\Model\ResourceModel\System\Message\Collection\Synchronized; +use Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized; +use Magento\AdminNotification\Model\System\MessageFactory; +use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\AsynchronousOperations\Model\BulkNotificationManagement; +use Magento\AsynchronousOperations\Model\Operation\Details; +use Magento\AsynchronousOperations\Model\StatusMapper; +use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\Bulk\BulkStatusInterface; +use Magento\Framework\Encryption\Encryptor; + /** * Class Plugin to add bulks related notification messages to Synchronized Collection */ class Plugin { /** - * @var \Magento\AdminNotification\Model\System\MessageFactory + * @var MessageFactory */ private $messageFactory; /** - * @var \Magento\Framework\Bulk\BulkStatusInterface + * @var BulkStatusInterface */ private $bulkStatus; /** - * @var \Magento\Authorization\Model\UserContextInterface + * @var UserContextInterface */ private $userContext; /** - * @var \Magento\AsynchronousOperations\Model\Operation\Details + * @var Details */ private $operationDetails; /** - * @var \Magento\AsynchronousOperations\Model\BulkNotificationManagement + * @var AccessManager */ - private $bulkNotificationManagement; + private $accessManager; /** - * @var \Magento\Framework\AuthorizationInterface + * @var BulkNotificationManagement */ - private $authorization; + private $bulkNotificationManagement; /** - * @var \Magento\AsynchronousOperations\Model\StatusMapper + * @var StatusMapper */ private $statusMapper; + /** + * @var Encryptor + */ + private $encryptor; + /** * Plugin constructor. * - * @param \Magento\AdminNotification\Model\System\MessageFactory $messageFactory - * @param \Magento\Framework\Bulk\BulkStatusInterface $bulkStatus - * @param \Magento\AsynchronousOperations\Model\BulkNotificationManagement $bulkNotificationManagement - * @param \Magento\Authorization\Model\UserContextInterface $userContext - * @param \Magento\AsynchronousOperations\Model\Operation\Details $operationDetails - * @param \Magento\Framework\AuthorizationInterface $authorization - * @param \Magento\AsynchronousOperations\Model\StatusMapper $statusMapper + * @param MessageFactory $messageFactory + * @param BulkStatusInterface $bulkStatus + * @param BulkNotificationManagement $bulkNotificationManagement + * @param UserContextInterface $userContext + * @param Details $operationDetails + * @param StatusMapper $statusMapper + * @param AccessManager $accessManager + * @param Encryptor $encryptor */ public function __construct( - \Magento\AdminNotification\Model\System\MessageFactory $messageFactory, - \Magento\Framework\Bulk\BulkStatusInterface $bulkStatus, - \Magento\AsynchronousOperations\Model\BulkNotificationManagement $bulkNotificationManagement, - \Magento\Authorization\Model\UserContextInterface $userContext, - \Magento\AsynchronousOperations\Model\Operation\Details $operationDetails, - \Magento\Framework\AuthorizationInterface $authorization, - \Magento\AsynchronousOperations\Model\StatusMapper $statusMapper + MessageFactory $messageFactory, + BulkStatusInterface $bulkStatus, + BulkNotificationManagement $bulkNotificationManagement, + UserContextInterface $userContext, + Details $operationDetails, + StatusMapper $statusMapper, + AccessManager $accessManager, + Encryptor $encryptor ) { $this->messageFactory = $messageFactory; $this->bulkStatus = $bulkStatus; $this->userContext = $userContext; $this->operationDetails = $operationDetails; $this->bulkNotificationManagement = $bulkNotificationManagement; - $this->authorization = $authorization; $this->statusMapper = $statusMapper; + $this->accessManager = $accessManager; + $this->encryptor = $encryptor; } /** * Adding bulk related messages to notification area * - * @param \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $collection + * @param Synchronized $collection * @param array $result * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterToArray( - \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $collection, + Synchronized $collection, $result ) { - if (!$this->authorization->isAllowed('Magento_Logging::system_magento_logging_bulk_operations')) { + if (!$this->accessManager->isOwnActionsAllowed()) { return $result; } $userId = $this->userContext->getUserId(); - $userBulks = $this->bulkStatus->getBulksByUser($userId); + $userType = $this->userContext->getUserType(); + $userBulks = $this->bulkStatus->getBulksByUserAndType($userId, $userType); $acknowledgedBulks = $this->getAcknowledgedBulksUuid( $this->bulkNotificationManagement->getAcknowledgedBulksByUser($userId) ); @@ -108,7 +127,7 @@ public function afterToArray( 'data' => [ 'text' => __('Task "%1": ', $bulk->getDescription()) . $text, 'severity' => \Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR, - 'identity' => md5('bulk' . $bulkUuid), + 'identity' => $this->encryptor->hash('bulk' . $bulkUuid, Encryptor::HASH_VERSION_SHA256), 'uuid' => $bulkUuid, 'status' => $bulkStatus, 'created_at' => $bulk->getStartTime() diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php new file mode 100644 index 0000000000000..b50d848c10421 --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php @@ -0,0 +1,94 @@ +userContextMock = $this->createMock(UserContextInterface::class); + $this->entityManagerMock = $this->createMock(EntityManager::class); + $this->bulkSummaryFactoryMock = $this->createPartialMock( + BulkSummaryInterfaceFactory::class, + ['create'] + ); + $this->authorizationMock = $this->createMock(AuthorizationInterface::class); + + $this->model = new AccessManager( + $this->userContextMock, + $this->entityManagerMock, + $this->bulkSummaryFactoryMock, + $this->authorizationMock + ); + } + + /** + * @dataProvider summaryDataProvider + * @param string $bulkUserId + * @param bool $expectedResult + */ + public function testIsAllowedForBulkUuid($bulkUserId, $expectedResult) + { + $adminId = 1; + $uuid = 'test-001'; + $bulkSummaryMock = $this->createMock(BulkSummaryInterface::class); + + $this->bulkSummaryFactoryMock->expects($this->once())->method('create')->willReturn($bulkSummaryMock); + $this->entityManagerMock->expects($this->once()) + ->method('load') + ->with($bulkSummaryMock, $uuid) + ->willReturn($bulkSummaryMock); + + $bulkSummaryMock->expects($this->once())->method('getUserId')->willReturn($bulkUserId); + $this->userContextMock->expects($this->once())->method('getUserId')->willReturn($adminId); + + $this->assertEquals($this->model->isAllowedForBulkUuid($uuid), $expectedResult); + } + + /** + * @return array + */ + public static function summaryDataProvider() + { + return [ + [2, false], + [1, true] + ]; + } +} diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php index 5365cb64c19c1..534fedf27188d 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php @@ -80,7 +80,7 @@ class PluginTest extends TestCase /** * @var string */ - private $resourceName = 'Magento_Logging::system_magento_logging_bulk_operations'; + private $encryptor; protected function setUp(): void { @@ -103,15 +103,16 @@ protected function setUp(): void $this->bulkNotificationMock, $this->userContextMock, $this->operationsDetailsMock, - $this->authorizationMock, - $this->statusMapper + $this->statusMapper, + $this->accessManager, + $this->encryptor ); } public function testAfterToArrayIfNotAllowed() { $result = []; - $this->authorizationMock + $this->accessManager ->expects($this->once()) ->method('isAllowed') ->with($this->resourceName) @@ -146,10 +147,9 @@ public function testAfterTo($operationDetails) $bulkMock->expects($this->once())->method('getDescription')->willReturn('Bulk Description'); $this->messagefactoryMock->expects($this->once())->method('create')->willReturn($this->messageMock); $this->messageMock->expects($this->once())->method('toArray')->willReturn($bulkArray); - $this->authorizationMock + $this->accessManager ->expects($this->once()) - ->method('isAllowed') - ->with($this->resourceName) + ->method('isOwnActionsAllowed') ->willReturn(true); $this->userContextMock->expects($this->once())->method('getUserId')->willReturn($userId); $this->bulkNotificationMock @@ -158,7 +158,7 @@ public function testAfterTo($operationDetails) ->with($userId) ->willReturn([]); $this->statusMapper->expects($this->once())->method('operationStatusToBulkSummaryStatus'); - $this->bulkStatusMock->expects($this->once())->method('getBulksByUser')->willReturn($userBulks); + $this->bulkStatusMock->expects($this->once())->method('getBulksByUserAndType')->willReturn($userBulks); $result2 = $this->plugin->afterToArray($this->collectionMock, $result); $this->assertEquals(2, $result2['totalRecords']); } diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php index 93e77d271414f..8a4ef13fcc501 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php @@ -10,6 +10,7 @@ use Magento\AdminNotification\Ui\Component\DataProvider\DataProvider; use Magento\AsynchronousOperations\Ui\Component\AdminNotification\Plugin; use Magento\Framework\AuthorizationInterface; +use Magento\AsynchronousOperations\Model\AccessManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php index b5670639dce09..69371444d77b9 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php @@ -6,15 +6,18 @@ namespace Magento\AsynchronousOperations\Ui\Component\AdminNotification; +use Magento\AdminNotification\Ui\Component\DataProvider\DataProvider; +use Magento\AsynchronousOperations\Model\AccessManager; + /** * Class Plugin to eliminate Bulk related links in the notification area */ class Plugin { /** - * @var \Magento\Framework\AuthorizationInterface + * @var AccessManager */ - private $authorization; + private $accessManager; /** * @var bool @@ -23,30 +26,29 @@ class Plugin /** * Plugin constructor. - * @param \Magento\Framework\AuthorizationInterface $authorization + * + * @param AccessManager $accessManager */ public function __construct( - \Magento\Framework\AuthorizationInterface $authorization + AccessManager $accessManager ) { - $this->authorization = $authorization; + $this->accessManager = $accessManager; } /** * Prepares Meta * - * @param \Magento\AdminNotification\Ui\Component\DataProvider\DataProvider $dataProvider + * @param DataProvider $dataProvider * @param array $result * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetMeta( - \Magento\AdminNotification\Ui\Component\DataProvider\DataProvider $dataProvider, + DataProvider $dataProvider, $result ) { if (!isset($this->isAllowed)) { - $this->isAllowed = $this->authorization->isAllowed( - 'Magento_Logging::system_magento_logging_bulk_operations' - ); + $this->isAllowed = $this->accessManager->isOwnActionsAllowed(); } $result['columns']['arguments']['data']['config']['isAllowed'] = $this->isAllowed; return $result; diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php new file mode 100644 index 0000000000000..cae5c66ce4b8b --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php @@ -0,0 +1,95 @@ +filterBuilder = $filterBuilder; + $this->accessManager = $accessManager; + $this->userContext = $userContext; + $this->collection = $collectionFactory->create(); + parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); + } + + /** + * Get data for Bulk Operations Grid + * + * @return array + */ + public function getData() + { + $allowedUserTypes = $this->accessManager->getGlobalAllowedUserTypes(); + $connection = $this->getCollection()->getConnection(); + $whereOr = []; + if (count($allowedUserTypes) > 0) { + $whereOr[] = $connection->quoteInto("user_type IN(?)", $allowedUserTypes); + } + + if ($this->accessManager->isOwnActionsAllowed()) { + $whereOr[] = implode( + ' AND ', + [ + $connection->quoteInto('user_type = ?', $this->userContext->getUserType()), + $connection->quoteInto('user_id = ?', $this->userContext->getUserId()) + ] + ); + } + + $whereCond = '(' . implode(') OR (', $whereOr) . ')'; + $this->getCollection()->getSelect()->where($whereCond); + + return $this->getCollection()->toArray(); + } +} diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php index 5f2fbd9ea8b11..0a337b620e6ba 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php @@ -9,21 +9,15 @@ use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory; use Magento\Framework\Event\ManagerInterface as EventManager; use Psr\Log\LoggerInterface as Logger; -use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\Bulk\BulkSummaryInterface; use Magento\AsynchronousOperations\Model\StatusMapper; use Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql; /** - * Class SearchResult + * Implementing of Search Results for Bulk Operations */ class SearchResult extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult { - /** - * @var UserContextInterface - */ - private $userContext; - /** * @var StatusMapper */ @@ -45,27 +39,25 @@ class SearchResult extends \Magento\Framework\View\Element\UiComponent\DataProvi * @param Logger $logger * @param FetchStrategy $fetchStrategy * @param EventManager $eventManager - * @param UserContextInterface $userContextInterface * @param StatusMapper $statusMapper * @param CalculatedStatusSql $calculatedStatusSql * @param string $mainTable - * @param null $resourceModel + * @param null|string $resourceModel * @param string $identifierName * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct( EntityFactory $entityFactory, Logger $logger, FetchStrategy $fetchStrategy, EventManager $eventManager, - UserContextInterface $userContextInterface, StatusMapper $statusMapper, CalculatedStatusSql $calculatedStatusSql, $mainTable = 'magento_bulk', $resourceModel = null, $identifierName = 'uuid' ) { - $this->userContext = $userContextInterface; $this->statusMapper = $statusMapper; $this->calculatedStatusSql = $calculatedStatusSql; parent::__construct( @@ -80,7 +72,7 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ protected function _initSelect() { @@ -90,15 +82,12 @@ protected function _initSelect() '*', 'status' => $this->calculatedStatusSql->get($this->getTable('magento_operation')) ] - )->where( - 'user_id=?', - $this->userContext->getUserId() ); return $this; } /** - * {@inheritdoc} + * @inheritdoc */ protected function _afterLoad() { @@ -110,7 +99,12 @@ protected function _afterLoad() } /** - * {@inheritdoc} + * Add additional field for filter request + * + * @param array|string $field + * @param string|array $condition + * + * @return $this */ public function addFieldToFilter($field, $condition = null) { @@ -133,7 +127,7 @@ public function addFieldToFilter($field, $condition = null) } /** - * {@inheritdoc} + * @inheritdoc */ public function getSelectCountSql() { diff --git a/app/code/Magento/AsynchronousOperations/etc/acl.xml b/app/code/Magento/AsynchronousOperations/etc/acl.xml index 42521ad40ff63..b49d327a78fba 100644 --- a/app/code/Magento/AsynchronousOperations/etc/acl.xml +++ b/app/code/Magento/AsynchronousOperations/etc/acl.xml @@ -10,9 +10,14 @@ - - - + + + + + + + + diff --git a/app/code/Magento/AsynchronousOperations/etc/adminhtml/menu.xml b/app/code/Magento/AsynchronousOperations/etc/adminhtml/menu.xml index 2e9fe34c45cec..455041f7d01ec 100644 --- a/app/code/Magento/AsynchronousOperations/etc/adminhtml/menu.xml +++ b/app/code/Magento/AsynchronousOperations/etc/adminhtml/menu.xml @@ -13,7 +13,7 @@ module="Magento_AsynchronousOperations" sortOrder="70" parent="Magento_Backend::system" dependsOnModule="Magento_AsynchronousOperations" - resource="Magento_Logging::magento_logging"/> + resource="Magento_AsynchronousOperations::magento_logging"/> + resource="Magento_AsynchronousOperations::system_magento_logging_bulk_operations"/> diff --git a/app/code/Magento/AsynchronousOperations/etc/di.xml b/app/code/Magento/AsynchronousOperations/etc/di.xml index 820bdd26e62b2..7b5d8bbd3d6d5 100644 --- a/app/code/Magento/AsynchronousOperations/etc/di.xml +++ b/app/code/Magento/AsynchronousOperations/etc/di.xml @@ -93,9 +93,6 @@ - diff --git a/app/code/Magento/AsynchronousOperations/etc/extension_attributes.xml b/app/code/Magento/AsynchronousOperations/etc/extension_attributes.xml index 6eeda62373f06..dbbeda4900006 100644 --- a/app/code/Magento/AsynchronousOperations/etc/extension_attributes.xml +++ b/app/code/Magento/AsynchronousOperations/etc/extension_attributes.xml @@ -9,7 +9,7 @@ - + start_time diff --git a/app/code/Magento/AsynchronousOperations/etc/webapi.xml b/app/code/Magento/AsynchronousOperations/etc/webapi.xml index 4c10a5756c8d6..97b6c09f88285 100644 --- a/app/code/Magento/AsynchronousOperations/etc/webapi.xml +++ b/app/code/Magento/AsynchronousOperations/etc/webapi.xml @@ -11,28 +11,28 @@ - + - + - + - + diff --git a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/bulk_listing.xml b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/bulk_listing.xml index 87dc0525eb1c0..25b9c76fd5e3e 100644 --- a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/bulk_listing.xml +++ b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/bulk_listing.xml @@ -24,8 +24,8 @@ - Magento_Logging::system_magento_logging_bulk_operations - + Magento_AsynchronousOperations::system_magento_logging_bulk_operations + id id diff --git a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_listing.xml b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_listing.xml index 2ac762e398521..dc8e1d2d689c8 100644 --- a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_listing.xml +++ b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_listing.xml @@ -24,7 +24,7 @@ - Magento_Logging::system_magento_logging_bulk_operations + Magento_AsynchronousOperations::system_magento_logging_bulk_operations id diff --git a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_modal_listing.xml b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_modal_listing.xml index 62a4935da8ba7..c10c9e7b3b63c 100644 --- a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_modal_listing.xml +++ b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/failed_operation_modal_listing.xml @@ -24,7 +24,7 @@ - Magento_Logging::system_magento_logging_bulk_operations + Magento_AsynchronousOperations::system_magento_logging_bulk_operations id diff --git a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_listing.xml b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_listing.xml index 3618e10ee77d8..ab2bf3542d8c4 100644 --- a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_listing.xml +++ b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_listing.xml @@ -24,7 +24,7 @@ - Magento_Logging::system_magento_logging_bulk_operations + Magento_AsynchronousOperations::system_magento_logging_bulk_operations id diff --git a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_modal_listing.xml b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_modal_listing.xml index 97e3e897c2533..6014c14281e47 100644 --- a/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_modal_listing.xml +++ b/app/code/Magento/AsynchronousOperations/view/adminhtml/ui_component/retriable_operation_modal_listing.xml @@ -24,7 +24,7 @@ - Magento_Logging::system_magento_logging_bulk_operations + Magento_AsynchronousOperations::system_magento_logging_bulk_operations id diff --git a/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php b/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php index 45352af9c5c8c..4057d61957f64 100644 --- a/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php +++ b/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php @@ -38,9 +38,20 @@ public function getOperationsCountByBulkIdAndStatus($bulkUuid, $status); * @param int $userId * @return BulkSummaryInterface[] * @since 103.0.0 + * @deprecated 100.3.0 */ public function getBulksByUser($userId); + /** + * Get all bulks created by user and user type + * + * @param int $userId + * @param int $userTypeId + * @return BulkSummaryInterface[] + * @since 100.2.0 + */ + public function getBulksByUserAndType($userId, $userTypeId); + /** * Computational status based on statuses of belonging operations * From 1d7dea8c79aab2bbdbe2c28d1ffc148d72a34b7b Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Thu, 5 Nov 2020 10:34:18 +0200 Subject: [PATCH 2/5] fix unit tests --- .../Test/Unit/Model/AccessManagerTest.php | 36 ++++++++++++------- .../Collection/Synchronized/PluginTest.php | 14 ++++++-- .../AdminNotification/PluginTest.php | 16 ++++----- .../Ui/Component/AdminNotification/Plugin.php | 9 ++--- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php index b50d848c10421..1e5f28ed4841c 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php @@ -12,8 +12,10 @@ use Magento\Framework\AuthorizationInterface; use Magento\Framework\EntityManager\EntityManager; use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; -class AccessManagerTest extends \PHPUnit\Framework\TestCase +class AccessManagerTest extends TestCase { /** * @var AccessManager @@ -21,26 +23,29 @@ class AccessManagerTest extends \PHPUnit\Framework\TestCase private $model; /** - * @var \PHPUnit\Framework\MockObject\MockObject + * @var MockObject */ private $userContextMock; /** - * @var \PHPUnit\Framework\MockObject\MockObject + * @var MockObject */ private $entityManagerMock; /** - * @var \PHPUnit\Framework\MockObject\MockObject + * @var MockObject */ private $bulkSummaryFactoryMock; /** - * @var \PHPUnit\Framework\MockObject\MockObject + * @var MockObject */ private $authorizationMock; - protected function setUp() + /** + * @inheritDoc + */ + protected function setUp(): void { $this->userContextMock = $this->createMock(UserContextInterface::class); $this->entityManagerMock = $this->createMock(EntityManager::class); @@ -60,23 +65,30 @@ protected function setUp() /** * @dataProvider summaryDataProvider - * @param string $bulkUserId + * @param int $bulkUserId * @param bool $expectedResult + * @return void */ - public function testIsAllowedForBulkUuid($bulkUserId, $expectedResult) + public function testIsAllowedForBulkUuid(int $bulkUserId, bool $expectedResult): void { $adminId = 1; $uuid = 'test-001'; $bulkSummaryMock = $this->createMock(BulkSummaryInterface::class); - $this->bulkSummaryFactoryMock->expects($this->once())->method('create')->willReturn($bulkSummaryMock); + $this->bulkSummaryFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($bulkSummaryMock); $this->entityManagerMock->expects($this->once()) ->method('load') ->with($bulkSummaryMock, $uuid) ->willReturn($bulkSummaryMock); - $bulkSummaryMock->expects($this->once())->method('getUserId')->willReturn($bulkUserId); - $this->userContextMock->expects($this->once())->method('getUserId')->willReturn($adminId); + $bulkSummaryMock->expects($this->once()) + ->method('getUserId') + ->willReturn($bulkUserId); + $this->userContextMock->expects($this->once()) + ->method('getUserId') + ->willReturn($adminId); $this->assertEquals($this->model->isAllowedForBulkUuid($uuid), $expectedResult); } @@ -84,7 +96,7 @@ public function testIsAllowedForBulkUuid($bulkUserId, $expectedResult) /** * @return array */ - public static function summaryDataProvider() + public static function summaryDataProvider(): array { return [ [2, false], diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php index 534fedf27188d..f52aaa98d7845 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php @@ -11,6 +11,7 @@ use Magento\AdminNotification\Model\System\Message; use Magento\AdminNotification\Model\System\MessageFactory; use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface; +use Magento\AsynchronousOperations\Model\AccessManager; use Magento\AsynchronousOperations\Model\BulkNotificationManagement; use Magento\AsynchronousOperations\Model\BulkSummary; use Magento\AsynchronousOperations\Model\Operation\Details; @@ -19,6 +20,7 @@ use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\AuthorizationInterface; use Magento\Framework\Bulk\BulkStatusInterface; +use Magento\Framework\Encryption\Encryptor; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -78,7 +80,12 @@ class PluginTest extends TestCase private $statusMapper; /** - * @var string + * @var AccessManager|MockObject + */ + private $accessManager; + + /** + * @var Encryptor|MockObject */ private $encryptor; @@ -97,6 +104,8 @@ protected function setUp(): void $this->collectionMock = $this->createMock(Synchronized::class); $this->bulkNotificationMock = $this->createMock(BulkNotificationManagement::class); $this->statusMapper = $this->createMock(StatusMapper::class); + $this->accessManager = $this->createMock(AccessManager::class); + $this->encryptor = $this->createMock(Encryptor::class); $this->plugin = new Plugin( $this->messagefactoryMock, $this->bulkStatusMock, @@ -114,8 +123,7 @@ public function testAfterToArrayIfNotAllowed() $result = []; $this->accessManager ->expects($this->once()) - ->method('isAllowed') - ->with($this->resourceName) + ->method('isOwnActionsAllowed') ->willReturn(false); $this->assertEquals($result, $this->plugin->afterToArray($this->collectionMock, $result)); } diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php index 8a4ef13fcc501..86bc4955e79c8 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php @@ -9,7 +9,6 @@ use Magento\AdminNotification\Ui\Component\DataProvider\DataProvider; use Magento\AsynchronousOperations\Ui\Component\AdminNotification\Plugin; -use Magento\Framework\AuthorizationInterface; use Magento\AsynchronousOperations\Model\AccessManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -22,16 +21,14 @@ class PluginTest extends TestCase private $plugin; /** - * @var MockObject + * @var AccessManager|MockObject */ - private $authorizationMock; + private $accessMangerMock; protected function setUp(): void { - $this->authorizationMock = $this->getMockForAbstractClass(AuthorizationInterface::class); - $this->plugin = new Plugin( - $this->authorizationMock - ); + $this->accessMangerMock = $this->createMock(AccessManager::class); + $this->plugin = new Plugin($this->accessMangerMock); } public function testAfterGetMeta() @@ -49,7 +46,10 @@ public function testAfterGetMeta() ] ]; $dataProviderMock = $this->createMock(DataProvider::class); - $this->authorizationMock->expects($this->once())->method('isAllowed')->willReturn(true); + $this->accessMangerMock->expects($this->once()) + ->method('isOwnActionsAllowed') + ->willReturn(true); + $this->assertEquals($expectedResult, $this->plugin->afterGetMeta($dataProviderMock, $result)); } } diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php index 69371444d77b9..23d1cb867b7fc 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php @@ -25,8 +25,6 @@ class Plugin private $isAllowed; /** - * Plugin constructor. - * * @param AccessManager $accessManager */ public function __construct( @@ -43,14 +41,13 @@ public function __construct( * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterGetMeta( - DataProvider $dataProvider, - $result - ) { + public function afterGetMeta(DataProvider $dataProvider, $result) + { if (!isset($this->isAllowed)) { $this->isAllowed = $this->accessManager->isOwnActionsAllowed(); } $result['columns']['arguments']['data']['config']['isAllowed'] = $this->isAllowed; + return $result; } } From 69badcd0ba4e7eb3d604fb0e21b64ade5b650997 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Thu, 5 Nov 2020 17:58:10 +0200 Subject: [PATCH 3/5] split logic, revert BIC --- .../Controller/Adminhtml/Bulk/Details.php | 37 +++-- .../Controller/Adminhtml/Bulk/Retry.php | 30 ++-- .../Controller/Adminhtml/Index/Index.php | 30 ++-- .../Adminhtml/Notification/Dismiss.php | 34 +---- .../Model/AccessManager.php | 137 ------------------ .../Model/AccessValidator.php | 2 +- .../Model/BulkOperationsStatus.php | 8 - .../Model/BulkStatus.php | 25 ---- .../Model/GetBulksByUserAndType.php | 83 +++++++++++ .../Model/GetGlobalAllowedUserTypes.php | 64 ++++++++ .../Model/IsAllowedForBulkUuid.php | 84 +++++++++++ .../Collection/Synchronized/Plugin.php | 71 ++++++--- .../Ui/Component/AdminNotification/Plugin.php | 29 ++-- .../DataProvider/Bulk/DataProvider.php | 58 +++++--- .../Magento/AsynchronousOperations/etc/di.xml | 1 + .../Framework/Bulk/BulkStatusInterface.php | 12 +- .../Bulk/GetBulksByUserAndTypeInterface.php | 24 +++ 17 files changed, 423 insertions(+), 306 deletions(-) delete mode 100644 app/code/Magento/AsynchronousOperations/Model/AccessManager.php create mode 100644 app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php create mode 100644 app/code/Magento/AsynchronousOperations/Model/GetGlobalAllowedUserTypes.php create mode 100644 app/code/Magento/AsynchronousOperations/Model/IsAllowedForBulkUuid.php create mode 100644 lib/internal/Magento/Framework/Bulk/GetBulksByUserAndTypeInterface.php diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php index b159e7fbd3e42..27258efee4c54 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Details.php @@ -5,11 +5,14 @@ */ namespace Magento\AsynchronousOperations\Controller\Adminhtml\Bulk; -use Magento\AsynchronousOperations\Model\AccessManager; -use Magento\Framework\View\Result\PageFactory; -use Magento\Backend\App\Action\Context; +use Magento\AsynchronousOperations\Model\AccessValidator; +use Magento\AsynchronousOperations\Model\IsAllowedForBulkUuid; use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\View\Result\Page; +use Magento\Framework\View\Result\PageFactory; /** * Class View Operation Details Controller @@ -22,9 +25,10 @@ class Details extends Action implements HttpGetActionInterface private $resultPageFactory; /** - * @var AccessManager + * @var AccessValidator + * @deprecated */ - private $accessManager; + private $accessValidator; /** * @var string @@ -32,22 +36,29 @@ class Details extends Action implements HttpGetActionInterface private $menuId; /** - * Details constructor. - * + * @var IsAllowedForBulkUuid + */ + private $isAllowedForBulkUuid; + + /** * @param Context $context * @param PageFactory $resultPageFactory - * @param AccessManager $accessManager + * @param AccessValidator $accessValidator * @param string $menuId + * @param IsAllowedForBulkUuid|null $isAllowedForBulkUuid */ public function __construct( Context $context, PageFactory $resultPageFactory, - AccessManager $accessManager, - $menuId = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations' + AccessValidator $accessValidator, + $menuId = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations', + ?IsAllowedForBulkUuid $isAllowedForBulkUuid = null ) { $this->resultPageFactory = $resultPageFactory; - $this->accessManager = $accessManager; + $this->accessValidator = $accessValidator; $this->menuId = $menuId; + $this->isAllowedForBulkUuid = $isAllowedForBulkUuid + ?: ObjectManager::getInstance()->get(IsAllowedForBulkUuid::class); parent::__construct($context); } @@ -56,13 +67,13 @@ public function __construct( */ protected function _isAllowed() { - return $this->accessManager->isAllowedForBulkUuid($this->getRequest()->getParam('uuid')); + return $this->isAllowedForBulkUuid->execute($this->getRequest()->getParam('uuid')); } /** * Bulk details action * - * @return \Magento\Framework\View\Result\Page + * @return Page */ public function execute() { diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php index 484af0dbe32eb..347eb0e909cb4 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php @@ -7,12 +7,15 @@ use Magento\AsynchronousOperations\Model\BulkManagement; use Magento\AsynchronousOperations\Model\BulkNotificationManagement; +use Magento\AsynchronousOperations\Model\IsAllowedForBulkUuid; use Magento\Backend\App\Action\Context; use Magento\Backend\Model\View\Result\Redirect; use Magento\Backend\App\Action; -use Magento\AsynchronousOperations\Model\AccessManager; -use Magento\Framework\Controller\ResultFactory; +use Magento\AsynchronousOperations\Model\AccessValidator; use Magento\Framework\App\Action\HttpPostActionInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Bulk\GetBulksByUserAndTypeInterface; +use Magento\Framework\Controller\ResultFactory; /** * Class Bulk Retry Controller @@ -30,28 +33,35 @@ class Retry extends Action implements HttpPostActionInterface private $notificationManagement; /** - * @var AccessManager + * @var AccessValidator + */ + private $accessValidator; + + /** + * @var IsAllowedForBulkUuid */ - private $accessManager; + private $isAllowedForBulkUuid; /** - * Retry constructor. - * * @param Context $context * @param BulkManagement $bulkManagement * @param BulkNotificationManagement $notificationManagement - * @param AccessManager $accessManager + * @param AccessValidator $accessValidator + * @param IsAllowedForBulkUuid|null $isAllowedForBulkUuid */ public function __construct( Context $context, BulkManagement $bulkManagement, BulkNotificationManagement $notificationManagement, - AccessManager $accessManager + AccessValidator $accessValidator, + ?IsAllowedForBulkUuid $isAllowedForBulkUuid = null ) { parent::__construct($context); $this->bulkManagement = $bulkManagement; $this->notificationManagement = $notificationManagement; - $this->accessManager = $accessManager; + $this->accessValidator = $accessValidator; + $this->isAllowedForBulkUuid = $isAllowedForBulkUuid + ?: ObjectManager::getInstance()->get(IsAllowedForBulkUuid::class); } /** @@ -59,7 +69,7 @@ public function __construct( */ protected function _isAllowed() { - return $this->accessManager->isAllowedForBulkUuid($this->getRequest()->getParam('uuid')); + return $this->isAllowedForBulkUuid->execute($this->getRequest()->getParam('uuid')); } /** diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php index 292f9a95594af..87e2941bff255 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Index/Index.php @@ -6,16 +6,15 @@ namespace Magento\AsynchronousOperations\Controller\Adminhtml\Index; +use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; -use Magento\Framework\View\Result\PageFactory; use Magento\Framework\View\Result\Page; -use Magento\AsynchronousOperations\Model\AccessManager; -use Magento\Backend\App\Action; -use Magento\Framework\App\Action\HttpGetActionInterface; +use \Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\View\Result\PageFactory; class Index extends Action implements HttpGetActionInterface { - public const BULK_OPERATIONS_MENU_ID = "Magento_AsynchronousOperations::system_magento_logging_bulk_operations"; + public const ADMIN_RESOURCE = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations'; /** * @var PageFactory @@ -23,34 +22,25 @@ class Index extends Action implements HttpGetActionInterface private $resultPageFactory; /** - * @var AccessManager + * @var string */ - private $accessManager; + private $menuId; /** - * Details constructor. * @param Context $context * @param PageFactory $resultPageFactory - * @param AccessManager $accessManager + * @param string $menuId */ public function __construct( Context $context, PageFactory $resultPageFactory, - AccessManager $accessManager + $menuId = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations' ) { $this->resultPageFactory = $resultPageFactory; - $this->accessManager = $accessManager; + $this->menuId = $menuId; parent::__construct($context); } - /** - * @inheritDoc - */ - protected function _isAllowed() - { - return $this->accessManager->isOwnActionsAllowed(); - } - /** * Bulk list action * @@ -60,7 +50,7 @@ public function execute() { $resultPage = $this->resultPageFactory->create(); $resultPage->initLayout(); - $this->_setActiveMenu(self::BULK_OPERATIONS_MENU_ID); + $this->_setActiveMenu($this->menuId); $resultPage->getConfig()->getTitle()->prepend(__('Bulk Actions Log')); return $resultPage; } diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php index bb38733546ecd..ac75d09974b7d 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Notification/Dismiss.php @@ -6,50 +6,32 @@ namespace Magento\AsynchronousOperations\Controller\Adminhtml\Notification; use Magento\AsynchronousOperations\Model\BulkNotificationManagement; -use Magento\Backend\App\Action\Context; use Magento\Backend\App\Action; -use Magento\Framework\Controller\ResultFactory; -use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Backend\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\Controller\Result\Json; +use Magento\Framework\Controller\ResultFactory; /** * Class Bulk Notification Dismiss Controller */ class Dismiss extends Action implements HttpGetActionInterface { + public const ADMIN_RESOURCE = 'Magento_AsynchronousOperations::system_magento_logging_bulk_operations'; + /** * @var BulkNotificationManagement */ private $notificationManagement; /** - * @var AccessManager - */ - private $accessManager; - - /** - * Class constructor. - * * @param Context $context * @param BulkNotificationManagement $notificationManagement - * @param AccessManager $accessManager */ - public function __construct( - Context $context, - BulkNotificationManagement $notificationManagement, - AccessManager $accessManager - ) { + public function __construct(Context $context, BulkNotificationManagement $notificationManagement) + { parent::__construct($context); $this->notificationManagement = $notificationManagement; - $this->accessManager = $accessManager; - } - - /** - * @inheritDoc - */ - protected function _isAllowed() - { - return $this->accessManager->isOwnActionsAllowed(); } /** @@ -64,7 +46,7 @@ public function execute() $isAcknowledged = $this->notificationManagement->acknowledgeBulks($bulkUuids); - /** @var \Magento\Framework\Controller\Result\Json $result */ + /** @var Json $result */ $result = $this->resultFactory->create(ResultFactory::TYPE_JSON); if (!$isAcknowledged) { $result->setHttpResponseCode(400); diff --git a/app/code/Magento/AsynchronousOperations/Model/AccessManager.php b/app/code/Magento/AsynchronousOperations/Model/AccessManager.php deleted file mode 100644 index 7b420f10f03ab..0000000000000 --- a/app/code/Magento/AsynchronousOperations/Model/AccessManager.php +++ /dev/null @@ -1,137 +0,0 @@ -userContext = $userContext; - $this->entityManager = $entityManager; - $this->bulkSummaryFactory = $bulkSummaryFactory; - $this->authorization = $authorization; - $this->allowedUserTypes = $this->getGlobalAllowedUserTypes(); - } - - /** - * Check if content allowed for current use depends from assigned user roles and bulkUuid - * - * @param int $bulkUuid - * @return bool - */ - public function isAllowedForBulkUuid($bulkUuid) - { - - /** @var BulkSummaryInterface $bulkSummary */ - $bulkSummary = $this->entityManager->load( - $this->bulkSummaryFactory->create(), - $bulkUuid - ); - - if (in_array($bulkSummary->getUserType(), $this->allowedUserTypes)) { - return true; - } - - if ($bulkSummary->getUserType() === $this->userContext->getUserType() - && $bulkSummary->getUserId() === $this->userContext->getUserId()) { - return true; - } - - return false; - } - - /** - * Get Allowed user types for current user - * - * @return array - */ - public function getGlobalAllowedUserTypes() - { - $userTypes = [ - self::BULK_LOGGING_ACL_GUESTS => UserContextInterface::USER_TYPE_GUEST, - self::BULK_LOGGING_ACL_INTEGRATIONS => UserContextInterface::USER_TYPE_INTEGRATION, - self::BULK_LOGGING_ACL_ADMIN => UserContextInterface::USER_TYPE_ADMIN, - self::BULK_LOGGING_ACL_CUSTOMERS => UserContextInterface::USER_TYPE_CUSTOMER - ]; - - $allowedUserTypes = []; - foreach ($userTypes as $resourceId => $userTypeId) { - if ($this->authorization->isAllowed($resourceId)) { - $allowedUserTypes[] = $userTypeId; - } - } - - return $allowedUserTypes; - } - - /** - * Check if it allowed to see own bulk operations. - * - * @return bool - */ - public function isOwnActionsAllowed() - { - return $this->authorization->isAllowed(self::BULK_LOGGING_ACL); - } -} diff --git a/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php b/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php index 8a5830f6f73a4..7e44e0cf2de6d 100644 --- a/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php +++ b/app/code/Magento/AsynchronousOperations/Model/AccessValidator.php @@ -8,7 +8,7 @@ /** * Class AccessValidator. Used to validate if user has an access to Bulk Operation - * @deprecated 100.3.0, use Magento\AsynchronousOperations\Model\AccessManager instead + * @deprecated see \Magento\AsynchronousOperations\Model\IsAllowedForBulkUuid */ class AccessValidator { diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php b/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php index 76e43d47eda28..14ac44fabd305 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkOperationsStatus.php @@ -95,14 +95,6 @@ public function getBulksByUser($userId) return $this->bulkStatus->getBulksByUser($userId); } - /** - * @inheritDoc - */ - public function getBulksByUserAndType($userId, $userTypeId) - { - return $this->bulkStatus->getBulksByUser($userId, $userTypeId); - } - /** * @inheritDoc */ diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php b/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php index fc95135ff6e3b..be907f114a41e 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkStatus.php @@ -134,31 +134,6 @@ public function getBulksByUser($userId) return $collection->getItems(); } - /** - * @inheritDoc - */ - public function getBulksByUserAndType($userId, $userTypeId) - { - /** @var ResourceModel\Bulk\Collection $collection */ - $collection = $this->bulkCollectionFactory->create(); - $operationTableName = $this->resourceConnection->getTableName('magento_operation'); - $statusesArray = [ - OperationInterface::STATUS_TYPE_RETRIABLY_FAILED, - OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED, - BulkSummaryInterface::NOT_STARTED, - OperationInterface::STATUS_TYPE_OPEN, - OperationInterface::STATUS_TYPE_COMPLETE - ]; - $select = $collection->getSelect(); - $select->columns(['status' => $this->calculatedStatusSql->get($operationTableName)]) - ->order(new \Zend_Db_Expr('FIELD(status, ' . implode(',', $statusesArray) . ')')); - $collection->addFieldToFilter('user_id', $userId) - ->addFieldToFilter('user_type', $userTypeId) - ->addOrder('start_time'); - - return $collection->getItems(); - } - /** * @inheritDoc */ diff --git a/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php b/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php new file mode 100644 index 0000000000000..aad98d55623e0 --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php @@ -0,0 +1,83 @@ +bulkCollectionFactory = $bulkCollection; + $this->resourceConnection = $resourceConnection; + $this->calculatedStatusSql = $calculatedStatusSql; + } + + /** + * @inheritDoc + */ + public function execute($userId, $userTypeId): array + { + /** @var Collection $collection */ + $collection = $this->bulkCollectionFactory->create(); + $operationTableName = $this->resourceConnection->getTableName('magento_operation'); + + $select = $collection->getSelect(); + $select->columns(['status' => $this->calculatedStatusSql->get($operationTableName)]) + ->order(new \Zend_Db_Expr('FIELD(status, ' . implode(',', $this->statusesArray) . ')')); + $collection->addFieldToFilter('user_id', $userId) + ->addFieldToFilter('user_type', $userTypeId) + ->addOrder('start_time'); + + return $collection->getItems(); + } +} diff --git a/app/code/Magento/AsynchronousOperations/Model/GetGlobalAllowedUserTypes.php b/app/code/Magento/AsynchronousOperations/Model/GetGlobalAllowedUserTypes.php new file mode 100644 index 0000000000000..367aa6f8e9527 --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Model/GetGlobalAllowedUserTypes.php @@ -0,0 +1,64 @@ +authorization = $authorization; + } + + /** + * Returns allowed user types + * + * @return array + */ + public function execute(): array + { + $userTypes = [ + self::BULK_LOGGING_ACL_GUESTS => UserContextInterface::USER_TYPE_GUEST, + self::BULK_LOGGING_ACL_INTEGRATIONS => UserContextInterface::USER_TYPE_INTEGRATION, + self::BULK_LOGGING_ACL_ADMIN => UserContextInterface::USER_TYPE_ADMIN, + self::BULK_LOGGING_ACL_CUSTOMERS => UserContextInterface::USER_TYPE_CUSTOMER + ]; + + $allowedUserTypes = []; + foreach ($userTypes as $resourceId => $userTypeId) { + if ($this->authorization->isAllowed($resourceId)) { + $allowedUserTypes[] = $userTypeId; + } + } + + return $allowedUserTypes; + } +} diff --git a/app/code/Magento/AsynchronousOperations/Model/IsAllowedForBulkUuid.php b/app/code/Magento/AsynchronousOperations/Model/IsAllowedForBulkUuid.php new file mode 100644 index 0000000000000..2c98b0cc33ecc --- /dev/null +++ b/app/code/Magento/AsynchronousOperations/Model/IsAllowedForBulkUuid.php @@ -0,0 +1,84 @@ +userContext = $userContext; + $this->entityManager = $entityManager; + $this->bulkSummaryFactory = $bulkSummaryFactory; + $this->allowedUserTypes = $getGlobalAllowedUserTypes->execute(); + } + + /** + * Returns is content allowed + * + * @param string $bulkUuid + * @return bool + */ + public function execute(string $bulkUuid): bool + { + /** @var BulkSummaryInterface $bulkSummary */ + $bulkSummary = $this->entityManager->load($this->bulkSummaryFactory->create(), $bulkUuid); + + return in_array($bulkSummary->getUserType(), $this->allowedUserTypes) || $this->isAllowedForUser($bulkSummary); + } + + /** + * Returns is bulk allowed for user + * + * @param BulkSummaryInterface $bulkSummary + * @return bool + */ + private function isAllowedForUser(BulkSummaryInterface $bulkSummary): bool + { + return $bulkSummary->getUserType() === $this->userContext->getUserType() + && $bulkSummary->getUserId() === $this->userContext->getUserId(); + } +} diff --git a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php index bb5c555f42c98..7f63eebf1a5ff 100644 --- a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php @@ -7,19 +7,26 @@ use Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized; use Magento\AdminNotification\Model\System\MessageFactory; -use Magento\AsynchronousOperations\Model\AccessManager; use Magento\AsynchronousOperations\Model\BulkNotificationManagement; use Magento\AsynchronousOperations\Model\Operation\Details; use Magento\AsynchronousOperations\Model\StatusMapper; use Magento\Authorization\Model\UserContextInterface; +use Magento\Framework\AuthorizationInterface; use Magento\Framework\Bulk\BulkStatusInterface; +use Magento\Framework\Bulk\BulkSummaryInterface; +use Magento\Framework\Bulk\GetBulksByUserAndTypeInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Encryption\Encryptor; +use Magento\Framework\Notification\MessageInterface; /** * Class Plugin to add bulks related notification messages to Synchronized Collection + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Plugin { + private const BULK_LOGGING_ACL = "Magento_AsynchronousOperations::system_magento_logging_bulk_operations"; + /** * @var MessageFactory */ @@ -40,11 +47,6 @@ class Plugin */ private $operationDetails; - /** - * @var AccessManager - */ - private $accessManager; - /** * @var BulkNotificationManagement */ @@ -55,22 +57,31 @@ class Plugin */ private $statusMapper; + /** + * @var AuthorizationInterface|mixed|null + */ + private $authorization; + /** * @var Encryptor */ private $encryptor; /** - * Plugin constructor. - * + * @var GetBulksByUserAndTypeInterface|null + */ + private $getBulksByUserAndType; + + /** * @param MessageFactory $messageFactory * @param BulkStatusInterface $bulkStatus * @param BulkNotificationManagement $bulkNotificationManagement * @param UserContextInterface $userContext * @param Details $operationDetails * @param StatusMapper $statusMapper - * @param AccessManager $accessManager - * @param Encryptor $encryptor + * @param AuthorizationInterface|null $authorization + * @param Encryptor|null $encryptor + * @param GetBulksByUserAndTypeInterface|null $getBulksByUserAndType */ public function __construct( MessageFactory $messageFactory, @@ -79,8 +90,9 @@ public function __construct( UserContextInterface $userContext, Details $operationDetails, StatusMapper $statusMapper, - AccessManager $accessManager, - Encryptor $encryptor + ?AuthorizationInterface $authorization = null, + ?Encryptor $encryptor = null, + ?GetBulksByUserAndTypeInterface $getBulksByUserAndType = null ) { $this->messageFactory = $messageFactory; $this->bulkStatus = $bulkStatus; @@ -88,8 +100,10 @@ public function __construct( $this->operationDetails = $operationDetails; $this->bulkNotificationManagement = $bulkNotificationManagement; $this->statusMapper = $statusMapper; - $this->accessManager = $accessManager; - $this->encryptor = $encryptor; + $this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class); + $this->encryptor = $encryptor ?: ObjectManager::getInstance()->get(Encryptor::class); + $this->getBulksByUserAndType = $getBulksByUserAndType + ?: ObjectManager::getInstance()->get(GetBulksByUserAndTypeInterface::class); } /** @@ -100,16 +114,14 @@ public function __construct( * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterToArray( - Synchronized $collection, - $result - ) { - if (!$this->accessManager->isOwnActionsAllowed()) { + public function afterToArray(Synchronized $collection, $result) + { + if (!$this->isAllowed()) { return $result; } - $userId = $this->userContext->getUserId(); - $userType = $this->userContext->getUserType(); - $userBulks = $this->bulkStatus->getBulksByUserAndType($userId, $userType); + $userId = (int) $this->userContext->getUserId(); + $userType = (int) $this->userContext->getUserType(); + $userBulks = $this->getBulksByUserAndType->execute($userId, $userType); $acknowledgedBulks = $this->getAcknowledgedBulksUuid( $this->bulkNotificationManagement->getAcknowledgedBulksByUser($userId) ); @@ -120,13 +132,13 @@ public function afterToArray( $details = $this->operationDetails->getDetails($bulkUuid); $text = $this->getText($details); $bulkStatus = $this->statusMapper->operationStatusToBulkSummaryStatus($bulk->getStatus()); - if ($bulkStatus === \Magento\Framework\Bulk\BulkSummaryInterface::IN_PROGRESS) { + if ($bulkStatus === BulkSummaryInterface::IN_PROGRESS) { $text = __('%1 item(s) are currently being updated.', $details['operations_total']) . $text; } $data = [ 'data' => [ 'text' => __('Task "%1": ', $bulk->getDescription()) . $text, - 'severity' => \Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR, + 'severity' => MessageInterface::SEVERITY_MAJOR, 'identity' => $this->encryptor->hash('bulk' . $bulkUuid, Encryptor::HASH_VERSION_SHA256), 'uuid' => $bulkUuid, 'status' => $bulkStatus, @@ -142,6 +154,7 @@ public function afterToArray( $bulkMessages = array_slice($bulkMessages, 0, 5); $result['items'] = array_merge($bulkMessages, $result['items']); } + return $result; } @@ -187,4 +200,14 @@ private function getAcknowledgedBulksUuid($acknowledgedBulks) } return $acknowledgedBulksArray; } + + /** + * Check if it allowed to see bulk operations. + * + * @return bool + */ + private function isAllowed(): bool + { + return $this->authorization->isAllowed(self::BULK_LOGGING_ACL); + } } diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php index 23d1cb867b7fc..49402644406ad 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/AdminNotification/Plugin.php @@ -7,17 +7,19 @@ namespace Magento\AsynchronousOperations\Ui\Component\AdminNotification; use Magento\AdminNotification\Ui\Component\DataProvider\DataProvider; -use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Framework\AuthorizationInterface; /** * Class Plugin to eliminate Bulk related links in the notification area */ class Plugin { + private const BULK_LOGGING_ACL = "Magento_AsynchronousOperations::system_magento_logging_bulk_operations"; + /** - * @var AccessManager + * @var AuthorizationInterface */ - private $accessManager; + private $authorization; /** * @var bool @@ -25,12 +27,11 @@ class Plugin private $isAllowed; /** - * @param AccessManager $accessManager + * @param AuthorizationInterface $authorization */ - public function __construct( - AccessManager $accessManager - ) { - $this->accessManager = $accessManager; + public function __construct(AuthorizationInterface $authorization) + { + $this->authorization = $authorization; } /** @@ -44,10 +45,20 @@ public function __construct( public function afterGetMeta(DataProvider $dataProvider, $result) { if (!isset($this->isAllowed)) { - $this->isAllowed = $this->accessManager->isOwnActionsAllowed(); + $this->isAllowed = $this->isAllowed(); } $result['columns']['arguments']['data']['config']['isAllowed'] = $this->isAllowed; return $result; } + + /** + * Check if it allowed to see bulk operations. + * + * @return bool + */ + private function isAllowed(): bool + { + return $this->authorization->isAllowed(self::BULK_LOGGING_ACL); + } } diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php index cae5c66ce4b8b..482a4bc3b4420 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/Bulk/DataProvider.php @@ -3,12 +3,15 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + namespace Magento\AsynchronousOperations\Ui\Component\DataProvider\Bulk; -use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\AsynchronousOperations\Model\GetGlobalAllowedUserTypes; use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory; use Magento\Authorization\Model\UserContextInterface; -use Magento\Framework\Api\FilterBuilder; +use Magento\Framework\AuthorizationInterface; use Magento\Ui\DataProvider\AbstractDataProvider; /** @@ -16,16 +19,17 @@ */ class DataProvider extends AbstractDataProvider { + private const BULK_LOGGING_ACL = "Magento_AsynchronousOperations::system_magento_logging_bulk_operations"; /** - * @var AccessManager + * @var AuthorizationInterface */ - private $accessManager; + private $authorization; /** - * @var FilterBuilder + * @var GetGlobalAllowedUserTypes */ - private $filterBuilder; + private $getGlobalAllowedUserTypes; /** * @var UserContextInterface @@ -33,14 +37,12 @@ class DataProvider extends AbstractDataProvider private $userContext; /** - * DataProvider constructor. - * * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $collectionFactory - * @param AccessManager $accessManager - * @param FilterBuilder $filterBuilder + * @param AuthorizationInterface $authorization + * @param GetGlobalAllowedUserTypes $getGlobalAllowedUserTypes * @param UserContextInterface $userContext * @param array $meta * @param array $data @@ -50,16 +52,16 @@ public function __construct( $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, - AccessManager $accessManager, - FilterBuilder $filterBuilder, + AuthorizationInterface $authorization, + GetGlobalAllowedUserTypes $getGlobalAllowedUserTypes, UserContextInterface $userContext, array $meta = [], array $data = [] ) { - $this->filterBuilder = $filterBuilder; - $this->accessManager = $accessManager; - $this->userContext = $userContext; $this->collection = $collectionFactory->create(); + $this->authorization = $authorization; + $this->getGlobalAllowedUserTypes = $getGlobalAllowedUserTypes; + $this->userContext = $userContext; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); } @@ -68,16 +70,17 @@ public function __construct( * * @return array */ - public function getData() + public function getData(): array { - $allowedUserTypes = $this->accessManager->getGlobalAllowedUserTypes(); + $allowedUserTypes = $this->getGlobalAllowedUserTypes->execute(); $connection = $this->getCollection()->getConnection(); + $whereOr = []; - if (count($allowedUserTypes) > 0) { - $whereOr[] = $connection->quoteInto("user_type IN(?)", $allowedUserTypes); + if ($allowedUserTypes) { + $whereOr[] = $connection->quoteInto('user_type IN (?)', $allowedUserTypes); } - if ($this->accessManager->isOwnActionsAllowed()) { + if ($this->isAllowed()) { $whereOr[] = implode( ' AND ', [ @@ -87,9 +90,20 @@ public function getData() ); } - $whereCond = '(' . implode(') OR (', $whereOr) . ')'; - $this->getCollection()->getSelect()->where($whereCond); + $this->getCollection() + ->getSelect() + ->where('(' . implode(') OR (', $whereOr) . ')'); return $this->getCollection()->toArray(); } + + /** + * Check if it allowed to see own bulk operations. + * + * @return bool + */ + private function isAllowed(): bool + { + return $this->authorization->isAllowed(self::BULK_LOGGING_ACL); + } } diff --git a/app/code/Magento/AsynchronousOperations/etc/di.xml b/app/code/Magento/AsynchronousOperations/etc/di.xml index 7b5d8bbd3d6d5..f335c5f570c38 100644 --- a/app/code/Magento/AsynchronousOperations/etc/di.xml +++ b/app/code/Magento/AsynchronousOperations/etc/di.xml @@ -6,6 +6,7 @@ */ --> + diff --git a/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php b/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php index 4057d61957f64..b8506bdc795f0 100644 --- a/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php +++ b/lib/internal/Magento/Framework/Bulk/BulkStatusInterface.php @@ -38,20 +38,10 @@ public function getOperationsCountByBulkIdAndStatus($bulkUuid, $status); * @param int $userId * @return BulkSummaryInterface[] * @since 103.0.0 - * @deprecated 100.3.0 + * @deprecated see \Magento\Framework\Bulk\GetBulksByUserAndTypeInterface */ public function getBulksByUser($userId); - /** - * Get all bulks created by user and user type - * - * @param int $userId - * @param int $userTypeId - * @return BulkSummaryInterface[] - * @since 100.2.0 - */ - public function getBulksByUserAndType($userId, $userTypeId); - /** * Computational status based on statuses of belonging operations * diff --git a/lib/internal/Magento/Framework/Bulk/GetBulksByUserAndTypeInterface.php b/lib/internal/Magento/Framework/Bulk/GetBulksByUserAndTypeInterface.php new file mode 100644 index 0000000000000..4984b23d4fcb1 --- /dev/null +++ b/lib/internal/Magento/Framework/Bulk/GetBulksByUserAndTypeInterface.php @@ -0,0 +1,24 @@ + Date: Thu, 5 Nov 2020 18:29:56 +0200 Subject: [PATCH 4/5] minor changes --- .../Model/BulkNotificationManagement.php | 2 +- .../Model/GetBulksByUserAndType.php | 8 ++++---- .../Message/Collection/Synchronized/Plugin.php | 4 ++-- .../Ui/Component/DataProvider/SearchResult.php | 17 ++++++++++------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php b/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php index d0dd965980100..212c841642b46 100644 --- a/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php +++ b/app/code/Magento/AsynchronousOperations/Model/BulkNotificationManagement.php @@ -10,7 +10,7 @@ use Magento\Framework\EntityManager\MetadataPool; use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory as BulkCollectionFactory; use Magento\Framework\Data\Collection; -use \Magento\Authorization\Model\UserContextInterface; +use Magento\Authorization\Model\UserContextInterface; /** * Class for bulk notification manager diff --git a/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php b/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php index aad98d55623e0..a8b2f086027cb 100644 --- a/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php +++ b/app/code/Magento/AsynchronousOperations/Model/GetBulksByUserAndType.php @@ -48,18 +48,18 @@ class GetBulksByUserAndType implements GetBulksByUserAndTypeInterface private $calculatedStatusSql; /** - * @param ResourceConnection $resourceConnection * @param CalculatedStatusSql $calculatedStatusSql + * @param ResourceConnection $resourceConnection * @param CollectionFactory $bulkCollection */ public function __construct( - ResourceConnection $resourceConnection, CalculatedStatusSql $calculatedStatusSql, + ResourceConnection $resourceConnection, CollectionFactory $bulkCollection ) { - $this->bulkCollectionFactory = $bulkCollection; - $this->resourceConnection = $resourceConnection; $this->calculatedStatusSql = $calculatedStatusSql; + $this->resourceConnection = $resourceConnection; + $this->bulkCollectionFactory = $bulkCollection; } /** diff --git a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php index 7f63eebf1a5ff..1a9ee79c4d587 100644 --- a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php @@ -89,8 +89,8 @@ public function __construct( BulkNotificationManagement $bulkNotificationManagement, UserContextInterface $userContext, Details $operationDetails, + AuthorizationInterface $authorization, StatusMapper $statusMapper, - ?AuthorizationInterface $authorization = null, ?Encryptor $encryptor = null, ?GetBulksByUserAndTypeInterface $getBulksByUserAndType = null ) { @@ -99,8 +99,8 @@ public function __construct( $this->userContext = $userContext; $this->operationDetails = $operationDetails; $this->bulkNotificationManagement = $bulkNotificationManagement; + $this->authorization = $authorization; $this->statusMapper = $statusMapper; - $this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class); $this->encryptor = $encryptor ?: ObjectManager::getInstance()->get(Encryptor::class); $this->getBulksByUserAndType = $getBulksByUserAndType ?: ObjectManager::getInstance()->get(GetBulksByUserAndTypeInterface::class); diff --git a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php index 0a337b620e6ba..68d6512395f58 100644 --- a/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php +++ b/app/code/Magento/AsynchronousOperations/Ui/Component/DataProvider/SearchResult.php @@ -9,6 +9,7 @@ use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory; use Magento\Framework\Event\ManagerInterface as EventManager; use Psr\Log\LoggerInterface as Logger; +use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\Bulk\BulkSummaryInterface; use Magento\AsynchronousOperations\Model\StatusMapper; use Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql; @@ -18,6 +19,11 @@ */ class SearchResult extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult { + /** + * @var UserContextInterface + */ + private $userContext; + /** * @var StatusMapper */ @@ -39,25 +45,27 @@ class SearchResult extends \Magento\Framework\View\Element\UiComponent\DataProvi * @param Logger $logger * @param FetchStrategy $fetchStrategy * @param EventManager $eventManager + * @param UserContextInterface $userContextInterface * @param StatusMapper $statusMapper * @param CalculatedStatusSql $calculatedStatusSql * @param string $mainTable * @param null|string $resourceModel * @param string $identifierName * @SuppressWarnings(PHPMD.ExcessiveParameterList) - * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct( EntityFactory $entityFactory, Logger $logger, FetchStrategy $fetchStrategy, EventManager $eventManager, + UserContextInterface $userContextInterface, StatusMapper $statusMapper, CalculatedStatusSql $calculatedStatusSql, $mainTable = 'magento_bulk', $resourceModel = null, $identifierName = 'uuid' ) { + $this->userContext = $userContextInterface; $this->statusMapper = $statusMapper; $this->calculatedStatusSql = $calculatedStatusSql; parent::__construct( @@ -99,12 +107,7 @@ protected function _afterLoad() } /** - * Add additional field for filter request - * - * @param array|string $field - * @param string|array $condition - * - * @return $this + * @inheritdoc */ public function addFieldToFilter($field, $condition = null) { From 1d9d59163b519bf855fbd5118e2c0fd64d81978e Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Fri, 6 Nov 2020 10:53:02 +0200 Subject: [PATCH 5/5] fix unit tests --- .../Controller/Adminhtml/Bulk/Retry.php | 1 - .../Collection/Synchronized/Plugin.php | 2 +- ...rTest.php => IsAllowedForBulkUuidTest.php} | 31 ++++++---- .../Collection/Synchronized/PluginTest.php | 61 +++++++++++++------ .../AdminNotification/PluginTest.php | 24 +++++--- 5 files changed, 76 insertions(+), 43 deletions(-) rename app/code/Magento/AsynchronousOperations/Test/Unit/Model/{AccessManagerTest.php => IsAllowedForBulkUuidTest.php} (74%) diff --git a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php index 347eb0e909cb4..15b93d98dd732 100644 --- a/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php +++ b/app/code/Magento/AsynchronousOperations/Controller/Adminhtml/Bulk/Retry.php @@ -14,7 +14,6 @@ use Magento\AsynchronousOperations\Model\AccessValidator; use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\App\ObjectManager; -use Magento\Framework\Bulk\GetBulksByUserAndTypeInterface; use Magento\Framework\Controller\ResultFactory; /** diff --git a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php index 1a9ee79c4d587..c9bc2e692da59 100644 --- a/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php +++ b/app/code/Magento/AsynchronousOperations/Model/ResourceModel/System/Message/Collection/Synchronized/Plugin.php @@ -78,8 +78,8 @@ class Plugin * @param BulkNotificationManagement $bulkNotificationManagement * @param UserContextInterface $userContext * @param Details $operationDetails + * @param AuthorizationInterface $authorization * @param StatusMapper $statusMapper - * @param AuthorizationInterface|null $authorization * @param Encryptor|null $encryptor * @param GetBulksByUserAndTypeInterface|null $getBulksByUserAndType */ diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/IsAllowedForBulkUuidTest.php similarity index 74% rename from app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php rename to app/code/Magento/AsynchronousOperations/Test/Unit/Model/IsAllowedForBulkUuidTest.php index 1e5f28ed4841c..37f610e752be0 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/AccessManagerTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/IsAllowedForBulkUuidTest.php @@ -4,43 +4,48 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\AsynchronousOperations\Test\Unit\Model; -use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\AsynchronousOperations\Model\GetGlobalAllowedUserTypes; +use Magento\AsynchronousOperations\Model\IsAllowedForBulkUuid; use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory; use Magento\Authorization\Model\UserContextInterface; -use Magento\Framework\AuthorizationInterface; use Magento\Framework\EntityManager\EntityManager; use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -class AccessManagerTest extends TestCase +/** + * Test for \Magento\AsynchronousOperations\Model\IsAllowedForBulkUuid. + */ +class IsAllowedForBulkUuidTest extends TestCase { /** - * @var AccessManager + * @var IsAllowedForBulkUuid */ private $model; /** - * @var MockObject + * @var UserContextInterface|MockObject */ private $userContextMock; /** - * @var MockObject + * @var EntityManager|MockObject */ private $entityManagerMock; /** - * @var MockObject + * @var BulkSummaryInterfaceFactory|MockObject */ private $bulkSummaryFactoryMock; /** - * @var MockObject + * @var GetGlobalAllowedUserTypes|MockObject */ - private $authorizationMock; + private $getGlobalAllowedUserTypes; /** * @inheritDoc @@ -53,13 +58,13 @@ protected function setUp(): void BulkSummaryInterfaceFactory::class, ['create'] ); - $this->authorizationMock = $this->createMock(AuthorizationInterface::class); + $this->getGlobalAllowedUserTypes = $this->createMock(GetGlobalAllowedUserTypes::class); - $this->model = new AccessManager( + $this->model = new IsAllowedForBulkUuid( $this->userContextMock, $this->entityManagerMock, $this->bulkSummaryFactoryMock, - $this->authorizationMock + $this->getGlobalAllowedUserTypes ); } @@ -90,7 +95,7 @@ public function testIsAllowedForBulkUuid(int $bulkUserId, bool $expectedResult): ->method('getUserId') ->willReturn($adminId); - $this->assertEquals($this->model->isAllowedForBulkUuid($uuid), $expectedResult); + $this->assertEquals($this->model->execute($uuid), $expectedResult); } /** diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php index f52aaa98d7845..ce2703856c31e 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Model/ResourceModel/System/Message/Collection/Synchronized/PluginTest.php @@ -11,7 +11,6 @@ use Magento\AdminNotification\Model\System\Message; use Magento\AdminNotification\Model\System\MessageFactory; use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface; -use Magento\AsynchronousOperations\Model\AccessManager; use Magento\AsynchronousOperations\Model\BulkNotificationManagement; use Magento\AsynchronousOperations\Model\BulkSummary; use Magento\AsynchronousOperations\Model\Operation\Details; @@ -20,6 +19,7 @@ use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\AuthorizationInterface; use Magento\Framework\Bulk\BulkStatusInterface; +use Magento\Framework\Bulk\GetBulksByUserAndTypeInterface; use Magento\Framework\Encryption\Encryptor; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -80,15 +80,18 @@ class PluginTest extends TestCase private $statusMapper; /** - * @var AccessManager|MockObject + * @var GetBulksByUserAndTypeInterface|MockObject */ - private $accessManager; + private $getBulksByUserAndTypeMock; /** * @var Encryptor|MockObject */ private $encryptor; + /** + * @inheritDoc + */ protected function setUp(): void { $this->messagefactoryMock = $this->createPartialMock( @@ -97,33 +100,40 @@ protected function setUp(): void ); $this->bulkStatusMock = $this->getMockForAbstractClass(BulkStatusInterface::class); + $this->bulkNotificationMock = $this->createMock(BulkNotificationManagement::class); $this->userContextMock = $this->getMockForAbstractClass(UserContextInterface::class); $this->operationsDetailsMock = $this->createMock(Details::class); $this->authorizationMock = $this->getMockForAbstractClass(AuthorizationInterface::class); $this->messageMock = $this->createMock(Message::class); $this->collectionMock = $this->createMock(Synchronized::class); - $this->bulkNotificationMock = $this->createMock(BulkNotificationManagement::class); $this->statusMapper = $this->createMock(StatusMapper::class); - $this->accessManager = $this->createMock(AccessManager::class); $this->encryptor = $this->createMock(Encryptor::class); + $this->getBulksByUserAndTypeMock = $this->createMock(GetBulksByUserAndTypeInterface::class); + $this->plugin = new Plugin( $this->messagefactoryMock, $this->bulkStatusMock, $this->bulkNotificationMock, $this->userContextMock, $this->operationsDetailsMock, + $this->authorizationMock, $this->statusMapper, - $this->accessManager, - $this->encryptor + $this->encryptor, + $this->getBulksByUserAndTypeMock, ); } - public function testAfterToArrayIfNotAllowed() + /** + * After toArray when not allowed + * + * @return void + */ + public function testAfterToArrayIfNotAllowed(): void { $result = []; - $this->accessManager + $this->authorizationMock ->expects($this->once()) - ->method('isOwnActionsAllowed') + ->method('isAllowed') ->willReturn(false); $this->assertEquals($result, $this->plugin->afterToArray($this->collectionMock, $result)); } @@ -131,8 +141,9 @@ public function testAfterToArrayIfNotAllowed() /** * @param array $operationDetails * @dataProvider afterToDataProvider + * @return void */ - public function testAfterTo($operationDetails) + public function testAfterTo(array $operationDetails): void { $bulkMock = $this->getMockBuilder(BulkSummary::class) ->addMethods(['getStatus']) @@ -152,29 +163,39 @@ public function testAfterTo($operationDetails) ->method('getDetails') ->with($bulkUuid) ->willReturn($operationDetails); - $bulkMock->expects($this->once())->method('getDescription')->willReturn('Bulk Description'); - $this->messagefactoryMock->expects($this->once())->method('create')->willReturn($this->messageMock); - $this->messageMock->expects($this->once())->method('toArray')->willReturn($bulkArray); - $this->accessManager + $bulkMock->expects($this->once()) + ->method('getDescription') + ->willReturn('Bulk Description'); + $this->messagefactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->messageMock); + $this->messageMock->expects($this->once()) + ->method('toArray') + ->willReturn($bulkArray); + $this->authorizationMock ->expects($this->once()) - ->method('isOwnActionsAllowed') + ->method('isAllowed') ->willReturn(true); - $this->userContextMock->expects($this->once())->method('getUserId')->willReturn($userId); + $this->userContextMock->expects($this->once()) + ->method('getUserId') + ->willReturn($userId); $this->bulkNotificationMock ->expects($this->once()) ->method('getAcknowledgedBulksByUser') ->with($userId) ->willReturn([]); - $this->statusMapper->expects($this->once())->method('operationStatusToBulkSummaryStatus'); - $this->bulkStatusMock->expects($this->once())->method('getBulksByUserAndType')->willReturn($userBulks); + $this->getBulksByUserAndTypeMock->expects($this->once()) + ->method('execute') + ->willReturn($userBulks); $result2 = $this->plugin->afterToArray($this->collectionMock, $result); + $this->assertEquals(2, $result2['totalRecords']); } /** * @return array */ - public function afterToDataProvider() + public function afterToDataProvider(): array { return [ [ diff --git a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php index 86bc4955e79c8..84be15403e737 100644 --- a/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php +++ b/app/code/Magento/AsynchronousOperations/Test/Unit/Ui/Component/AdminNotification/PluginTest.php @@ -9,7 +9,7 @@ use Magento\AdminNotification\Ui\Component\DataProvider\DataProvider; use Magento\AsynchronousOperations\Ui\Component\AdminNotification\Plugin; -use Magento\AsynchronousOperations\Model\AccessManager; +use Magento\Framework\AuthorizationInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -21,17 +21,25 @@ class PluginTest extends TestCase private $plugin; /** - * @var AccessManager|MockObject + * @var AuthorizationInterface|MockObject */ - private $accessMangerMock; + private $authorizationMock; + /** + * @inheritDoc + */ protected function setUp(): void { - $this->accessMangerMock = $this->createMock(AccessManager::class); - $this->plugin = new Plugin($this->accessMangerMock); + $this->authorizationMock = $this->getMockForAbstractClass(AuthorizationInterface::class); + $this->plugin = new Plugin($this->authorizationMock); } - public function testAfterGetMeta() + /** + * After getMeta test + * + * @return void + */ + public function testAfterGetMeta(): void { $result = []; $expectedResult = [ @@ -46,8 +54,8 @@ public function testAfterGetMeta() ] ]; $dataProviderMock = $this->createMock(DataProvider::class); - $this->accessMangerMock->expects($this->once()) - ->method('isOwnActionsAllowed') + $this->authorizationMock->expects($this->once()) + ->method('isAllowed') ->willReturn(true); $this->assertEquals($expectedResult, $this->plugin->afterGetMeta($dataProviderMock, $result));