1111use Magento \AdminAdobeIms \Service \ImsConfig ;
1212use Magento \AdminAdobeIms \Service \UpdateTokensService ;
1313use 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 ;
1418use Magento \Framework \App \Cache \Type \Config ;
1519use Magento \Framework \App \Cache \TypeListInterface ;
20+ use Magento \Framework \App \ObjectManager ;
1621use Magento \Framework \Console \Cli ;
1722use Magento \Framework \Exception \InvalidArgumentException ;
1823use 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 *
0 commit comments