Skip to content

Commit 3854187

Browse files
committed
B2B-2204: Implement Toggle For Enabling/Disabling Session Support for GraphQL
1 parent 76ca822 commit 3854187

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Model\Config;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* Config for catalog media
14+
*/
15+
class DisableSession
16+
{
17+
private const XML_PATH_GRAPHQL_DISABLE_SESSION = 'graphql/session/disable';
18+
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private $scopeConfig;
23+
24+
/**
25+
* @param ScopeConfigInterface $scopeConfig
26+
*/
27+
public function __construct(ScopeConfigInterface $scopeConfig)
28+
{
29+
$this->scopeConfig = $scopeConfig;
30+
}
31+
32+
/**
33+
* Get config value is session disabled for grapqhl area.
34+
*
35+
* @param string $scopeType
36+
* @param null|int|string $scopeCode
37+
* @return bool
38+
*/
39+
public function isDisabled($scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null): bool
40+
{
41+
$value = $this->scopeConfig->getValue(
42+
self::XML_PATH_GRAPHQL_DISABLE_SESSION,
43+
$scopeType,
44+
$scopeCode
45+
);
46+
47+
if ($value === null) {
48+
return false;
49+
}
50+
51+
return (bool)$value;
52+
}
53+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Plugin;
9+
10+
use Magento\Framework\App\Area;
11+
use Magento\Framework\App\State;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Session\SessionStartChecker;
14+
use Magento\GraphQl\Model\Config\DisableSession as DisableSessionConfig;
15+
16+
/**
17+
* Disable session in graphql area if configured.
18+
*/
19+
class DisableSession
20+
{
21+
/**
22+
* @var DisableSessionConfig
23+
*/
24+
private $disableSessionConfig;
25+
26+
/**
27+
* @var State
28+
*/
29+
private $appState;
30+
31+
/**
32+
* @param DisableSessionConfig $disableSessionConfig
33+
* @param State $appState
34+
*/
35+
public function __construct(
36+
DisableSessionConfig $disableSessionConfig,
37+
State $appState
38+
) {
39+
$this->disableSessionConfig = $disableSessionConfig;
40+
$this->appState = $appState;
41+
}
42+
43+
/**
44+
* Prevents session starting while in graphql area and session is disabled in config.
45+
* Returns true if session is allowed to start and false to avoid starting the session.
46+
* @see \Magento\Framework\Session\SessionStartChecker::check
47+
*
48+
* @param SessionStartChecker $subject
49+
* @param bool $result
50+
* @return bool
51+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
52+
*/
53+
public function afterCheck(SessionStartChecker $subject, bool $result): bool
54+
{
55+
try {
56+
if (!$result ||
57+
($this->appState->getAreaCode() === Area::AREA_GRAPHQL && $this->disableSessionConfig->isDisabled())
58+
) {
59+
return false;
60+
}
61+
} catch (LocalizedException $e) {
62+
// If area code is not set.
63+
} finally {
64+
return $result;
65+
}
66+
}
67+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
</depends>
2727
</field>
2828
</group>
29+
<group id="graphql_session" translate="label" type="text" sortOrder="20" showInDefault="0" showInWebsite="0" showInStore="0">
30+
<label>GraphQl Session Management</label>
31+
<field id="disabled" translate="label" type="select" sortOrder="20" showInDefault="0" showInWebsite="0" showInStore="0">
32+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
33+
<label>Disable session</label>
34+
<config_path>graphql/session/disable</config_path>
35+
</field>
36+
</group>
2937
</section>
3038
</system>
3139
</config>

app/code/Magento/GraphQl/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@
3838
</argument>
3939
</arguments>
4040
</type>
41+
<type name="Magento\Framework\Session\SessionStartChecker">
42+
<plugin name="graphql_session_disable" type="Magento\GraphQl\Plugin\DisableSession"/>
43+
</type>
4144
</config>

0 commit comments

Comments
 (0)