Skip to content

Commit eb348e2

Browse files
committed
Merge remote-tracking branch 'origin/CABPI-465' into gl_pr_arrows_sep6_2022
2 parents f26ae80 + 7b97e2e commit eb348e2

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

app/code/Magento/AdminAdobeIms/Console/Command/AdminAdobeImsEnableCommand.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
use Magento\AdminAdobeIms\Service\ImsConfig;
1212
use Magento\AdminAdobeIms\Service\UpdateTokensService;
1313
use Magento\AdobeImsApi\Api\AuthorizationInterface;
14+
use Magento\Authorization\Model\Acl\Role\Group;
15+
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory;
16+
use Magento\Authorization\Model\Role;
17+
use Magento\Authorization\Model\UserContextInterface;
1418
use Magento\Framework\App\Cache\Type\Config;
1519
use Magento\Framework\App\Cache\TypeListInterface;
20+
use Magento\Framework\App\ObjectManager;
1621
use Magento\Framework\Console\Cli;
1722
use Magento\Framework\Exception\InvalidArgumentException;
1823
use Magento\Framework\Exception\LocalizedException;
@@ -67,6 +72,16 @@ class AdminAdobeImsEnableCommand extends Command
6772
*/
6873
private UpdateTokensService $updateTokensService;
6974

75+
/**
76+
* @var Role
77+
*/
78+
private Role $role;
79+
80+
/**
81+
* @var CollectionFactory
82+
*/
83+
private CollectionFactory $roleCollection;
84+
7085
/**
7186
* @var AuthorizationInterface
7287
*/
@@ -78,20 +93,26 @@ class AdminAdobeImsEnableCommand extends Command
7893
* @param TypeListInterface $cacheTypeList
7994
* @param UpdateTokensService $updateTokensService
8095
* @param AuthorizationInterface $authorization
96+
* @param Role|null $role
97+
* @param CollectionFactory|null $roleCollection
8198
*/
8299
public function __construct(
83100
ImsConfig $adminImsConfig,
84101
ImsCommandOptionService $imsCommandOptionService,
85102
TypeListInterface $cacheTypeList,
86103
UpdateTokensService $updateTokensService,
87-
AuthorizationInterface $authorization
104+
AuthorizationInterface $authorization,
105+
Role $role = null,
106+
CollectionFactory $roleCollection = null
88107
) {
89108
parent::__construct();
90109
$this->adminImsConfig = $adminImsConfig;
91110
$this->imsCommandOptionService = $imsCommandOptionService;
92111
$this->cacheTypeList = $cacheTypeList;
93112
$this->updateTokensService = $updateTokensService;
94113
$this->authorization = $authorization;
114+
$this->role = $role ?: ObjectManager::getInstance()->get(Role::class);
115+
$this->roleCollection = $roleCollection ?: ObjectManager::getInstance()->get(CollectionFactory::class);
95116

96117
$this->setName('admin:adobe-ims:enable')
97118
->setDescription('Enable Adobe IMS Module.')
@@ -163,6 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
163184
if ($clientId && $clientSecret && $organizationId && $isTwoFactorAuthEnabled) {
164185
$enabled = $this->enableModule($clientId, $clientSecret, $organizationId, $isTwoFactorAuthEnabled);
165186
if ($enabled) {
187+
$this->saveImsAuthorizationRole();
166188
$output->writeln(__('Admin Adobe IMS integration is enabled'));
167189
return Cli::RETURN_SUCCESS;
168190
}
@@ -181,6 +203,27 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
181203
}
182204
}
183205

206+
/**
207+
* Save new Adobe IMS role
208+
*
209+
* @return bool
210+
* @throws \Exception
211+
*/
212+
private function saveImsAuthorizationRole(): bool
213+
{
214+
$roleCollection = $this->roleCollection->create()->addFieldToFilter('role_name', 'Adobe Ims');
215+
if (!$roleCollection->getSize()) {
216+
$this->role->setRoleName('Adobe Ims')
217+
->setUserType((string)UserContextInterface::USER_TYPE_ADMIN)
218+
->setUserId(0)
219+
->setRoleType(Group::ROLE_TYPE)
220+
->setParentId(0)
221+
->save();
222+
}
223+
224+
return true;
225+
}
226+
184227
/**
185228
* Enable Admin Adobe IMS Module when testConnection was successfully
186229
*

app/code/Magento/AdminAdobeIms/Test/Unit/Command/AdminAdobeImsEnableCommandTest.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99

1010
use Exception;
1111
use Magento\AdminAdobeIms\Console\Command\AdminAdobeImsEnableCommand;
12-
use Magento\AdminAdobeIms\Service\UpdateTokensService;
1312
use Magento\AdminAdobeIms\Service\ImsCommandOptionService;
1413
use Magento\AdminAdobeIms\Service\ImsConfig;
14+
use Magento\AdminAdobeIms\Service\UpdateTokensService;
1515
use Magento\AdobeImsApi\Api\AuthorizationInterface;
16+
use Magento\Authorization\Model\ResourceModel\Role\Collection as RoleCollection;
17+
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory;
18+
use Magento\Authorization\Model\Role;
1619
use Magento\Framework\App\Cache\Type\Config;
1720
use Magento\Framework\App\Cache\TypeListInterface;
1821
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
@@ -63,6 +66,16 @@ class AdminAdobeImsEnableCommandTest extends TestCase
6366
*/
6467
private $questionHelperMock;
6568

69+
/**
70+
* @var Role
71+
*/
72+
private $role;
73+
74+
/**
75+
* @var CollectionFactory
76+
*/
77+
private $roleCollection;
78+
6679
/**
6780
* @var AdminAdobeImsEnableCommand
6881
*/
@@ -77,6 +90,28 @@ protected function setUp(): void
7790
$this->imsCommandOptionService = $this->createMock(ImsCommandOptionService::class);
7891
$this->typeListInterface = $this->createMock(TypeListInterface::class);
7992
$this->updateTokensService = $this->createMock(UpdateTokensService::class);
93+
$roleCollectionMock = $this->createPartialMock(
94+
RoleCollection::class,
95+
['addFieldToFilter', 'getSize']
96+
);
97+
$roleCollectionMock->method('addFieldToFilter')->willReturnSelf();
98+
$this->roleCollection = $this->createPartialMock(
99+
CollectionFactory::class,
100+
['create']
101+
);
102+
$this->roleCollection->method('create')->willReturn(
103+
$roleCollectionMock
104+
);
105+
$this->role = $this->getMockBuilder(Role::class)
106+
->setMethods(['setParentId','setRoleType','setUserId','setRoleName','setUserType','save'])
107+
->disableOriginalConstructor()
108+
->getMock();
109+
$this->role->method('setRoleName')->willReturnSelf();
110+
$this->role->method('setUserType')->willReturnSelf();
111+
$this->role->method('setUserId')->willReturnSelf();
112+
$this->role->method('setRoleType')->willReturnSelf();
113+
$this->role->method('setParentId')->willReturnSelf();
114+
$this->role->method('save')->willReturnSelf();
80115

81116
$this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
82117
->disableOriginalConstructor()
@@ -89,7 +124,9 @@ protected function setUp(): void
89124
'imsCommandOptionService' => $this->imsCommandOptionService,
90125
'cacheTypeList' => $this->typeListInterface,
91126
'updateTokenService' => $this->updateTokensService,
92-
'authorization' => $this->authorizationUrlMock
127+
'authorization' => $this->authorizationUrlMock,
128+
'role' => $this->role,
129+
'roleCollection' => $this->roleCollection
93130
]
94131
);
95132
}

0 commit comments

Comments
 (0)