Skip to content

Commit 239314d

Browse files
committed
Improve button visibility per allowed actions for user
1 parent 989139f commit 239314d

File tree

14 files changed

+72
-20
lines changed

14 files changed

+72
-20
lines changed

app/code/Magento/MediaGalleryUi/Ui/Component/Control/CreateFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434
/**
3535
* @inheritdoc
3636
*/
37-
public function getButtonData()
37+
public function getButtonData(): array
3838
{
3939
$buttonData = [
4040
'label' => __('Create Folder'),

app/code/Magento/MediaGalleryUi/Ui/Component/Control/DeleteAssets.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
}
3333

3434
/**
35-
* @return array
35+
* @inheritdoc
3636
*/
3737
public function getButtonData(): array
3838
{

app/code/Magento/MediaGalleryUi/Ui/Component/Control/DeleteFolder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function __construct(
3232
}
3333

3434
/**
35-
* @return array
35+
* @inheritdoc
3636
*/
37-
public function getButtonData()
37+
public function getButtonData(): array
3838
{
3939
$buttonData = [
4040
'label' => __('Delete Folder'),

app/code/Magento/MediaGalleryUi/Ui/Component/Control/InsertAsstes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function __construct(
3434
/**
3535
* @inheritdoc
3636
*/
37-
public function getButtonData()
37+
public function getButtonData(): array
3838
{
3939
$buttonData = [
4040
'label' => __('Add Selected'),
41-
'on_click' => 'return false;");',
41+
'on_click' => 'return false;',
4242
'class' => 'action-primary no-display media-gallery-add-selected',
4343
'sort_order' => 110,
4444
];

app/code/Magento/MediaGalleryUi/Ui/Component/Control/UploadAssets.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function __construct(
3232
}
3333

3434
/**
35-
* @return array
35+
* @inheritdoc
3636
*/
37-
public function getButtonData()
37+
public function getButtonData(): array
3838
{
3939
$buttonData = [
4040
'label' => __('Upload Image'),

app/code/Magento/MediaGalleryUi/Ui/Component/Listing/Columns/Url.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
use Magento\Framework\View\Element\UiComponentFactory;
1616
use Magento\Store\Model\StoreManagerInterface;
1717
use Magento\Ui\Component\Listing\Columns\Column;
18+
use Magento\Framework\AuthorizationInterface;
1819

1920
/**
2021
* Overlay column
2122
*/
2223
class Url extends Column
2324
{
25+
private const ACL_IMAGE_ACTIONS = [
26+
'insert_assets' => 'Magento_MediaGallery::insert_assets',
27+
'delete_assets' => 'Magento_MediaGallery::delete_assets'
28+
];
29+
2430
/**
2531
* @var StoreManagerInterface
2632
*/
@@ -41,13 +47,19 @@ class Url extends Column
4147
*/
4248
private $storage;
4349

50+
/**
51+
* @var AuthorizationInterface
52+
*/
53+
private $authorization;
54+
4455
/**
4556
* @param ContextInterface $context
4657
* @param UiComponentFactory $uiComponentFactory
4758
* @param StoreManagerInterface $storeManager
4859
* @param UrlInterface $urlInterface
4960
* @param Images $images
5061
* @param Storage $storage
62+
* @param AuthorizationInterface $authorization
5163
* @param array $components
5264
* @param array $data
5365
*/
@@ -58,6 +70,7 @@ public function __construct(
5870
UrlInterface $urlInterface,
5971
Images $images,
6072
Storage $storage,
73+
AuthorizationInterface $authorization,
6174
array $components = [],
6275
array $data = []
6376
) {
@@ -66,6 +79,7 @@ public function __construct(
6679
$this->urlInterface = $urlInterface;
6780
$this->images = $images;
6881
$this->storage = $storage;
82+
$this->authorization = $authorization;
6983
}
7084

7185
/**
@@ -98,13 +112,29 @@ public function prepare(): void
98112
array_replace_recursive(
99113
(array)$this->getData('config'),
100114
[
115+
'allowedActions' => $this->getAllowedActions(),
101116
'onInsertUrl' => $this->urlInterface->getUrl('cms/wysiwyg_images/oninsert'),
102117
'storeId' => $this->storeManager->getStore()->getId()
103118
]
104119
)
105120
);
106121
}
107122

123+
/**
124+
* Return allowed actions for media gallery image
125+
*/
126+
private function getAllowedActions(): array
127+
{
128+
$allowedActions = [];
129+
foreach (self::ACL_IMAGE_ACTIONS as $key => $action) {
130+
if ($this->authorization->isAllowed($action)) {
131+
$allowedActions[] = $key;
132+
}
133+
}
134+
135+
return $allowedActions;
136+
}
137+
108138
/**
109139
* Get URL for the provided media asset path
110140
*

app/code/Magento/MediaGalleryUi/view/adminhtml/templates/image_details.phtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,3 @@ use Magento\Framework\Escaper;
105105
}
106106
}
107107
</script>
108-
109-

app/code/Magento/MediaGalleryUi/view/adminhtml/templates/image_details_standalone.phtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,3 @@ use Magento\Backend\Block\Template;
9797
}
9898
}
9999
</script>
100-
101-

app/code/Magento/MediaGalleryUi/view/adminhtml/web/css/source/_module.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999

100100
.media-gallery-container {
101101

102+
.action-disabled {
103+
opacity: 0.5;
104+
}
102105
.masonry-image-grid .no-data-message-container,
103106
.masonry-image-grid .error-message-container {
104107
left: 50%;

app/code/Magento/MediaGalleryUi/view/adminhtml/web/js/directory/directories.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ define([
1919

2020
return Component.extend({
2121
defaults: {
22+
allowedActions: [],
2223
directoryTreeSelector: '#media-gallery-directory-tree',
2324
deleteButtonSelector: '#delete_folder',
2425
createFolderButtonSelector: '#create_folder',
@@ -179,7 +180,7 @@ define([
179180
* @param {String} folderId
180181
*/
181182
setActive: function (folderId) {
182-
if (!$.inArray('delete_folder', this.allowedActions)) {
183+
if (!this.allowedActions.includes('delete_folder')) {
183184
return;
184185
}
185186

app/code/Magento/MediaGalleryUi/view/adminhtml/web/js/grid/columns/image.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ define([
1717
addSelectedBtnSelector: '#add_selected',
1818
deleteSelectedBtnSelector: '#delete_selected',
1919
selected: null,
20+
allowedActions: [],
2021
fields: {
2122
id: 'id',
2223
url: 'url',
@@ -39,7 +40,8 @@ define([
3940
{
4041
component: 'Magento_MediaGalleryUi/js/grid/columns/image/actions',
4142
name: '${ $.name }_actions',
42-
imageModelName: '${ $.name }'
43+
imageModelName: '${ $.name }',
44+
allowedActions: '${ $.allowedActions }'
4345
}
4446
]
4547
},
@@ -222,8 +224,15 @@ define([
222224
toggleAddSelectedButton: function () {
223225
if (this.selected() === null) {
224226
this.hideAddSelectedAndDeleteButon();
225-
} else {
227+
228+
return;
229+
}
230+
231+
if (this.allowedActions.includes('insert_assets')) {
226232
$(this.addSelectedBtnSelector).removeClass('no-display');
233+
}
234+
235+
if (this.allowedActions.includes('delete_assets')) {
227236
$(this.deleteSelectedBtnSelector).removeClass('no-display');
228237
}
229238
},

app/code/Magento/MediaGalleryUi/view/adminhtml/web/js/grid/columns/image/actions.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ define([
88
'uiComponent',
99
'Magento_MediaGalleryUi/js/action/deleteImageWithDetailConfirmation',
1010
'Magento_MediaGalleryUi/js/grid/columns/image/insertImageAction',
11-
'mage/translate'
11+
'mage/translate',
12+
'Magento_Ui/js/lib/view/utils/async'
1213
], function ($, _, Component, deleteImageWithDetailConfirmation, image, $t) {
1314
'use strict';
1415

@@ -17,20 +18,24 @@ define([
1718
template: 'Magento_MediaGalleryUi/grid/columns/image/actions',
1819
mediaGalleryImageDetailsName: 'mediaGalleryImageDetails',
1920
mediaGalleryEditDetailsName: 'mediaGalleryEditDetails',
21+
allowedActions: [],
2022
actionsList: [
2123
{
2224
name: 'image-details',
2325
title: $t('View Details'),
26+
classes: 'action-menu-item',
2427
handler: 'viewImageDetails'
2528
},
2629
{
2730
name: 'edit',
2831
title: $t('Edit'),
32+
classes: 'action-menu-item',
2933
handler: 'editImageDetails'
3034
},
3135
{
3236
name: 'delete',
3337
title: $t('Delete'),
38+
classes: 'action-menu-item media-gallery-delete-assets',
3439
handler: 'deleteImageAction'
3540
}
3641
],
@@ -50,6 +55,14 @@ define([
5055
this._super();
5156
this.initEvents();
5257

58+
if (!this.allowedActions.includes('delete_assets')) {
59+
$.async('.media-gallery-delete-assets', function () {
60+
$('.media-gallery-delete-assets').attr('click', '#')
61+
.unbind('click')
62+
.addClass('action-disabled');
63+
});
64+
}
65+
5366
return this;
5467
},
5568

app/code/Magento/MediaGalleryUi/view/adminhtml/web/js/grid/massaction/massactions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ define([
107107
* If images records less than one, disable "delete images" button
108108
*/
109109
checkButtonVisibility: function () {
110-
if (!$.inArray('delete_assets', this.allowedActions)) {
110+
if (!this.allowedActions.includes('delete_assets')) {
111111
return;
112112
}
113113

app/code/Magento/MediaGalleryUi/view/adminhtml/web/template/grid/columns/image/actions.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
<each args="{ data: actionsList, as: 'action' }">
99
<li>
10-
<a class="action-menu-item" href="" text="action.title"
10+
<a href="#" text="action.title"
1111
click="$parent[action.handler].bind($parent, $row())"
12-
attr="{'data-action': 'item-' + action.name}">
12+
attr="{'data-action': 'item-' + action.name, class: action.classes}">
1313
</a>
1414
</li>
15-
</each>
15+
</each>

0 commit comments

Comments
 (0)