From 46c9e4e457115f0e2db3970b226331ded6944fbb Mon Sep 17 00:00:00 2001 From: yangyao Date: Tue, 30 May 2023 11:33:23 +0800 Subject: [PATCH 1/5] Inject OAuth integrations after enable plugin --- .gitignore | 1 + Plugin/StoreConfigExtensionAttributes.php | 53 +++++++++++++++++++++ Setup/InstallData.php | 52 +++++++++++++++++++++ Setup/IntegrationTrait.php | 57 +++++++++++++++++++++++ Setup/UpgradeData.php | 48 +++++++++++++++++++ composer.json | 2 +- etc/di.xml | 6 +++ etc/extension_attributes.xml | 6 +++ etc/module.xml | 2 +- 9 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Plugin/StoreConfigExtensionAttributes.php create mode 100644 Setup/InstallData.php create mode 100644 Setup/IntegrationTrait.php create mode 100644 Setup/UpgradeData.php create mode 100644 etc/di.xml create mode 100644 etc/extension_attributes.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/Plugin/StoreConfigExtensionAttributes.php b/Plugin/StoreConfigExtensionAttributes.php new file mode 100644 index 0000000..b009a66 --- /dev/null +++ b/Plugin/StoreConfigExtensionAttributes.php @@ -0,0 +1,53 @@ +storeConfigExtensionFactory = $storeConfigExtensionFactory; + $this->userContext = $userContext; + $this->integrationService = $integrationService; + } + + private function getApiScopes() + { + $integrationId = $this->userContext->getUserId(); + $apiScopes = ''; + if ($integrationId) { + $scopes = $this->integrationService->getSelectedResources($integrationId); + $apiScopes = is_array($scopes) ? implode(',', $scopes) : $scopes; + } + return $apiScopes; + } + + public function afterGetStoreConfigs(StoreConfigManagerInterface $subject, $result) + { + /** @var StoreConfigInterface $store */ + foreach ($result as $store) { + $extensionAttributes = $store->getExtensionAttributes(); + if (!$extensionAttributes) { + $extensionAttributes = $this->storeConfigExtensionFactory->create(); + } + $extensionAttributes->setData('permissions', $this->getApiScopes()); + $store->setExtensionAttributes($extensionAttributes); + } + return $result; + } +} diff --git a/Setup/InstallData.php b/Setup/InstallData.php new file mode 100644 index 0000000..aa735c4 --- /dev/null +++ b/Setup/InstallData.php @@ -0,0 +1,52 @@ +storeRepository = $storeRepository; + $this->integrationService = $integrationService; + $this->authorizationService = $authorizationService; + } + + /** + * {@inheritdoc} + */ + + public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $this->run($setup, $context); + } +} diff --git a/Setup/IntegrationTrait.php b/Setup/IntegrationTrait.php new file mode 100644 index 0000000..2b02c54 --- /dev/null +++ b/Setup/IntegrationTrait.php @@ -0,0 +1,57 @@ +storeRepository->getList(); + $integrationNames = []; + foreach ($storeList as $index => $item) { + $storeId = $item->getId(); + if ($storeId == 0 || $storeId > 100) continue; + foreach ($this->apps as $app) { + $this->createIntegration($this->buildIntegrationData($app, $storeId, $item->getCode())); + } + } + } + + private function buildIntegrationData($app, $storeId, $storeCode) + { + $name = sprintf("AfterShip %s For Store: %s", ucfirst($app), $storeCode); + $identityLinkUrl = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/identity", $app); + $endpoint = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/callback?store_id=%d", $app, $storeId); + if ($app === 'tracking') { + $endpoint = sprintf("https://accounts.aftership.com/oauth/magento-2/callback?store_id=%d", $storeId); + $identityLinkUrl = 'https://accounts.aftership.com/oauth/magento-2/identity'; + } + $integrationData = [ + 'name' => $name, + 'email' => 'apps@aftership.com', + 'endpoint' => $endpoint, + 'identity_link_url' => $identityLinkUrl + ]; + return $integrationData; + } + + private function createIntegration($integrationData) + { + $integration = $this->integrationService->findByName($integrationData['name']); + if ($integration->getId()) { + $integrationData[Integration::ID] = $integration->getId(); + $this->integrationService->update($integrationData); + } else { + $integration = $this->integrationService->create($integrationData); + } + $this->authorizationService->grantAllPermissions($integration->getId()); + return $integration; + } + +} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 0000000..500575e --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,48 @@ +storeRepository = $storeRepository; + $this->integrationService = $integrationService; + $this->authorizationService = $authorizationService; + } + + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $this->run($setup, $context); + } +} diff --git a/composer.json b/composer.json index c32b29d..69255b7 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "aftership/aftership-apps-magento2", "description": "AfterShip extension for Magento 2. Allows connect with AfterShip and more.", "type": "magento2-module", - "version": "1.0.0", + "version": "1.0.1", "minimum-stability": "stable", "license": "MIT", "keywords": ["aftership", "magento", "magento2", "magento-2", "tracking", "shipping"], diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..9375027 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etc/extension_attributes.xml b/etc/extension_attributes.xml new file mode 100644 index 0000000..b3891ed --- /dev/null +++ b/etc/extension_attributes.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etc/module.xml b/etc/module.xml index 03ae76b..d793502 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - + From 180d9d5681a95c162341879a3241fc86f8e21c33 Mon Sep 17 00:00:00 2001 From: yangyao Date: Tue, 30 May 2023 12:29:42 +0800 Subject: [PATCH 2/5] Add compatibility for magento version >=2.3 --- Plugin/StoreConfigExtensionAttributes.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Plugin/StoreConfigExtensionAttributes.php b/Plugin/StoreConfigExtensionAttributes.php index b009a66..cc7593f 100644 --- a/Plugin/StoreConfigExtensionAttributes.php +++ b/Plugin/StoreConfigExtensionAttributes.php @@ -45,7 +45,14 @@ public function afterGetStoreConfigs(StoreConfigManagerInterface $subject, $resu if (!$extensionAttributes) { $extensionAttributes = $this->storeConfigExtensionFactory->create(); } - $extensionAttributes->setData('permissions', $this->getApiScopes()); + // setPermissions method is generated by extension_attributes.xml. + if (method_exists($extensionAttributes, 'setPermissions')) { + call_user_func_array(array($extensionAttributes, 'setPermissions'), array($this->getApiScopes())); + } + // Pass Upgrade compatibility tool check. + if (method_exists($extensionAttributes, 'setData')) { + call_user_func_array(array($extensionAttributes, 'setData'), array('permissions', $this->getApiScopes())); + } $store->setExtensionAttributes($extensionAttributes); } return $result; From a129227f13da7ae17c4375d052ca092b1c671360 Mon Sep 17 00:00:00 2001 From: yangyao Date: Tue, 6 Jun 2023 16:43:10 +0800 Subject: [PATCH 3/5] Try replace install & update script with recurring script --- Setup/{InstallData.php => RecurringData.php} | 2 +- Setup/UpgradeData.php | 48 -------------------- 2 files changed, 1 insertion(+), 49 deletions(-) rename Setup/{InstallData.php => RecurringData.php} (96%) delete mode 100644 Setup/UpgradeData.php diff --git a/Setup/InstallData.php b/Setup/RecurringData.php similarity index 96% rename from Setup/InstallData.php rename to Setup/RecurringData.php index aa735c4..b12bc96 100644 --- a/Setup/InstallData.php +++ b/Setup/RecurringData.php @@ -9,7 +9,7 @@ use Magento\Framework\Setup\InstallDataInterface; use Magento\Store\Api\StoreRepositoryInterface; -class InstallData implements InstallDataInterface +class RecurringData implements InstallDataInterface { use IntegrationTrait; diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php deleted file mode 100644 index 500575e..0000000 --- a/Setup/UpgradeData.php +++ /dev/null @@ -1,48 +0,0 @@ -storeRepository = $storeRepository; - $this->integrationService = $integrationService; - $this->authorizationService = $authorizationService; - } - - public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) - { - $this->run($setup, $context); - } -} From b765258f325a2812b89aa7c9c320a54c8f4d9097 Mon Sep 17 00:00:00 2001 From: yangyao Date: Tue, 6 Jun 2023 16:59:34 +0800 Subject: [PATCH 4/5] Remove IntegrationTrait --- Setup/IntegrationTrait.php | 57 -------------------------------------- Setup/RecurringData.php | 43 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 59 deletions(-) delete mode 100644 Setup/IntegrationTrait.php diff --git a/Setup/IntegrationTrait.php b/Setup/IntegrationTrait.php deleted file mode 100644 index 2b02c54..0000000 --- a/Setup/IntegrationTrait.php +++ /dev/null @@ -1,57 +0,0 @@ -storeRepository->getList(); - $integrationNames = []; - foreach ($storeList as $index => $item) { - $storeId = $item->getId(); - if ($storeId == 0 || $storeId > 100) continue; - foreach ($this->apps as $app) { - $this->createIntegration($this->buildIntegrationData($app, $storeId, $item->getCode())); - } - } - } - - private function buildIntegrationData($app, $storeId, $storeCode) - { - $name = sprintf("AfterShip %s For Store: %s", ucfirst($app), $storeCode); - $identityLinkUrl = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/identity", $app); - $endpoint = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/callback?store_id=%d", $app, $storeId); - if ($app === 'tracking') { - $endpoint = sprintf("https://accounts.aftership.com/oauth/magento-2/callback?store_id=%d", $storeId); - $identityLinkUrl = 'https://accounts.aftership.com/oauth/magento-2/identity'; - } - $integrationData = [ - 'name' => $name, - 'email' => 'apps@aftership.com', - 'endpoint' => $endpoint, - 'identity_link_url' => $identityLinkUrl - ]; - return $integrationData; - } - - private function createIntegration($integrationData) - { - $integration = $this->integrationService->findByName($integrationData['name']); - if ($integration->getId()) { - $integrationData[Integration::ID] = $integration->getId(); - $this->integrationService->update($integrationData); - } else { - $integration = $this->integrationService->create($integrationData); - } - $this->authorizationService->grantAllPermissions($integration->getId()); - return $integration; - } - -} diff --git a/Setup/RecurringData.php b/Setup/RecurringData.php index b12bc96..76c7f8e 100644 --- a/Setup/RecurringData.php +++ b/Setup/RecurringData.php @@ -7,11 +7,11 @@ use Magento\Integration\Api\IntegrationServiceInterface; use Magento\Integration\Api\AuthorizationServiceInterface; use Magento\Framework\Setup\InstallDataInterface; +use Magento\Integration\Model\Integration; use Magento\Store\Api\StoreRepositoryInterface; class RecurringData implements InstallDataInterface { - use IntegrationTrait; /** * @var StoreRepositoryInterface @@ -47,6 +47,45 @@ public function __construct( public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { - $this->run($setup, $context); + $storeList = $this->storeRepository->getList(); + $integrationNames = []; + foreach ($storeList as $index => $item) { + $storeId = $item->getId(); + if ($storeId == 0) continue; + foreach ($this->apps as $app) { + $this->createIntegration($this->buildIntegrationData($app, $storeId, $item->getCode())); + } + } } + + private function buildIntegrationData($app, $storeId, $storeCode) + { + $name = sprintf("AfterShip %s For Store: %s", ucfirst($app), $storeCode); + $identityLinkUrl = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/identity", $app); + $endpoint = sprintf("https://accounts.aftership.com/oauth/%s/magento-2/callback?store_id=%d", $app, $storeId); + if ($app === 'tracking') { + $endpoint = sprintf("https://accounts.aftership.com/oauth/magento-2/callback?store_id=%d", $storeId); + $identityLinkUrl = 'https://accounts.aftership.com/oauth/magento-2/identity'; + } + $integrationData = [ + 'name' => $name, + 'email' => 'apps@aftership.com', + 'endpoint' => $endpoint, + 'identity_link_url' => $identityLinkUrl + ]; + return $integrationData; + } + + private function createIntegration($integrationData) + { + $integration = $this->integrationService->findByName($integrationData['name']); + if ($integration->getId()) { + $integrationData[Integration::ID] = $integration->getId(); + $this->integrationService->update($integrationData); + } else { + $integration = $this->integrationService->create($integrationData); + } + $this->authorizationService->grantAllPermissions($integration->getId()); + return $integration; + } } From 8087b57e25986a301c4a53a5b3a2de88c4adb0c4 Mon Sep 17 00:00:00 2001 From: yangyao Date: Tue, 6 Jun 2023 17:23:00 +0800 Subject: [PATCH 5/5] Remove IntegrationTrait --- Setup/RecurringData.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Setup/RecurringData.php b/Setup/RecurringData.php index 76c7f8e..bbd41bd 100644 --- a/Setup/RecurringData.php +++ b/Setup/RecurringData.php @@ -30,6 +30,8 @@ class RecurringData implements InstallDataInterface */ protected $authorizationService; + private $apps = ['tracking', 'returns']; + public function __construct( StoreRepositoryInterface $storeRepository, IntegrationServiceInterface $integrationService, @@ -48,7 +50,6 @@ public function __construct( public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $storeList = $this->storeRepository->getList(); - $integrationNames = []; foreach ($storeList as $index => $item) { $storeId = $item->getId(); if ($storeId == 0) continue;