Skip to content

Commit c2a591c

Browse files
author
hwyu@adobe.com
committed
MC-36034: Session size configuration
- Added session size configuration - Added unit test converage
1 parent 336eac0 commit c2a591c

File tree

13 files changed

+491
-2
lines changed

13 files changed

+491
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* System config email field backend model
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace Magento\Security\Block\Config\Backend\Session;
13+
14+
use Magento\Backend\Block\Template\Context;
15+
use Magento\Config\Block\System\Config\Form\Field;
16+
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
use Magento\Framework\Exception\ValidatorException;
18+
use Magento\Framework\Serialize\Serializer\Json;
19+
20+
/**
21+
* Backend Model for Max Session Size
22+
*/
23+
class SessionSize extends Field
24+
{
25+
/**
26+
* @var Json
27+
*/
28+
private $json;
29+
30+
/**
31+
* @param Context $context
32+
* @param Json $json
33+
* @param array $data
34+
*/
35+
public function __construct(
36+
Context $context,
37+
Json $json,
38+
array $data = []
39+
) {
40+
parent::__construct($context, $data);
41+
$this->json = $json;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
* @throws ValidatorException
47+
*/
48+
protected function _getElementHtml(AbstractElement $element)
49+
{
50+
$html = parent::_getElementHtml($element);
51+
$originalData = $element->getOriginalData();
52+
$maxSessionSizeAdminSelector = '#' . $element->getHtmlId();
53+
$jsString = '<script type="text/x-magento-init"> {"' .
54+
$maxSessionSizeAdminSelector . '": {
55+
"Magento_Security/js/system/config/session-size": {"modalTitleText": ' .
56+
$this->json->serialize(__($originalData['modal_title_text'])) . ', "modalContentBody": ' .
57+
$this->json->serialize($this->getModalContentBody($originalData['modal_content_body_path']))
58+
. '}}}</script>';
59+
60+
$html .= $jsString;
61+
return $html;
62+
}
63+
64+
/**
65+
* Get HTML for the modal content body when user switches to disable
66+
*
67+
* @param string $templatePath
68+
* @return string
69+
* @throws ValidatorException
70+
*/
71+
private function getModalContentBody(string $templatePath)
72+
{
73+
$templateFileName = $this->getTemplateFile($templatePath);
74+
75+
return $this->fetchView($templateFileName);
76+
}
77+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* System config email field backend model
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace Magento\Security\Model\Config\Backend\Session;
13+
14+
use Magento\Framework\App\Config\Value;
15+
16+
/**
17+
* Backend Model for Max Session Size
18+
*/
19+
class SessionSize extends Value
20+
{
21+
/**
22+
* @return $this
23+
*/
24+
public function beforeSave()
25+
{
26+
$value = (int)$this->getValue();
27+
28+
if ($value === null || $value < 0) {
29+
$value = 0;
30+
}
31+
$this->setValue((string)$value);
32+
return $this;
33+
}
34+
}

app/code/Magento/Security/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"require": {
88
"php": "~7.3.0||~7.4.0",
99
"magento/framework": "*",
10+
"magento/module-config": "*",
1011
"magento/module-backend": "*",
1112
"magento/module-store": "*",
1213
"magento/module-user": "*"

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@
3636
</field>
3737
</group>
3838
</section>
39+
<section id="system">
40+
<group id="security" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="1">
41+
<label>Security</label>
42+
<field id="max_session_size_admin" translate="label" type="text" sortOrder="1" showInDefault="1" canRestore="1">
43+
<label>Max Session Size in Admin</label>
44+
<attribute type="modal_title_text">Are You Sure About Your Max Session Size in Admin Settings?</attribute>
45+
<attribute type="modal_content_body_path">Magento_Security::system/config/session_size_admin/modal_content_body.phtml</attribute>
46+
<validate>required-entry validate-zero-or-greater validate-digits</validate>
47+
<frontend_model>Magento\Security\Block\Config\Backend\Session\SessionSize</frontend_model>
48+
<backend_model>Magento\Security\Model\Config\Backend\Session\SessionSize</backend_model>
49+
<comment>Limit the maximum session size in bytes. Use 0 to disable.</comment>
50+
</field>
51+
<field id="max_session_size_storefront" translate="label" type="text" sortOrder="2" showInDefault="1" canRestore="1">
52+
<label>Max Session Size in Storefront</label>
53+
<attribute type="modal_title_text">Are You Sure About Your Max Session Size in Storefront Settings?</attribute>
54+
<attribute type="modal_content_body_path">Magento_Security::system/config/session_size_storefront/modal_content_body.phtml</attribute>
55+
<validate>required-entry validate-zero-or-greater validate-digits</validate>
56+
<frontend_model>Magento\Security\Block\Config\Backend\Session\SessionSize</frontend_model>
57+
<backend_model>Magento\Security\Model\Config\Backend\Session\SessionSize</backend_model>
58+
<comment>Limit the maximum session size in bytes. Use 0 to disable.</comment>
59+
</field>
60+
</group>
61+
</section>s
3962
<section id="customer">
4063
<group id="password">
4164
<field id="password_reset_protection_type" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
<session_lifetime>900</session_lifetime>
1717
</security>
1818
</admin>
19+
<system>
20+
<security>
21+
<max_session_size_admin>256000</max_session_size_admin>
22+
<max_session_size_storefront>256000</max_session_size_storefront>
23+
</security>
24+
</system>
1925
<customer>
2026
<password>
2127
<password_reset_protection_type>1</password_reset_protection_type>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
?>
8+
<div>
9+
<p>
10+
<strong><?= $block->escapeHtml(__('Warning')) ?></strong>
11+
<?= $block->escapeHtml(__(': You are about to set max session size in admin to be lower than recommended default session size. Low max session size in admin could break admin functionalities such as admin panel login. Are you sure you want to make this change?')) ?>
12+
</p>
13+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
?>
8+
<div>
9+
<p>
10+
<strong><?= $block->escapeHtml(__('Warning')) ?></strong>
11+
<?= $block->escapeHtml(__(': You are about to set max session size in storefront to be lower than recommended default session size. Low max session size in storefront could break storefront functionalities such as customer login. Are you sure you want to make this change?')) ?>
12+
</p>
13+
</div>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'mage/translate',
9+
'Magento_Ui/js/modal/confirm',
10+
'domReady!'
11+
], function ($, $t, confirm) {
12+
'use strict';
13+
14+
return function (config, inputEl) {
15+
var $inputEl = $(inputEl);
16+
17+
$inputEl.on('blur', function () {
18+
var inputVal = parseInt($inputEl.val(), 10);
19+
20+
if (256000 > inputVal) {
21+
confirm({
22+
title: $t(config.modalTitleText),
23+
content: $t(config.modalContentBody),
24+
buttons: [{
25+
text: $t('No'),
26+
class: 'action-secondary action-dismiss',
27+
28+
/**
29+
* Close modal and trigger 'cancel' action on click
30+
*/
31+
click: function (event) {
32+
this.closeModal(event);
33+
}
34+
}, {
35+
text: $t('Yes'),
36+
class: 'action-primary action-accept',
37+
38+
/**
39+
* Close modal and trigger 'confirm' action on click
40+
*/
41+
click: function (event) {
42+
this.closeModal(event, true);
43+
}
44+
}],
45+
actions: {
46+
47+
/**
48+
* Revert back to original value
49+
*/
50+
cancel: function () {
51+
$inputEl.val(256000);
52+
}
53+
}
54+
});
55+
}
56+
});
57+
};
58+
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@
174174
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
175175
</arguments>
176176
</type>
177+
<type name="Magento\Framework\Session\SessionMaxSizeConfig">
178+
<arguments>
179+
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
180+
</arguments>
181+
</type>
177182
<type name="Magento\Framework\Session\SidResolver">
178183
<arguments>
179184
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>

app/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,11 @@
17671767
<argument name="scopeType" xsi:type="const">Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT</argument>
17681768
</arguments>
17691769
</type>
1770+
<type name="Magento\Framework\Session\SessionMaxSizeConfig">
1771+
<arguments>
1772+
<argument name="scopeType" xsi:type="const">Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT</argument>
1773+
</arguments>
1774+
</type>
17701775
<virtualType name="CsrfRequestValidator" type="Magento\Framework\App\Request\CsrfValidator" />
17711776
<virtualType name="RequestValidator" type="Magento\Framework\App\Request\CompositeValidator">
17721777
<arguments>

0 commit comments

Comments
 (0)