Skip to content

Commit

Permalink
IBX-8811: Rebranded SiteAccess session prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
konradoboza committed Aug 29, 2024
1 parent 7e4312d commit 27226cf
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 149 deletions.
85 changes: 0 additions & 85 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2140,11 +2140,6 @@ parameters:
count: 1
path: src/bundle/Core/EventListener/SessionInitByPostListener.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListener\\:\\:onSiteAccessMatch\\(\\) has no return type specified\\.$#"
count: 1
path: src/bundle/Core/EventListener/SessionSetDynamicNameListener.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Core\\\\EventListener\\\\SiteAccessListener\\:\\:generateViewParametersArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -24460,46 +24455,6 @@ parameters:
count: 2
path: tests/bundle/Core/EventListener/RoutingListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionInitByPostListenerTest\\:\\:testGetSubscribedEvents\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionInitByPostListenerTest\\:\\:testOnSiteAccessMatchNewSessionName\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionInitByPostListenerTest\\:\\:testOnSiteAccessMatchNoSessionService\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionInitByPostListenerTest\\:\\:testOnSiteAccessMatchRequestNoSessionName\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionInitByPostListenerTest\\:\\:testOnSiteAccessMatchSubRequest\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:onSiteAccessMatchProvider\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testGetSubscribedEvents\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatch\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatch\\(\\) has parameter \\$configuredSessionStorageOptions with no type specified\\.$#"
count: 1
Expand All @@ -24510,36 +24465,6 @@ parameters:
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatchNoConfiguredSessionName\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatchNoSession\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatchNonNativeSessionStorage\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListenerTest\\:\\:testOnSiteAccessMatchSubRequest\\(\\) has no return type specified\\.$#"
count: 1
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Parameter \\#1 \\$configResolver of class Ibexa\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListener constructor expects Ibexa\\\\Contracts\\\\Core\\\\SiteAccess\\\\ConfigResolverInterface, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
count: 6
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Parameter \\#2 \\$sessionStorageFactory of class Ibexa\\\\Bundle\\\\Core\\\\EventListener\\\\SessionSetDynamicNameListener constructor expects Symfony\\\\Component\\\\HttpFoundation\\\\Session\\\\Storage\\\\SessionStorageFactoryInterface, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
count: 5
path: tests/bundle/Core/EventListener/SessionSetDynamicNameListenerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\SiteAccessListenerTest\\:\\:siteAccessMatchProvider\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -44720,16 +44645,6 @@ parameters:
count: 1
path: tests/lib/Limitation/SubtreeLimitationTypeTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Component\\\\Serializer\\\\SimplifiedRequestNormalizerTest\\:\\:testNormalize\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Component/Serializer/SimplifiedRequestNormalizerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Component\\\\Serializer\\\\SimplifiedRequestNormalizerTest\\:\\:testSupportsNormalization\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Component/Serializer/SimplifiedRequestNormalizerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Component\\\\Serializer\\\\Stubs\\\\CompoundStub\\:\\:__construct\\(\\) has parameter \\$subMatchers with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder)
->children()
->scalarNode('name')
->info('The session name. If you want a session name per siteaccess, use "{siteaccess_hash}" token. Will override default session name from framework.session.name')
->example('eZSESSID{siteaccess_hash}')
->example('IBX_SESSION_ID{siteaccess_hash}')
->end()
->scalarNode('cookie_lifetime')->end()
->scalarNode('cookie_path')->end()
Expand Down
40 changes: 12 additions & 28 deletions src/bundle/Core/EventListener/SessionSetDynamicNameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Core\EventListener;

Expand All @@ -21,27 +22,16 @@
*
* Allows to set a dynamic session name based on the siteaccess name.
*/
class SessionSetDynamicNameListener implements EventSubscriberInterface
final readonly class SessionSetDynamicNameListener implements EventSubscriberInterface
{
public const MARKER = '{siteaccess_hash}';
public const string MARKER = '{siteaccess_hash}';

/**
* Prefix for session name.
*/
public const SESSION_NAME_PREFIX = 'eZSESSID';

/** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
private $configResolver;

/** @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageFactoryInterface */
private $sessionStorageFactory;
public const string SESSION_NAME_PREFIX = 'IBX_SESSION_ID';

public function __construct(
ConfigResolverInterface $configResolver,
SessionStorageFactoryInterface $sessionStorageFactory
private ConfigResolverInterface $configResolver,
private SessionStorageFactoryInterface $sessionStorageFactory
) {
$this->configResolver = $configResolver;
$this->sessionStorageFactory = $sessionStorageFactory;
}

public static function getSubscribedEvents(): array
Expand All @@ -51,7 +41,7 @@ public static function getSubscribedEvents(): array
];
}

