11
11
use Magento \AdminAdobeIms \Service \ImsConfig ;
12
12
use Magento \AdminAdobeIms \Service \UpdateTokensService ;
13
13
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 ;
14
18
use Magento \Framework \App \Cache \Type \Config ;
15
19
use Magento \Framework \App \Cache \TypeListInterface ;
20
+ use Magento \Framework \App \ObjectManager ;
16
21
use Magento \Framework \Console \Cli ;
17
22
use Magento \Framework \Exception \InvalidArgumentException ;
18
23
use Magento \Framework \Exception \LocalizedException ;
@@ -67,6 +72,16 @@ class AdminAdobeImsEnableCommand extends Command
67
72
*/
68
73
private UpdateTokensService $ updateTokensService ;
69
74
75
+ /**
76
+ * @var Role
77
+ */
78
+ private Role $ role ;
79
+
80
+ /**
81
+ * @var CollectionFactory
82
+ */
83
+ private CollectionFactory $ roleCollection ;
84
+
70
85
/**
71
86
* @var AuthorizationInterface
72
87
*/
@@ -78,20 +93,26 @@ class AdminAdobeImsEnableCommand extends Command
78
93
* @param TypeListInterface $cacheTypeList
79
94
* @param UpdateTokensService $updateTokensService
80
95
* @param AuthorizationInterface $authorization
96
+ * @param Role|null $role
97
+ * @param CollectionFactory|null $roleCollection
81
98
*/
82
99
public function __construct (
83
100
ImsConfig $ adminImsConfig ,
84
101
ImsCommandOptionService $ imsCommandOptionService ,
85
102
TypeListInterface $ cacheTypeList ,
86
103
UpdateTokensService $ updateTokensService ,
87
- AuthorizationInterface $ authorization
104
+ AuthorizationInterface $ authorization ,
105
+ Role $ role = null ,
106
+ CollectionFactory $ roleCollection = null
88
107
) {
89
108
parent ::__construct ();
90
109
$ this ->adminImsConfig = $ adminImsConfig ;
91
110
$ this ->imsCommandOptionService = $ imsCommandOptionService ;
92
111
$ this ->cacheTypeList = $ cacheTypeList ;
93
112
$ this ->updateTokensService = $ updateTokensService ;
94
113
$ this ->authorization = $ authorization ;
114
+ $ this ->role = $ role ?: ObjectManager::getInstance ()->get (Role::class);
115
+ $ this ->roleCollection = $ roleCollection ?: ObjectManager::getInstance ()->get (CollectionFactory::class);
95
116
96
117
$ this ->setName ('admin:adobe-ims:enable ' )
97
118
->setDescription ('Enable Adobe IMS Module. ' )
@@ -163,6 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
163
184
if ($ clientId && $ clientSecret && $ organizationId && $ isTwoFactorAuthEnabled ) {
164
185
$ enabled = $ this ->enableModule ($ clientId , $ clientSecret , $ organizationId , $ isTwoFactorAuthEnabled );
165
186
if ($ enabled ) {
187
+ $ this ->saveImsAuthorizationRole ();
166
188
$ output ->writeln (__ ('Admin Adobe IMS integration is enabled ' ));
167
189
return Cli::RETURN_SUCCESS ;
168
190
}
@@ -181,6 +203,27 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
181
203
}
182
204
}
183
205
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
+
184
227
/**
185
228
* Enable Admin Adobe IMS Module when testConnection was successfully
186
229
*
0 commit comments