Skip to content

Commit b98c652

Browse files
authored
Merge pull request #1170 from magento-okapis/2.2-develop-pr-1
[Okapis] Bugs P1
2 parents 6086595 + 48051a4 commit b98c652

File tree

24 files changed

+1444
-50
lines changed

24 files changed

+1444
-50
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Integration\Cron;
7+
8+
use Magento\Integration\Model\ResourceModel\Oauth\Token as TokenResourceModel;
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
11+
12+
/**
13+
* Cron class for deleting expired OAuth tokens.
14+
*/
15+
class CleanExpiredTokens
16+
{
17+
/**
18+
* @var TokenResourceModel
19+
*/
20+
private $tokenResourceModel;
21+
22+
/**
23+
* @var OauthHelper
24+
*/
25+
private $oauthHelper;
26+
27+
/**
28+
* Initialize dependencies.
29+
*
30+
* @param TokenResourceModel $tokenResourceModel
31+
* @param OauthHelper $oauthHelper
32+
*/
33+
public function __construct(
34+
TokenResourceModel $tokenResourceModel,
35+
OauthHelper $oauthHelper
36+
) {
37+
$this->tokenResourceModel = $tokenResourceModel;
38+
$this->oauthHelper = $oauthHelper;
39+
}
40+
41+
/**
42+
* Delete expired customer and admin tokens.
43+
*
44+
* @return void
45+
*/
46+
public function execute()
47+
{
48+
$this->tokenResourceModel->deleteExpiredTokens(
49+
$this->oauthHelper->getAdminTokenLifetime(),
50+
[UserContextInterface::USER_TYPE_ADMIN]
51+
);
52+
$this->tokenResourceModel->deleteExpiredTokens(
53+
$this->oauthHelper->getCustomerTokenLifetime(),
54+
[UserContextInterface::USER_TYPE_CUSTOMER]
55+
);
56+
}
57+
}

app/code/Magento/Integration/Helper/Oauth/Data.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $
6363
public function isCleanupProbability()
6464
{
6565
// Safe get cleanup probability value from system configuration
66-
$configValue = (int)$this->_scopeConfig->getValue(
67-
self::XML_PATH_CLEANUP_PROBABILITY,
68-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
69-
);
66+
$configValue = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_PROBABILITY);
7067
return $configValue > 0 ? 1 == \Magento\Framework\Math\Random::getRandomNumber(1, $configValue) : false;
7168
}
7269

@@ -77,10 +74,7 @@ public function isCleanupProbability()
7774
*/
7875
public function getCleanupExpirationPeriod()
7976
{
80-
$minutes = (int)$this->_scopeConfig->getValue(
81-
self::XML_PATH_CLEANUP_EXPIRATION_PERIOD,
82-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
83-
);
77+
$minutes = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_EXPIRATION_PERIOD);
8478
return $minutes > 0 ? $minutes : self::CLEANUP_EXPIRATION_PERIOD_DEFAULT;
8579
}
8680

@@ -91,10 +85,7 @@ public function getCleanupExpirationPeriod()
9185
*/
9286
public function getConsumerExpirationPeriod()
9387
{
94-
$seconds = (int)$this->_scopeConfig->getValue(
95-
self::XML_PATH_CONSUMER_EXPIRATION_PERIOD,
96-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
97-
);
88+
$seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_EXPIRATION_PERIOD);
9889
return $seconds > 0 ? $seconds : self::CONSUMER_EXPIRATION_PERIOD_DEFAULT;
9990
}
10091

@@ -105,10 +96,7 @@ public function getConsumerExpirationPeriod()
10596
*/
10697
public function getConsumerPostMaxRedirects()
10798
{
108-
$redirects = (int)$this->_scopeConfig->getValue(
109-
self::XML_PATH_CONSUMER_POST_MAXREDIRECTS,
110-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
111-
);
99+
$redirects = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_MAXREDIRECTS);
112100
return $redirects > 0 ? $redirects : 0;
113101
}
114102

@@ -119,10 +107,29 @@ public function getConsumerPostMaxRedirects()
119107
*/
120108
public function getConsumerPostTimeout()
121109
{
122-
$seconds = (int)$this->_scopeConfig->getValue(
123-
self::XML_PATH_CONSUMER_POST_TIMEOUT,
124-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
125-
);
110+
$seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_TIMEOUT);
126111
return $seconds > 0 ? $seconds : self::CONSUMER_POST_TIMEOUT_DEFAULT;
127112
}
113+
114+
/**
115+
* Get customer token lifetime from config.
116+
*
117+
* @return int hours
118+
*/
119+
public function getCustomerTokenLifetime()
120+
{
121+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/customer');
122+
return $hours > 0 ? $hours : 0;
123+
}
124+
125+
/**
126+
* Get customer token lifetime from config.
127+
*
128+
* @return int hours
129+
*/
130+
public function getAdminTokenLifetime()
131+
{
132+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/admin');
133+
return $hours > 0 ? $hours : 0;
134+
}
128135
}

