Skip to content

Commit 67e77fe

Browse files
author
He, Joan(johe)
committed
Merge pull request #421 from magento-extensibility/ext_pr
[Extensibility] public PRs and bug fixes
2 parents ea578b7 + bfdcc6d commit 67e77fe

File tree

71 files changed

+963
-505
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+963
-505
lines changed

app/code/Magento/Backend/Model/Auth/Session.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,13 @@ public function isLoggedIn()
171171
}
172172

173173
/**
174-
* Set session UpdatedAt to current time and update cookie expiration time
174+
* Set session UpdatedAt to current time
175175
*
176176
* @return void
177177
*/
178178
public function prolong()
179179
{
180-
$lifetime = $this->_config->getValue(self::XML_PATH_SESSION_LIFETIME);
181-
$currentTime = time();
182-
183-
$this->setUpdatedAt($currentTime);
184-
$cookieValue = $this->cookieManager->getCookie($this->getName());
185-
if ($cookieValue) {
186-
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
187-
->setDuration($lifetime)
188-
->setPath($this->sessionConfig->getCookiePath())
189-
->setDomain($this->sessionConfig->getCookieDomain())
190-
->setSecure($this->sessionConfig->getCookieSecure())
191-
->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
192-
$this->cookieManager->setPublicCookie($this->getName(), $cookieValue, $cookieMetadata);
193-
}
180+
$this->setUpdatedAt(time());
194181
}
195182

196183
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Model\Config\SessionLifetime;
7+
8+
use Magento\Framework\App\Config\Value;
9+
use Magento\Framework\Exception\LocalizedException;
10+
11+
/**
12+
* Backend model for the admin/security/session_lifetime configuration field. Validates session lifetime.
13+
*/
14+
class BackendModel extends Value
15+
{
16+
/** Maximum dmin session lifetime; 1 year*/
17+
const MAX_LIFETIME = 31536000;
18+
19+
/** Minimum admin session lifetime */
20+
const MIN_LIFETIME = 60;
21+
22+
public function beforeSave()
23+
{
24+
$value = (int) $this->getValue();
25+
if ($value > self::MAX_LIFETIME) {
26+
throw new LocalizedException(
27+
__('Admin session lifetime must be less than or equal to 31536000 seconds (one year)')
28+
);
29+
} else if ($value < self::MIN_LIFETIME) {
30+
throw new LocalizedException(
31+
__('Admin session lifetime must be greater than or equal to 60 seconds')
32+
);
33+
}
34+
return parent::beforeSave();
35+
}
36+
}

app/code/Magento/Backend/Model/Session/AdminConfig.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
/**
1616
* Magento Backend session configuration
17-
*
18-
* @method Config setSaveHandler()
1917
*/
2018
class AdminConfig extends Config
2119
{
@@ -107,4 +105,14 @@ private function extractAdminPath()
107105
$cookiePath = $baseUrl . $backendApp->getCookiePath();
108106
return $cookiePath;
109107
}
108+
109+
/**
110+
* Set session cookie lifetime to session duration
111+
*
112+
* @return $this
113+
*/
114+
protected function configureCookieLifetime()
115+
{
116+
return $this->setCookieLifetime(0);
117+
}
110118
}

app/code/Magento/Backend/Test/Unit/Model/Auth/SessionTest.php

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -162,67 +162,7 @@ public function testIsLoggedInPositive()
162162

