Skip to content

Commit

Permalink
Merge pull request #7 from yangyao/oauth
Browse files Browse the repository at this point in the history
Inject OAuth integrations after enable plugin
  • Loading branch information
yangyao authored Jun 9, 2023
2 parents 834146b + 8087b57 commit 3aca922
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
60 changes: 60 additions & 0 deletions Plugin/StoreConfigExtensionAttributes.php
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;
}
}
92 changes: 92 additions & 0 deletions Setup/RecurringData.php
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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
6 changes: 6 additions & 0 deletions etc/di.xml
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>
6 changes: 6 additions & 0 deletions etc/extension_attributes.xml
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>
2 changes: 1 addition & 1 deletion etc/module.xml
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>

0 comments on commit 3aca922

Please sign in to comment.