|
| 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\Authorization\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\Authorization\Model\Acl\Role\Group; |
| 11 | +use Magento\Authorization\Model\ResourceModel\Role as RoleResource; |
| 12 | +use Magento\Authorization\Model\UserContextInterface; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\SharedCatalog\Model\SharedCatalogFactory; |
| 15 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 16 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 17 | +use Magento\Authorization\Model\RoleFactory; |
| 18 | +use Magento\Authorization\Model\RulesFactory; |
| 19 | +use Magento\User\Model\UserFactory; |
| 20 | + |
| 21 | +/** |
| 22 | + * Creating a new admin role |
| 23 | + */ |
| 24 | +class Role implements RevertibleDataFixtureInterface |
| 25 | +{ |
| 26 | + private const DEFAULT_DATA = [ |
| 27 | + 'role_name' => 'Role Name %uniqid%', |
| 28 | + 'role_type' => Group::ROLE_TYPE, |
| 29 | + 'user_id' => 0, |
| 30 | + 'user_type' => UserContextInterface::USER_TYPE_ADMIN |
| 31 | + ]; |
| 32 | + |
| 33 | + private const DEFAULT_DATA_RULES = [ |
| 34 | + 'id' => null, |
| 35 | + 'role_id' => null, |
| 36 | + 'resources' => ['Magento_Backend::all'] |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var RoleFactory |
| 41 | + */ |
| 42 | + private $roleFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ProcessorInterface |
| 46 | + */ |
| 47 | + private $dataProcessor; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var RoleResource |
| 51 | + */ |
| 52 | + private $roleResourceModel; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var RulesFactory |
| 56 | + */ |
| 57 | + private $rulesFactory; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param RoleFactory $roleFactory |
| 61 | + * @param RoleResource $roleResourceModel |
| 62 | + * @param RulesFactory $rulesFactory |
| 63 | + * @param ProcessorInterface $dataProcessor |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + RoleFactory $roleFactory, |
| 67 | + RoleResource $roleResourceModel, |
| 68 | + RulesFactory $rulesFactory, |
| 69 | + ProcessorInterface $dataProcessor |
| 70 | + ) { |
| 71 | + $this->roleFactory = $roleFactory; |
| 72 | + $this->roleResourceModel = $roleResourceModel; |
| 73 | + $this->rulesFactory = $rulesFactory; |
| 74 | + $this->dataProcessor = $dataProcessor; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritdoc |
| 79 | + */ |
| 80 | + public function apply(array $data = []): ?DataObject |
| 81 | + { |
| 82 | + $role = $this->roleFactory->create(); |
| 83 | + $role->setData($this->prepareData(array_diff_key($data, self::DEFAULT_DATA_RULES))); |
| 84 | + $this->roleResourceModel->save($role); |
| 85 | + |
| 86 | + $rules = $this->rulesFactory->create(); |
| 87 | + $rules->setRoleId($role->getId() ?? null); |
| 88 | + $rules->setResources($data['resources'] ?? self::DEFAULT_DATA_RULES['resources']); |
| 89 | + $rules->saveRel(); |
| 90 | + |
| 91 | + return $role; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @inheritdoc |
| 96 | + */ |
| 97 | + public function revert(DataObject $data): void |
| 98 | + { |
| 99 | + $role = $this->roleFactory->create(); |
| 100 | + $role->load($data->getId()); |
| 101 | + |
| 102 | + if ($role->getId() !== null) { |
| 103 | + $role->delete(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Prepare admin role data |
| 109 | + * |
| 110 | + * @param array $data |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + private function prepareData(array $data): array |
| 114 | + { |
| 115 | + $data = array_merge(self::DEFAULT_DATA, $data); |
| 116 | + return $this->dataProcessor->process($this, $data); |
| 117 | + } |
| 118 | +} |
0 commit comments