Skip to content

Commit

Permalink
Use the new actor provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Feb 26, 2025
1 parent bf00701 commit 1415e20
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
"gedmo/doctrine-extensions": "^3.15.0"
"gedmo/doctrine-extensions": "^3.19.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function load(array $configs, ContainerBuilder $container)
$config = $processor->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('tool.xml');

$loaded = array();

Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/BlameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Sets the username from the security context by listening on kernel.request
*
* @author David Buchmann <[email protected]>
*
* @deprecated to be removed in 2.0, use the actor provider instead
*/
class BlameListener implements EventSubscriberInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/LoggerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
* @author Christophe Coevoet <[email protected]>
*
* @deprecated to be removed in 2.0, use the actor provider instead
*
* @phpstan-template T of Loggable|object
*/
class LoggerListener implements EventSubscriberInterface
Expand Down
5 changes: 4 additions & 1 deletion src/Resources/config/blameable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
<call method="setAnnotationReader">
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
<call method="setActorProvider">
<argument type="service" id="stof_doctrine_extensions.tool.actor_provider" />
</call>
</service>

<service id="stof_doctrine_extensions.event_listener.blame" class="%stof_doctrine_extensions.event_listener.blame.class%">
<deprecated package="stof/doctrine-extensions-bundle" version="1.14">The "%service_id%" service is deprecated and will be removed in 2.0. The "stof_doctrine_extensions.tool.actor_provider" service should be used to provide the user instead.</deprecated>
<argument type="service" id="stof_doctrine_extensions.listener.blameable" />
<argument type="service" id="security.token_storage" on-invalid="null" />
<argument type="service" id="security.authorization_checker" on-invalid="null" />
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
5 changes: 4 additions & 1 deletion src/Resources/config/loggable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
<call method="setAnnotationReader">
<argument type="service" id=".stof_doctrine_extensions.reader" on-invalid="ignore" />
</call>
<call method="setActorProvider">
<argument type="service" id="stof_doctrine_extensions.tool.actor_provider" />
</call>
</service>

<service id="stof_doctrine_extensions.event_listener.logger" class="%stof_doctrine_extensions.event_listener.logger.class%">
<deprecated package="stof/doctrine-extensions-bundle" version="1.14">The "%service_id%" service is deprecated and will be removed in 2.0. The "stof_doctrine_extensions.tool.actor_provider" service should be used to provide the user instead.</deprecated>
<argument type="service" id="stof_doctrine_extensions.listener.loggable" />
<argument type="service" id="security.token_storage" on-invalid="null" />
<argument type="service" id="security.authorization_checker" on-invalid="null" />
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
13 changes: 13 additions & 0 deletions src/Resources/config/tool.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="stof_doctrine_extensions.tool.actor_provider" class="Stof\DoctrineExtensionsBundle\Tool\TokenStorageActorProvider" public="false">
<argument type="service" id="security.token_storage" on-invalid="null" />
<argument type="service" id="security.authorization_checker" on-invalid="null" />
</service>
</services>
</container>
40 changes: 40 additions & 0 deletions src/Tool/TokenStorageActorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Stof\DoctrineExtensionsBundle\Tool;

use Gedmo\Tool\ActorProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Provides an actor for the extensions using the token storage.
*
* @internal
*/
final class TokenStorageActorProvider implements ActorProviderInterface
{
private ?TokenStorageInterface $tokenStorage;
private ?AuthorizationCheckerInterface $authorizationChecker;

public function __construct(?TokenStorageInterface $tokenStorage = null, ?AuthorizationCheckerInterface $authorizationChecker = null)
{
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}

public function getActor(): ?UserInterface
{
if (null === $this->tokenStorage || null === $this->authorizationChecker) {
return null;
}

$token = $this->tokenStorage->getToken();

if (null === $token || !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return null;
}

return $token->getUser();
}
}

0 comments on commit 1415e20

Please sign in to comment.