99namespace Magento \AdminAdobeIms \Console \Command ;
1010
1111use Magento \AdminAdobeIms \Model \ImsConnection ;
12- use Magento \AdminAdobeIms \Service \UpdateTokensService ;
1312use Magento \AdminAdobeIms \Service \ImsCommandOptionService ;
1413use Magento \AdminAdobeIms \Service \ImsConfig ;
14+ use Magento \AdminAdobeIms \Service \UpdateTokensService ;
15+ use Magento \Authorization \Model \ResourceModel \Role \CollectionFactory ;
16+ use Magento \Authorization \Model \Role ;
17+ use Magento \Authorization \Model \UserContextInterface ;
1518use Magento \Framework \App \Cache \Type \Config ;
1619use Magento \Framework \App \Cache \TypeListInterface ;
20+ use Magento \Framework \App \ObjectManager ;
1721use Magento \Framework \Console \Cli ;
1822use Magento \Framework \Exception \InvalidArgumentException ;
1923use Magento \Framework \Exception \LocalizedException ;
@@ -73,26 +77,42 @@ class AdminAdobeImsEnableCommand extends Command
7377 */
7478 private UpdateTokensService $ updateTokensService ;
7579
80+ /**
81+ * @var Role|mixed
82+ */
83+ private Role $ role ;
84+
85+ /**
86+ * @var CollectionFactory
87+ */
88+ private CollectionFactory $ roleCollection ;
89+
7690 /**
7791 * @param ImsConfig $adminImsConfig
7892 * @param ImsConnection $adminImsConnection
7993 * @param ImsCommandOptionService $imsCommandOptionService
8094 * @param TypeListInterface $cacheTypeList
8195 * @param UpdateTokensService $updateTokensService
96+ * @param Role $role
97+ * @param CollectionFactory $roleCollection
8298 */
8399 public function __construct (
84100 ImsConfig $ adminImsConfig ,
85101 ImsConnection $ adminImsConnection ,
86102 ImsCommandOptionService $ imsCommandOptionService ,
87103 TypeListInterface $ cacheTypeList ,
88- UpdateTokensService $ updateTokensService
104+ UpdateTokensService $ updateTokensService ,
105+ Role $ role = null ,
106+ CollectionFactory $ roleCollection = null
89107 ) {
90108 parent ::__construct ();
91109 $ this ->adminImsConfig = $ adminImsConfig ;
92110 $ this ->adminImsConnection = $ adminImsConnection ;
93111 $ this ->imsCommandOptionService = $ imsCommandOptionService ;
94112 $ this ->cacheTypeList = $ cacheTypeList ;
95113 $ this ->updateTokensService = $ updateTokensService ;
114+ $ this ->role = $ role ?: ObjectManager::getInstance ()->get (Role::class);
115+ $ this ->roleCollection = $ roleCollection ?: ObjectManager::getInstance ()->get (CollectionFactory::class);
96116
97117 $ this ->setName ('admin:adobe-ims:enable ' )
98118 ->setDescription ('Enable Adobe IMS Module. ' )
@@ -164,6 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
164184 if ($ clientId && $ clientSecret && $ organizationId && $ isTwoFactorAuthEnabled ) {
165185 $ enabled = $ this ->enableModule ($ clientId , $ clientSecret , $ organizationId , $ isTwoFactorAuthEnabled );
166186 if ($ enabled ) {
187+ $ this ->saveIMSAuthorizationRole ();
167188 $ output ->writeln (__ ('Admin Adobe IMS integration is enabled ' ));
168189 return Cli::RETURN_SUCCESS ;
169190 }
@@ -182,6 +203,22 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
182203 }
183204 }
184205
206+ private function saveIMSAuthorizationRole (): bool
207+ {
208+ $ roleCollection = $ this ->roleCollection ->create ()->addFieldToFilter ('role_name ' , 'AdobeImsRole ' );
209+
210+ if (!$ roleCollection ->getSize ()) {
211+ $ this ->role ->setRoleName ('AdobeImsRole ' )
212+ ->setUserType ((string )UserContextInterface::USER_TYPE_ADMIN )
213+ ->setUserId (0 )
214+ ->setRoleType (\Magento \Authorization \Model \Acl \Role \Group::ROLE_TYPE )
215+ ->setParentId (0 )
216+ ->save ();
217+ return true ;
218+ }
219+ return true ;
220+ }
221+
185222 /**
186223 * Enable Admin Adobe IMS Module when testConnection was successfully
187224 *
0 commit comments