Skip to content

Commit

Permalink
FIX incorrect saving of tenantId, allow scope to be custom
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Sep 16, 2021
1 parent e2d46c8 commit cf18f58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/XeroController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@

class XeroController extends Controller
{
private static $scope = 'openid offline_access email profile accounting.contacts accounting.contacts.read accounting.transactions accounting.transactions.read';

public function index()
{
$url = self::join_links(Director::absoluteBaseURL() . 'xero');

$provider = XeroFactory::singleton()->getProvider();

if (!isset($_GET['code'])) {
$scope = $this->config()->get('scope');

// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl([
'scope' => 'openid offline_access email profile accounting.contacts accounting.transactions'
'scope' => $scope
]);

$_SESSION['oauth2state'] = $provider->getState();
Expand Down Expand Up @@ -50,7 +54,7 @@ public function index()
$tenants = $provider->getTenants($token);

foreach ($tenants as $tenant) {
$id = $tenant->id;
$id = $tenant->tenantId;

$obj->XeroTenantId = $id;
$obj->write();
Expand Down
31 changes: 22 additions & 9 deletions src/XeroFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,34 @@ public function setupApplication()
{
$provider = $this->getProvider();
$config = SiteConfig::current_site_config();
$refresh = !$config->XeroTokenRefreshExpires || $config->dbObject('XeroTokenRefreshExpires')->InPast();

$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $config->XeroRefreshToken
]);
if ($refresh) {
$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $config->XeroRefreshToken
]);

$config->XeroAccessToken = $newAccessToken->getToken();
$accessToken = $newAccessToken->getToken();
$refreshToken = $newAccessToken->getRefreshToken();

$refresh = $newAccessToken->getRefreshToken();
$config->XeroRefreshToken = $refreshToken;
$config->XeroAccessToken = $accessToken;
$config->XeroTokenRefreshExpires = $newAccessToken->getExpires();

if ($refresh) {
$config->XeroRefreshToken = $refresh;
$this->tenants = $provider->getTenants($newAccessToken);

$config->XeroTenants = serialize($this->tenants);
$config->write();
} else {
$accessToken = $config->XeroAccessToken;

$this->tenants = unserialize($config->XeroTenants);
}

$this->tenants = $provider->getTenants($newAccessToken);
$config->write();
$this->application = new \XeroPHP\Application(
$accessToken,
$config->XeroTenantId
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/XeroSiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class XeroSiteConfigExtension extends DataExtension
{
private static $db = [
'XeroTenantId' => 'Varchar(200)',
'XeroAccessToken' => 'Varchar(200)',
'XeroRefreshToken' => 'Varchar(200)'
'XeroAccessToken' => 'Text',
'XeroRefreshToken' => 'Text',
'XeroTokenRefreshExpires' => 'Datetime',
'XeroTenants' => 'Text'
];

public function updateCMSFields(FieldList $fields)
Expand All @@ -40,7 +42,7 @@ public function updateCMSFields(FieldList $fields)
$tenantRecords = XeroFactory::singleton()->getTenants($this->owner->XeroAccessToken);

foreach ($tenantRecords as $tenant) {
$tenants[$tenant->id] = $tenant->tenantName;
$tenants[$tenant->tenantId] = $tenant->tenantName;
}

$fields->addFieldsToTab('Root.Xero', [
Expand Down

0 comments on commit cf18f58

Please sign in to comment.