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..cc7593f --- /dev/null +++ b/Plugin/StoreConfigExtensionAttributes.php @@ -0,0 +1,60 @@ +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(); + } + // 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; + } +} diff --git a/Setup/RecurringData.php b/Setup/RecurringData.php new file mode 100644 index 0000000..bbd41bd --- /dev/null +++ b/Setup/RecurringData.php @@ -0,0 +1,92 @@ +storeRepository = $storeRepository; + $this->integrationService = $integrationService; + $this->authorizationService = $authorizationService; + } + + /** + * {@inheritdoc} + */ + + public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $storeList = $this->storeRepository->getList(); + 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; + } +} 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 @@ - +