-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from yangyao/oauth
Inject OAuth integrations after enable plugin
- Loading branch information
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace AfterShip\Tracking\Plugin; | ||
|
||
use Magento\Integration\Api\IntegrationServiceInterface; | ||
use Magento\Authorization\Model\UserContextInterface; | ||
use Magento\Store\Api\Data\StoreConfigInterface; | ||
use Magento\Store\Api\StoreConfigManagerInterface; | ||
use Magento\Store\Api\Data\StoreConfigExtensionFactory; | ||
|
||
class StoreConfigExtensionAttributes | ||
{ | ||
private $userContext; | ||
private $integrationService; | ||
private $storeConfigExtensionFactory; | ||
|
||
public function __construct( | ||
StoreConfigExtensionFactory $storeConfigExtensionFactory, | ||
UserContextInterface $userContext, | ||
IntegrationServiceInterface $integrationService | ||
) | ||
{ | ||
|
||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace AfterShip\Tracking\Setup; | ||
|
||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
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 | ||
{ | ||
|
||
/** | ||
* @var StoreRepositoryInterface | ||
*/ | ||
private $storeRepository; | ||
|
||
/** | ||
* Integration service | ||
* | ||
* @var IntegrationServiceInterface | ||
*/ | ||
protected $integrationService; | ||
|
||
/** | ||
* @var AuthorizationServiceInterface $authorizationService | ||
*/ | ||
protected $authorizationService; | ||
|
||
private $apps = ['tracking', 'returns']; | ||
|
||
public function __construct( | ||
StoreRepositoryInterface $storeRepository, | ||
IntegrationServiceInterface $integrationService, | ||
AuthorizationServiceInterface $authorizationService | ||
) | ||
{ | ||
$this->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' => '[email protected]', | ||
'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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Store\Api\StoreConfigManagerInterface"> | ||
<plugin name="add_custom_field_to_store" type="AfterShip\Tracking\Plugin\StoreConfigExtensionAttributes"/> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> | ||
<extension_attributes for="Magento\Store\Api\Data\StoreConfigInterface"> | ||
<attribute code="permissions" type="string"/> | ||
</extension_attributes> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="AfterShip_Tracking" setup_version="1.0.0" schema_version="1.0.0"> | ||
<module name="AfterShip_Tracking" setup_version="1.0.1" schema_version="1.0.0"> | ||
</module> | ||
</config> |