|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaGalleryUi\Setup\Patch\Data; |
| 9 | + |
| 10 | +use Magento\Framework\Setup\Patch\PatchVersionInterface; |
| 11 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 12 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Patch is mechanism, that allows to do atomic upgrade data changes |
| 16 | + */ |
| 17 | +class AddMediaGalleryPermissions implements |
| 18 | + DataPatchInterface, |
| 19 | + PatchVersionInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ModuleDataSetupInterface $moduleDataSetup |
| 23 | + */ |
| 24 | + private $moduleDataSetup; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 28 | + */ |
| 29 | + public function __construct(ModuleDataSetupInterface $moduleDataSetup) |
| 30 | + { |
| 31 | + $this->moduleDataSetup = $moduleDataSetup; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Add child resources permissions for user roles with Magento_Cms::media_gallery permission |
| 36 | + */ |
| 37 | + public function apply(): void |
| 38 | + { |
| 39 | + $tableName = $this->moduleDataSetup->getTable('authorization_rule'); |
| 40 | + $connection = $this->moduleDataSetup->getConnection(); |
| 41 | + |
| 42 | + if (!$tableName) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $select = $connection->select() |
| 47 | + ->from($tableName, ['role_id']) |
| 48 | + ->where('resource_id = "Magento_Cms::media_gallery"'); |
| 49 | + |
| 50 | + $connection->insertMultiple($tableName, $this->getInsertData($connection->fetchCol($select))); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retrieve data to insert to authorization_rule table based on role ids |
| 55 | + * |
| 56 | + * @param array $roleIds |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + private function getInsertData(array $roleIds): array |
| 60 | + { |
| 61 | + $newResources = [ |
| 62 | + 'Magento_MediaGalleryUiApi::insert_assets', |
| 63 | + 'Magento_MediaGalleryUiApi::upload_assets', |
| 64 | + 'Magento_MediaGalleryUiApi::edit_assets', |
| 65 | + 'Magento_MediaGalleryUiApi::delete_assets', |
| 66 | + 'Magento_MediaGalleryUiApi::create_folder', |
| 67 | + 'Magento_MediaGalleryUiApi::delete_folder' |
| 68 | + ]; |
| 69 | + |
| 70 | + $data = []; |
| 71 | + |
| 72 | + foreach ($roleIds as $roleId) { |
| 73 | + foreach ($newResources as $resourceId) { |
| 74 | + $data[] = [ |
| 75 | + 'role_id' => $roleId, |
| 76 | + 'resource_id' => $resourceId, |
| 77 | + 'permission' => 'allow' |
| 78 | + ]; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @inheritdoc |
| 85 | + */ |
| 86 | + public function getAliases() |
| 87 | + { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @inheritdoc |
| 93 | + */ |
| 94 | + public static function getDependencies() |
| 95 | + { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @inheritdoc |
| 101 | + */ |
| 102 | + public static function getVersion() |
| 103 | + { |
| 104 | + return '2.4.2'; |
| 105 | + } |
| 106 | +} |
0 commit comments