Skip to content

Commit 595e06d

Browse files
authored
Merge pull request #1364 from magento-jackalopes/MAGETWO-69675
[jackalopes] MAGETWO-69675: Failed upgrade from 2.1.7 EE to 2.2.0
2 parents 54998a2 + e778bb3 commit 595e06d

File tree

13 files changed

+338
-51
lines changed

13 files changed

+338
-51
lines changed

app/code/Magento/Integration/Model/AdminTokenService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ public function createAdminAccessToken($username, $password)
9595
}
9696

9797
/**
98-
* {@inheritdoc}
98+
* Revoke token by admin id.
99+
*
100+
* The function will delete the token from the oauth_token table.
101+
*
102+
* @param int $adminId
103+
* @return bool
104+
* @throws \Magento\Framework\Exception\LocalizedException
99105
*/
100106
public function revokeAdminAccessToken($adminId)
101107
{
@@ -105,7 +111,7 @@ public function revokeAdminAccessToken($adminId)
105111
}
106112
try {
107113
foreach ($tokenCollection as $token) {
108-
$token->setRevoked(1)->save();
114+
$token->delete();
109115
}
110116
} catch (\Exception $e) {
111117
throw new LocalizedException(__('The tokens could not be revoked.'));

app/code/Magento/Integration/Model/CustomerTokenService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ public function createCustomerAccessToken($username, $password)
8888
}
8989

