Skip to content

Commit

Permalink
Improve buttons visibility per user permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Aug 28, 2020
1 parent eaa20f2 commit 8cdcefd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
27 changes: 26 additions & 1 deletion app/code/Magento/MediaGalleryUi/Ui/Component/DirectoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,44 @@
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Container;
use Magento\Framework\AuthorizationInterface;

/**
* Directories tree component
*/
class DirectoryTree extends Container
{
private const ACL_DELETE_FOLDER = 'Magento_MediaGallery::delete_folder';

/**
* @var UrlInterface
*/
private $url;

/**
* @var AuthorizationInterface
*/
private $authorization;

/**
* Constructor
*
* @param ContextInterface $context
* @param UrlInterface $url
* @param AuthorizationInterface $authorization
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UrlInterface $url,
AuthorizationInterface $authorization,
array $components = [],
array $data = []
) {
parent::__construct($context, $components, $data);
$this->url = $url;
$this->authorization = $authorization;
}

/**
Expand All @@ -50,12 +61,26 @@ public function prepare(): void
array_replace_recursive(
(array) $this->getData('config'),
[
'allowedActions' => [],
'allowedActions' => $this->getAllowedActions(),
'getDirectoryTreeUrl' => $this->url->getUrl('media_gallery/directories/gettree'),
'deleteDirectoryUrl' => $this->url->getUrl('media_gallery/directories/delete'),
'createDirectoryUrl' => $this->url->getUrl('media_gallery/directories/create')
]
)
);
}

/**
* Return allowed actions for media gallery
*/
private function getAllowedActions(): array
{
$allowedActions = [];

if ($this->authorization->isAllowed(self::ACL_DELETE_FOLDER)) {
$allowedActions[] = 'delete_folder';
}

return $allowedActions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryUi\Ui\Component\Listing\Massactions;

use Magento\Ui\Component\Container;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\AuthorizationInterface;

/**
* Massaction comntainer
*/
class Massaction extends Container
{
private const ACL_IMAGE_ACTIONS = [
'delete_assets' => 'Magento_MediaGallery::delete_assets'
];

/**
* @var AuthorizationInterface
*/
private $authorization;

/**
* Constructor
*
* @param ContextInterface $context
* @param AuthorizationInterface $authorization
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
AuthorizationInterface $authorization,
array $components = [],
array $data = []
) {
parent::__construct($context, $components, $data);
$this->authorization = $authorization;
}

/**
* @inheritdoc
*/
public function prepare(): void
{
parent::prepare();
$this->setData(
'config',
array_replace_recursive(
(array)$this->getData('config'),
[
'allowedActions' => $this->getAllowedActions()
]
)
);
}

/**
* Return allowed actions for media gallery
*/
private function getAllowedActions(): array
{
$allowedActions = [];
foreach (self::ACL_IMAGE_ACTIONS as $key => $action) {
if ($this->authorization->isAllowed($action)) {
$allowedActions[] = $key;
}
}

return $allowedActions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
<container name="media_gallery_massactions"
displayArea="sorting"
sortOrder="10"
class="Magento\MediaGalleryUi\Ui\Component\Listing\Massactions\Massaction"
component="Magento_MediaGalleryUi/js/grid/massaction/massactions"
template="Magento_MediaGalleryUi/grid/massactions/count" >
<argument name="data" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<container name="media_gallery_massactions"
displayArea="sorting"
sortOrder="10"
class="Magento\MediaGalleryUi\Ui\Component\Listing\Massactions\Massaction"
component="Magento_MediaGalleryUi/js/grid/massaction/massactions"
template="Magento_MediaGalleryUi/grid/massactions/count" >
<argument name="data" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define([

return Component.extend({
defaults: {
allowedActions: [],
filterChipsProvider: 'componentType = filters, ns = ${ $.ns }',
directoryTreeSelector: '#media-gallery-directory-tree',
getDirectoryTreeUrl: 'media_gallery/directories/gettree',
Expand All @@ -32,7 +33,8 @@ define([
},
viewConfig: [{
component: 'Magento_MediaGalleryUi/js/directory/directories',
name: '${ $.name }_directories'
name: '${ $.name }_directories',
allowedActions: '${ $.allowedActions }'
}]
},

Expand Down

0 comments on commit 8cdcefd

Please sign in to comment.