public function onSiteAccessMatch(PostSiteAccessMatchEvent $event)
public function onSiteAccessMatch(PostSiteAccessMatchEvent $event): void
{
$request = $event->getRequest();
$session = $request->hasSession() ? $request->getSession() : null;
Expand All @@ -69,26 +59,20 @@ public function onSiteAccessMatch(PostSiteAccessMatchEvent $event)
}

$sessionOptions = (array)$this->configResolver->getParameter('session');
$sessionName = isset($sessionOptions['name']) ? $sessionOptions['name'] : $session->getName();
$sessionName = $sessionOptions['name'] ?? $session->getName();
$sessionOptions['name'] = $this->getSessionName($sessionName, $event->getSiteAccess());
$sessionStorage->setOptions($sessionOptions);
}

/**
* @param string $sessionName
* @param \Ibexa\Core\MVC\Symfony\SiteAccess $siteAccess
*
* @return string
*/
private function getSessionName($sessionName, SiteAccess $siteAccess)
private function getSessionName(string $sessionName, SiteAccess $siteAccess): string
{
// Add session prefix if needed.
if (strpos($sessionName, static::SESSION_NAME_PREFIX) !== 0) {
$sessionName = static::SESSION_NAME_PREFIX . '_' . $sessionName;
if (!str_starts_with($sessionName, self::SESSION_NAME_PREFIX)) {
$sessionName = self::SESSION_NAME_PREFIX . '_' . $sessionName;
}

// Check if uniqueness marker is present. If so, session name will be unique for current siteaccess.
if (strpos($sessionName, self::MARKER) !== false) {
if (str_contains($sessionName, self::MARKER)) {
$sessionName = str_replace(self::MARKER, md5($siteAccess->name), $sessionName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Core/Resources/config/default_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ parameters:
# Common settings
ibexa.repositories: {}
ibexa.site_access.config.default.repository: ~
ibexa.session_name.default: "eZSESSID{siteaccess_hash}"
ibexa.session_name.default: "IBX_SESSION_ID{siteaccess_hash}"
ibexa.site_access.config.default.session_name: '%ibexa.session_name.default%' # Using "{siteaccess_hash}" in session name makes it unique per siteaccess
ibexa.site_access.config.default.session: { name: '%ibexa.session_name.default%' } # Session options that will override options from framework
ibexa.site_access.config.default.url_alias_router: true # Use UrlAliasRouter by default
Expand Down
20 changes: 10 additions & 10 deletions tests/bundle/Core/EventListener/SessionInitByPostListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\Core\EventListener;

Expand All @@ -18,18 +19,17 @@
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class SessionInitByPostListenerTest extends TestCase
final class SessionInitByPostListenerTest extends TestCase
{
/** @var \Ibexa\Bundle\Core\EventListener\SessionInitByPostListener */
private $listener;
private SessionInitByPostListener $listener;

protected function setUp(): void
{
parent::setUp();
$this->listener = new SessionInitByPostListener();
}

public function testGetSubscribedEvents()
public function testGetSubscribedEvents(): void
{
self::assertSame(
[
Expand All @@ -39,7 +39,7 @@ public function testGetSubscribedEvents()
);
}

public function testOnSiteAccessMatchNoSessionService()
public function testOnSiteAccessMatchNoSessionService(): void
{
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
Expand All @@ -49,7 +49,7 @@ public function testOnSiteAccessMatchNoSessionService()
self::assertNull($listener->onSiteAccessMatch($event));
}

public function testOnSiteAccessMatchSubRequest()
public function testOnSiteAccessMatchSubRequest(): void
{
$session = $this->createMock(SessionInterface::class);
$session
Expand All @@ -63,9 +63,9 @@ public function testOnSiteAccessMatchSubRequest()
$this->listener->onSiteAccessMatch($event);
}

public function testOnSiteAccessMatchRequestNoSessionName()
public function testOnSiteAccessMatchRequestNoSessionName(): void
{
$sessionName = 'eZSESSID';
$sessionName = 'IBX_SESSION_ID';

$session = $this->createMock(SessionInterface::class);
$session
Expand All @@ -90,9 +90,9 @@ public function testOnSiteAccessMatchRequestNoSessionName()
$this->listener->onSiteAccessMatch($event);
}

public function testOnSiteAccessMatchNewSessionName()
public function testOnSiteAccessMatchNewSessionName(): void
{
$sessionName = 'eZSESSID';
$sessionName = 'IBX_SESSION_ID';
$sessionId = 'foobar123';
$session = $this->createMock(SessionInterface::class);

Expand Down
Loading

0 comments on commit 27226cf

Please sign in to comment.