9090
/**
91-
* {@inheritdoc}
91+
* Revoke token by customer id.
92+
*
93+
* The function will delete the token from the oauth_token table.
94+
*
95+
* @param int $customerId
96+
* @return bool
97+
* @throws \Magento\Framework\Exception\LocalizedException
9298
*/
9399
public function revokeCustomerAccessToken($customerId)
94100
{
@@ -98,7 +104,7 @@ public function revokeCustomerAccessToken($customerId)
98104
}
99105
try {
100106
foreach ($tokenCollection as $token) {
101-
$token->setRevoked(1)->save();
107+
$token->delete();
102108
}
103109
} catch (\Exception $e) {
104110
throw new LocalizedException(__('The tokens could not be revoked.'));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Integration\Plugin\Model;
7+
8+
use Magento\Integration\Model\AdminTokenService;
9+
10+
/**
11+
* Plugin to delete admin tokens when admin becomes inactive
12+
*/
13+
class AdminUser
14+
{
15+
/**
16+
* @var AdminTokenService
17+
*/
18+
private $adminTokenService;
19+
20+
/**
21+
* @param AdminTokenService $adminTokenService
22+
*/
23+
public function __construct(
24+
AdminTokenService $adminTokenService
25+
) {
26+
$this->adminTokenService = $adminTokenService;
27+
}
28+
29+
/**
30+
* Check if admin is inactive - if so, invalidate their tokens
31+
*
32+
* @param \Magento\User\Model\User $subject
33+
* @param \Magento\Framework\DataObject $object
34+
* @return $this
35+
*/
36+
public function afterSave(
37+
\Magento\User\Model\User $subject,
38+
\Magento\Framework\DataObject $object
39+
) {
40+
$isActive = $object->getIsActive();
41+
if (isset($isActive) && $isActive == 0) {
42+
$this->adminTokenService->revokeAdminAccessToken($object->getId());
43+
}
44+
return $subject;
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Integration\Plugin\Model;
7+
8+
use Magento\Integration\Model\CustomerTokenService;
9+
10+
/**
11+
* Plugin to delete customer tokens when customer becomes inactive
12+
*/
13+
class CustomerUser
14+
{
15+
/**
16+
* @var CustomerTokenService
17+
*/
18+
private $customerTokenService;
19+
20+
/**
21+
* @param CustomerTokenService $customerTokenService
22+
*/
23+
public function __construct(
24+
CustomerTokenService $customerTokenService
25+
) {
26+
$this->customerTokenService = $customerTokenService;
27+
}
28+
29+
/**
30+
* Check if customer is inactive - if so, invalidate their tokens
31+
*
32+
* @param \Magento\Customer\Model\Customer $subject
33+
* @param \Magento\Framework\DataObject $object
34+
* @return $this
35+
*/
36+
public function afterSave(
37+
\Magento\Customer\Model\Customer $subject,
38+
\Magento\Framework\DataObject $object
39+
) {
40+
$isActive = $object->getIsActive();
41+
if (isset($isActive) && $isActive == 0) {
42+
$this->customerTokenService->revokeCustomerAccessToken($object->getId());
43+
}
44+
return $subject;
45+
}
46+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Integration\Setup;
7+
8+
use Magento\Framework\Setup\UpgradeDataInterface;
9+
use Magento\Framework\Setup\ModuleContextInterface;
10+
use Magento\Framework\Setup\ModuleDataSetupInterface;
11+
12+
/**
13+
* Upgrade data script for Integration module
14+
*/
15+
class UpgradeData implements UpgradeDataInterface
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
21+
{
22+
$setup->startSetup();
23+
24+
if (version_compare($context->getVersion(), '2.2.0', '<')) {
25+
$this->removeRevokedTokens($setup);
26+
$this->removeTokensFromInactiveAdmins($setup);
27+
$this->removeTokensFromInactiveCustomers($setup);
28+
}
29+
30+
$setup->endSetup();
31+
}
32+
33+
/**
34+
* Remove any revoked tokens from oauth_token table
35+
*
36+
* @param ModuleDataSetupInterface $setup
37+
* @return void
38+
*/
39+
private function removeRevokedTokens($setup)
40+
{
41+
$oauthTokenTable = $setup->getTable('oauth_token');
42+
43+
$where = ['revoked = ?' => 1];
44+
$setup->getConnection()->delete($oauthTokenTable, $where);
45+
}
46+
47+
/**
48+
* Remove any tokens from oauth_token table where admin is inactive
49+
*
50+
* @param ModuleDataSetupInterface $setup
51+
* @return void
52+
*/
53+
private function removeTokensFromInactiveAdmins($setup)
54+
{
55+
$oauthTokenTable = $setup->getTable('oauth_token');
56+
$adminUserTable = $setup->getTable('admin_user');
57+
58+
$select = $setup->getConnection()->select()->from(
59+
$adminUserTable,
60+
['user_id', 'is_active']
61+
);
62+
63+
$admins = $setup->getConnection()->fetchAll($select);
64+
foreach ($admins as $admin) {
65+
if ($admin['is_active'] == 0) {
66+
$where = ['admin_id = ?' => (int)$admin['user_id']];
67+
$setup->getConnection()->delete($oauthTokenTable, $where);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Remove any tokens from oauth_token table where customer is inactive
74+
*
75+
* @param ModuleDataSetupInterface $setup
76+
* @return void
77+
*/
78+
private function removeTokensFromInactiveCustomers($setup)
79+
{
80+
$oauthTokenTable = $setup->getTable('oauth_token');
81+
$adminUserTable = $setup->getTable('customer_entity');
82+
83+
$select = $setup->getConnection()->select()->from(
84+
$adminUserTable,
85+
['entity_id', 'is_active']
86+
);
87+
88+
$admins = $setup->getConnection()->fetchAll($select);
89+
foreach ($admins as $admin) {
90+
if ($admin['is_active'] == 0) {
91+
$where = ['customer_id = ?' => (int)$admin['entity_id']];
92+
$setup->getConnection()->delete($oauthTokenTable, $where);
93+
}
94+
}
95+
}
96+
}

app/code/Magento/Integration/Test/Unit/Model/AdminTokenServiceTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Integration\Model\Integration;
1414
use Magento\Integration\Model\Oauth\Token;
1515

16+
/**
17+
* Test for \Magento\Integration\Model\AdminTokenService
18+
*/
1619
class AdminTokenServiceTest extends \PHPUnit_Framework_TestCase
1720
{
1821
/** \Magento\Integration\Model\AdminTokenService */
@@ -50,7 +53,7 @@ protected function setUp()
5053

5154
$this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
5255
->disableOriginalConstructor()
53-
->setMethods(['getToken', 'loadByAdminId', 'setRevoked', 'save', '__wakeup'])->getMock();
56+
->setMethods(['getToken', 'loadByAdminId', 'delete', '__wakeup'])->getMock();
5457

5558
$this->_tokenModelCollectionMock = $this->getMockBuilder(
5659
\Magento\Integration\Model\ResourceModel\Oauth\Token\Collection::class
@@ -97,10 +100,8 @@ public function testRevokeAdminAccessToken()
97100
->with(null)
98101
->will($this->returnValue(1));
99102
$this->_tokenMock->expects($this->once())
100-
->method('setRevoked')
103+
->method('delete')
101104
->will($this->returnValue($this->_tokenMock));
102-
$this->_tokenMock->expects($this->once())
103-
->method('save');
104105

105106
$this->assertTrue($this->_tokenService->revokeAdminAccessToken($adminId));
106107
}
@@ -116,9 +117,7 @@ public function testRevokeAdminAccessTokenWithoutAdminId()
116117
->with(null)
117118
->will($this->returnValue($this->_tokenModelCollectionMock));
118119
$this->_tokenMock->expects($this->never())
119-
->method('save');
120-
$this->_tokenMock->expects($this->never())
121-
->method('setRevoked')
120+
->method('delete')
122121
->will($this->returnValue($this->_tokenMock));
123122
$this->_tokenService->revokeAdminAccessToken(null);
124123
}
@@ -142,10 +141,8 @@ public function testRevokeAdminAccessTokenCannotRevoked()
142141
->method('getIterator')
143142
->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
144143

145-
$this->_tokenMock->expects($this->never())
146-
->method('save');
147144
$this->_tokenMock->expects($this->once())
148-
->method('setRevoked')
145+
->method('delete')
149146
->will($this->throwException($exception));
150147
$this->_tokenService->revokeAdminAccessToken($adminId);
151148
}

app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Integration\Model\Integration;
1212
use Magento\Integration\Model\Oauth\Token;
1313

14+
/**
15+
* Test for \Magento\Integration\Model\CustomerTokenService
16+
*/
1417
class CustomerTokenServiceTest extends \PHPUnit_Framework_TestCase
1518
{
1619
/** \Magento\Integration\Model\CustomerTokenService */
@@ -49,7 +52,7 @@ protected function setUp()
4952

5053
$this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
5154
->disableOriginalConstructor()
52-
->setMethods(['getToken', 'loadByCustomerId', 'setRevoked', 'save', '__wakeup'])->getMock();
55+
->setMethods(['getToken', 'loadByCustomerId', 'delete', '__wakeup'])->getMock();
5356

5457
$this->_tokenModelCollectionMock = $this->getMockBuilder(
5558
\Magento\Integration\Model\ResourceModel\Oauth\Token\Collection::class
@@ -95,10 +98,8 @@ public function testRevokeCustomerAccessToken()
9598
->method('_fetchAll')
9699
->will($this->returnValue(1));
97100
$this->_tokenMock->expects($this->once())
98-
->method('setRevoked')
101+
->method('delete')
99102
->will($this->returnValue($this->_tokenMock));
100-
$this->_tokenMock->expects($this->once())
101-
->method('save');
102103

103104
$this->assertTrue($this->_tokenService->revokeCustomerAccessToken($customerId));
104105
}
@@ -114,9 +115,7 @@ public function testRevokeCustomerAccessTokenWithoutCustomerId()
114115
->with(null)
115116
->will($this->returnValue($this->_tokenModelCollectionMock));
116117
$this->_tokenMock->expects($this->never())
117-
->method('save');
118-
$this->_tokenMock->expects($this->never())
119-
->method('setRevoked')
118+
->method('delete')
120119
->will($this->returnValue($this->_tokenMock));
121120
$this->_tokenService->revokeCustomerAccessToken(null);
122121
}
@@ -140,10 +139,8 @@ public function testRevokeCustomerAccessTokenCannotRevoked()
140139
->method('getIterator')
141140
->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
142141

143-
$this->_tokenMock->expects($this->never())
144-
->method('save');
145142
$this->_tokenMock->expects($this->once())
146-
->method('setRevoked')
143+
->method('delete')
147144
->will($this->throwException($exception));
148145
$this->_tokenService->revokeCustomerAccessToken($customerId);
149146
}

app/code/Magento/Integration/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
<type name="Magento\Integration\Api\IntegrationServiceInterface">
3030
<plugin name="webapiIntegrationService" type="Magento\Integration\Model\Plugin\Integration"/>
3131
</type>
32+
<type name="Magento\User\Model\User">
33+
<plugin name="revokeTokensFromInactiveAdmins" type="Magento\Integration\Plugin\Model\AdminUser" />
34+
</type>
35+
<type name="Magento\Customer\Model\Customer">
36+
<plugin name="revokeTokensFromInactiveCustomers" type="Magento\Integration\Plugin\Model\CustomerUser" />
37+
</type>
3238
</config>

app/code/Magento/Integration/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_Integration" setup_version="2.0.1">
9+
<module name="Magento_Integration" setup_version="2.2.0">
1010
<sequence>
1111
<module name="Magento_Store"/>
1212
<module name="Magento_User"/>

0 commit comments

Comments
 (0)