app/code/Magento/Integration/Model/ResourceModel/Oauth/Token.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ public function deleteOldEntries($minutes)
104104
}
105105
}
106106

107+
/**
108+
* Delete expired tokens for the specified user types
109+
*
110+
* @param int $hours token lifetime
111+
* @param int[] $userTypes @see \Magento\Authorization\Model\UserContextInterface
112+
* @return int number of deleted tokens
113+
*/
114+
public function deleteExpiredTokens($hours, $userTypes)
115+
{
116+
if ($hours > 0) {
117+
$connection = $this->getConnection();
118+
119+
$userTypeCondition = $connection->quoteInto('user_type IN (?)', $userTypes);
120+
$createdAtCondition = $connection->quoteInto(
121+
'created_at <= ?',
122+
$this->_dateTime->formatDate($this->date->gmtTimestamp() - $hours * 60 * 60)
123+
);
124+
return $connection->delete(
125+
$this->getMainTable(),
126+
$userTypeCondition . ' AND ' . $createdAtCondition
127+
);
128+
} else {
129+
return 0;
130+
}
131+
}
132+
107133
/**
108134
* Select a single token of the specified type for the specified consumer.
109135
*

app/code/Magento/Integration/Test/Unit/Helper/Oauth/DataTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,44 @@ public function testGetConsumerPostTimeoutNonZero()
8181
$this->_scopeConfigMock->expects($this->once())->method('getValue')->will($this->returnValue(10));
8282
$this->assertEquals(10, $this->_dataHelper->getConsumerPostTimeout());
8383
}
84+
85+
public function testGetCustomerTokenLifetimeNotEmpty()
86+
{
87+
$this->_scopeConfigMock
88+
->expects($this->once())
89+
->method('getValue')
90+
->with('oauth/access_token_lifetime/customer')
91+
->will($this->returnValue(10));
92+
$this->assertEquals(10, $this->_dataHelper->getCustomerTokenLifetime());
93+
}
94+
95+
public function testGetCustomerTokenLifetimeEmpty()
96+
{
97+
$this->_scopeConfigMock
98+
->expects($this->once())
99+
->method('getValue')
100+
->with('oauth/access_token_lifetime/customer')
101+
->will($this->returnValue(null));
102+
$this->assertEquals(0, $this->_dataHelper->getCustomerTokenLifetime());
103+
}
104+
105+
public function testGetAdminTokenLifetimeNotEmpty()
106+
{
107+
$this->_scopeConfigMock
108+
->expects($this->once())
109+
->method('getValue')
110+
->with('oauth/access_token_lifetime/admin')
111+
->will($this->returnValue(10));
112+
$this->assertEquals(10, $this->_dataHelper->getAdminTokenLifetime());
113+
}
114+
115+
public function testGetAdminTokenLifetimeEmpty()
116+
{
117+
$this->_scopeConfigMock
118+
->expects($this->once())
119+
->method('getValue')
120+
->with('oauth/access_token_lifetime/admin')
121+
->will($this->returnValue(null));
122+
$this->assertEquals(0, $this->_dataHelper->getAdminTokenLifetime());
123+
}
84124
}

app/code/Magento/Integration/etc/adminhtml/system.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
<label>OAuth</label>
1212
<tab>service</tab>
1313
<resource>Magento_Integration::config_oauth</resource>
14+
<group id="access_token_lifetime" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
15+
<label>Access Token Expiration</label>
16+
<field id="customer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
17+
<label>Customer Token Lifetime (hours)</label>
18+
<comment>We will disable this feature if the value is empty.</comment>
19+
</field>
20+
<field id="admin" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
21+
<label>Admin Token Lifetime (hours)</label>
22+
<comment>We will disable this feature if the value is empty.</comment>
23+
</field>
24+
</group>
1425
<group id="cleanup" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="0" showInStore="0">
1526
<label>Cleanup Settings</label>
1627
<field id="cleanup_probability" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<max_failures_count>6</max_failures_count>
2222
<timeout>1800</timeout>
2323
</authentication_lock>
24+
<access_token_lifetime>
25+
<customer>1</customer>
26+
<admin>4</admin>
27+
</access_token_lifetime>
2428
</oauth>
2529
</default>
2630
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
<job name="outdated_authentication_failures_cleanup" instance="Magento\Integration\Cron\CleanExpiredAuthenticationFailures" method="execute">
1111
<schedule>* * * * *</schedule>
1212
</job>
13+
<job name="expired_tokens_cleanup" instance="Magento\Integration\Cron\CleanExpiredTokens" method="execute">
14+
<schedule>0 * * * *</schedule>
15+
</job>
1316
</group>
1417
</config>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Persistent\Model\Checkout;
8+
9+
use Magento\Checkout\Model\GuestPaymentInformationManagement;
10+
use Magento\Checkout\Model\Session;
11+
12+
/**
13+
* Plugin to convert shopping cart from persistent cart to guest cart before order save when customer not logged in
14+
*/
15+
class GuestPaymentInformationManagementPlugin
16+
{
17+
/**
18+
* Persistence Session Helper
19+
*
20+
* @var \Magento\Persistent\Helper\Session
21+
*/
22+
private $persistenceSessionHelper;
23+
24+
/**
25+
* Persistence Data Helper
26+
*
27+
* @var \Magento\Persistent\Helper\Data
28+
*/
29+
private $persistenceDataHelper;
30+
31+
/**
32+
* Customer Session
33+
*
34+
* @var \Magento\Customer\Model\Session
35+
*/
36+
private $customerSession;
37+
38+
/**
39+
* Checkout Session
40+
*
41+
* @var \Magento\Checkout\Model\Session
42+
*/
43+
private $checkoutSession;
44+
45+
/**
46+
* Quote Manager
47+
*
48+
* @var \Magento\Persistent\Model\QuoteManager
49+
*/
50+
private $quoteManager;
51+
52+
/**
53+
* Cart Repository
54+
*
55+
* @var \Magento\Quote\Api\CartRepositoryInterface
56+
*/
57+
private $cartRepository;
58+
59+
/**
60+
* Initialize dependencies
61+
*
62+
* @param \Magento\Persistent\Helper\Data $persistenceDataHelper
63+
* @param \Magento\Persistent\Helper\Session $persistenceSessionHelper
64+
* @param \Magento\Customer\Model\Session $customerSession
65+
* @param \Magento\Checkout\Model\Session $checkoutSession
66+
* @param \Magento\Persistent\Model\QuoteManager $quoteManager
67+
* @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository
68+
*/
69+
public function __construct(
70+
\Magento\Persistent\Helper\Data $persistenceDataHelper,
71+
\Magento\Persistent\Helper\Session $persistenceSessionHelper,
72+
\Magento\Customer\Model\Session $customerSession,
73+
\Magento\Checkout\Model\Session $checkoutSession,
74+
\Magento\Persistent\Model\QuoteManager $quoteManager,
75+
\Magento\Quote\Api\CartRepositoryInterface $cartRepository
76+
) {
77+
$this->persistenceDataHelper = $persistenceDataHelper;
78+
$this->persistenceSessionHelper = $persistenceSessionHelper;
79+
$this->customerSession = $customerSession;
80+
$this->checkoutSession = $checkoutSession;
81+
$this->quoteManager = $quoteManager;
82+
$this->cartRepository = $cartRepository;
83+
}
84+
85+
/**
86+
* Convert customer cart to guest cart before order is placed if customer is not logged in
87+
*
88+
* @param GuestPaymentInformationManagement $subject
89+
* @param string $cartId
90+
* @param string $email
91+
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
92+
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
93+
* @return void
94+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
95+
*/
96+
public function beforeSavePaymentInformationAndPlaceOrder(
97+
GuestPaymentInformationManagement $subject,
98+
$cartId,
99+
$email,
100+
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
101+
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
102+
) {
103+
if ($this->persistenceSessionHelper->isPersistent()
104+
&& !$this->customerSession->isLoggedIn()
105+
&& $this->persistenceDataHelper->isShoppingCartPersist()
106+
&& $this->quoteManager->isPersistent()
107+
) {
108+
$this->customerSession->setCustomerId(null);
109+
$this->customerSession->setCustomerGroupId(null);
110+
$this->quoteManager->convertCustomerCartToGuest();
111+
/** @var \Magento\Quote\Api\Data\CartInterface $quote */
112+
$quote = $this->cartRepository->get($this->checkoutSession->getQuote()->getId());
113+
$quote->setCustomerEmail($email);
114+
$quote->getAddressesCollection()->walk('setEmail', ['email' => $email]);
115+
$this->cartRepository->save($quote);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)