Skip to content

Commit cf18f58

Browse files
committed
FIX incorrect saving of tenantId, allow scope to be custom
1 parent e2d46c8 commit cf18f58

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/XeroController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99

1010
class XeroController extends Controller
1111
{
12+
private static $scope = 'openid offline_access email profile accounting.contacts accounting.contacts.read accounting.transactions accounting.transactions.read';
13+
1214
public function index()
1315
{
1416
$url = self::join_links(Director::absoluteBaseURL() . 'xero');
1517

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

1820
if (!isset($_GET['code'])) {
21+
$scope = $this->config()->get('scope');
22+
1923
// If we don't have an authorization code then get one
2024
$authUrl = $provider->getAuthorizationUrl([
21-
'scope' => 'openid offline_access email profile accounting.contacts accounting.transactions'
25+
'scope' => $scope
2226
]);
2327

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

5256
foreach ($tenants as $tenant) {
53-
$id = $tenant->id;
57+
$id = $tenant->tenantId;
5458

5559
$obj->XeroTenantId = $id;
5660
$obj->write();

src/XeroFactory.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,34 @@ public function setupApplication()
2929
{
3030
$provider = $this->getProvider();
3131
$config = SiteConfig::current_site_config();
32+
$refresh = !$config->XeroTokenRefreshExpires || $config->dbObject('XeroTokenRefreshExpires')->InPast();
3233

33-
$newAccessToken = $provider->getAccessToken('refresh_token', [
34-
'refresh_token' => $config->XeroRefreshToken
35-
]);
34+
if ($refresh) {
35+
$newAccessToken = $provider->getAccessToken('refresh_token', [
36+
'refresh_token' => $config->XeroRefreshToken
37+
]);
3638

37-
$config->XeroAccessToken = $newAccessToken->getToken();
39+
$accessToken = $newAccessToken->getToken();
40+
$refreshToken = $newAccessToken->getRefreshToken();
3841

39-
$refresh = $newAccessToken->getRefreshToken();
42+
$config->XeroRefreshToken = $refreshToken;
43+
$config->XeroAccessToken = $accessToken;
44+
$config->XeroTokenRefreshExpires = $newAccessToken->getExpires();
4045

41-
if ($refresh) {
42-
$config->XeroRefreshToken = $refresh;
46+
$this->tenants = $provider->getTenants($newAccessToken);
47+
48+
$config->XeroTenants = serialize($this->tenants);
49+
$config->write();
50+
} else {
51+
$accessToken = $config->XeroAccessToken;
52+
53+
$this->tenants = unserialize($config->XeroTenants);
4354
}
4455

45-
$this->tenants = $provider->getTenants($newAccessToken);
46-
$config->write();
56+
$this->application = new \XeroPHP\Application(
57+
$accessToken,
58+
$config->XeroTenantId
59+
);
4760
}
4861

4962
/**

src/XeroSiteConfigExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class XeroSiteConfigExtension extends DataExtension
1313
{
1414
private static $db = [
1515
'XeroTenantId' => 'Varchar(200)',
16-
'XeroAccessToken' => 'Varchar(200)',
17-
'XeroRefreshToken' => 'Varchar(200)'
16+
'XeroAccessToken' => 'Text',
17+
'XeroRefreshToken' => 'Text',
18+
'XeroTokenRefreshExpires' => 'Datetime',
19+
'XeroTenants' => 'Text'
1820
];
1921

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

4244
foreach ($tenantRecords as $tenant) {
43-
$tenants[$tenant->id] = $tenant->tenantName;
45+
$tenants[$tenant->tenantId] = $tenant->tenantName;
4446
}
4547

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

0 commit comments

Comments
 (0)