163163
public function testProlong()
164164
{
165-
$name = session_name();
166-
$cookie = 'cookie';
167-
$lifetime = 900;
168-
$path = '/';
169-
$domain = 'magento2';
170-
$secure = true;
171-
$httpOnly = true;
172-
173-
$cookieMetadata = $this->getMock('Magento\Framework\Stdlib\Cookie\PublicCookieMetadata');
174-
$cookieMetadata->expects($this->once())
175-
->method('setDuration')
176-
->with($lifetime)
177-
->will($this->returnSelf());
178-
$cookieMetadata->expects($this->once())
179-
->method('setPath')
180-
->with($path)
181-
->will($this->returnSelf());
182-
$cookieMetadata->expects($this->once())
183-
->method('setDomain')
184-
->with($domain)
185-
->will($this->returnSelf());
186-
$cookieMetadata->expects($this->once())
187-
->method('setSecure')
188-
->with($secure)
189-
->will($this->returnSelf());
190-
$cookieMetadata->expects($this->once())
191-
->method('setHttpOnly')
192-
->with($httpOnly)
193-
->will($this->returnSelf());
194-
195-
$this->cookieMetadataFactory->expects($this->once())
196-
->method('createPublicCookieMetadata')
197-
->will($this->returnValue($cookieMetadata));
198-
199-
$this->cookieManager->expects($this->once())
200-
->method('getCookie')
201-
->with($name)
202-
->will($this->returnValue($cookie));
203-
$this->cookieManager->expects($this->once())
204-
->method('setPublicCookie')
205-
->with($name, $cookie, $cookieMetadata);
206-
207-
$this->config->expects($this->once())
208-
->method('getValue')
209-
->with(\Magento\Backend\Model\Auth\Session::XML_PATH_SESSION_LIFETIME)
210-
->will($this->returnValue($lifetime));
211-
$this->sessionConfig->expects($this->once())
212-
->method('getCookiePath')
213-
->will($this->returnValue($path));
214-
$this->sessionConfig->expects($this->once())
215-
->method('getCookieDomain')
216-
->will($this->returnValue($domain));
217-
$this->sessionConfig->expects($this->once())
218-
->method('getCookieSecure')
219-
->will($this->returnValue($secure));
220-
$this->sessionConfig->expects($this->once())
221-
->method('getCookieHttpOnly')
222-
->will($this->returnValue($httpOnly));
223-
224165
$this->session->prolong();
225-
226166
$this->assertLessThanOrEqual(time(), $this->session->getUpdatedAt());
227167
}
228168

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Test\Unit\Model\Config\SessionLifetime;
7+
8+
use Magento\Backend\Model\Config\SessionLifetime\BackendModel;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
11+
class BackendModelTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @dataProvider adminSessionLifetimeDataProvider
15+
*/
16+
public function testBeforeSave($value, $errorMessage = null)
17+
{
18+
/** @var BackendModel $model */
19+
$model = (new ObjectManager($this))->getObject('Magento\Backend\Model\Config\SessionLifetime\BackendModel');
20+
if ($errorMessage !== null) {
21+
$this->setExpectedException('\Magento\Framework\Exception\LocalizedException', $errorMessage);
22+
}
23+
$model->setValue($value);
24+
$model->beforeSave();
25+
}
26+
27+
public function adminSessionLifetimeDataProvider()
28+
{
29+
return [
30+
[
31+
BackendModel::MIN_LIFETIME - 1,
32+
'Admin session lifetime must be greater than or equal to 60 seconds'
33+
],
34+
[
35+
BackendModel::MAX_LIFETIME + 1,
36+
'Admin session lifetime must be less than or equal to 31536000 seconds (one year)'
37+
],
38+
[
39+
900
40+
]
41+
];
42+
}
43+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@
388388
</field>
389389
<field id="session_lifetime" translate="label comment" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
390390
<label>Admin Session Lifetime (seconds)</label>
391-
<comment>Values less than 60 are ignored.</comment>
391+
<comment>Please enter at least 60 and at most 31536000 (one year).</comment>
392+
<backend_model>Magento\Backend\Model\Config\SessionLifetime\BackendModel</backend_model>
392393
<validate>validate-digits</validate>
393394
</field>
394395
</group>

app/code/Magento/Backend/i18n/en_US.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ Security,Security
556556
"Add Secret Key to URLs","Add Secret Key to URLs"
557557
"Login is Case Sensitive","Login is Case Sensitive"
558558
"Admin Session Lifetime (seconds)","Admin Session Lifetime (seconds)"
559-
"Values less than 60 are ignored.","Values less than 60 are ignored."
559+
"Please enter at least 60 and at most 31536000 (one year).","Please enter at least 60 and at most 31536000 (one year)."
560+
"Admin session lifetime must be less than or equal to 31536000 seconds (one year)","Admin session lifetime must be less than or equal to 31536000 seconds (one year)"
561+
"Admin session lifetime must be greater than or equal to 60 seconds","Admin session lifetime must be greater than or equal to 60 seconds"
560562
Web,Web
561563
"Url Options","Url Options"
562564
"Add Store Code to Urls","Add Store Code to Urls"

app/code/Magento/Config/Block/System/Config/Form/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected function _renderHint(\Magento\Framework\Data\Form\Element\AbstractElem
180180
* @param string $html
181181
* @return string
182182
*/
183-
protected function _decorateRowHtml($element, $html)
183+
protected function _decorateRowHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element, $html)
184184
{
185185
return '<tr id="row_' . $element->getHtmlId() . '">' . $html . '</tr>';
186186
}

app/code/Magento/Config/Test/Unit/Block/System/Config/Form/Field/NotificationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public function testRender()
2020

2121
/** @var \Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface $dateTimeFormatter */
2222
$dateTimeFormatter = $objectManager->getObject('Magento\Framework\Stdlib\DateTime\DateTimeFormatter');
23+
$localeResolver = $objectManager->getObject('Magento\Framework\Locale\Resolver');
24+
25+
$reflection = new \ReflectionClass('Magento\Framework\Stdlib\DateTime\DateTimeFormatter');
26+
$reflectionProperty = $reflection->getProperty('localeResolver');
27+
$reflectionProperty->setAccessible(true);
28+
$reflectionProperty->setValue($dateTimeFormatter, $localeResolver);
29+
2330
$formattedDate = $dateTimeFormatter->formatObject($testDatetime);
2431

2532
$htmlId = 'test_HTML_id';

app/code/Magento/Config/etc/system.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<xs:element name="header_css" type="xs:string" />
9696
<xs:element name="resource" type="typeAclResourceId" />
9797
<xs:element ref="group" />
98+
<xs:element name="frontend_model" type="typeModel" />
9899
</xs:choice>
99100
</xs:sequence>
100101
<xs:attributeGroup ref="elementsAttributeGroup"/>

0 commit comments

Comments
 (0)