diff --git a/composer.json b/composer.json index b80a8eb6..43c33aad 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "symfony/dependency-injection": "^5.4|^6.0", "symfony/http-kernel": "^5.4|^6.0", "symfony/security-core": "^5.4|^6.0", + "symfony/security-bundle": "^5.4|^6.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/string": "^5.4|^6.0", "symfony/translation-contracts": "^2.4|^3.0", diff --git a/src/Provider/UserProvider.php b/src/Provider/UserProvider.php index 5811cabd..02cc761f 100644 --- a/src/Provider/UserProvider.php +++ b/src/Provider/UserProvider.php @@ -4,8 +4,9 @@ namespace Knp\DoctrineBehaviors\Provider; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Knp\DoctrineBehaviors\Contract\Provider\UserProviderInterface; -use Symfony\Component\Security\Core\Security; +use Knp\DoctrineBehaviors\Security\Security; final class UserProvider implements UserProviderInterface { @@ -18,7 +19,7 @@ public function __construct( public function provideUser() { $token = $this->security->getToken(); - if ($token !== null) { + if ($token instanceof TokenInterface) { $user = $token->getUser(); if ($this->blameableUserEntity) { if ($user instanceof $this->blameableUserEntity) { diff --git a/src/Security/Security.php b/src/Security/Security.php new file mode 100644 index 00000000..c4d63ba2 --- /dev/null +++ b/src/Security/Security.php @@ -0,0 +1,37 @@ +security->getToken(); + } + } +} elseif (class_exists(SecurityCore::class)) { + final class Security { + public function __construct( + private SecurityCore $security, + ) { + } + + public function getToken(): ?TokenInterface + { + return $this->security->getToken(); + } + } +} else { + throw new \LogicException(sprintf('You cannot use "%s" because either the Symfony Security Bundle or Symfony Security Core is not installed. If you use Symfony 6.2+, try running "composer require symfony/security-bundle", otherwise run "composer require symfony/security-core".', __CLASS__)); +} diff --git a/tests/config/config_test.php b/tests/config/config_test.php index 7a20bf2e..f1a93ed1 100644 --- a/tests/config/config_test.php +++ b/tests/config/config_test.php @@ -8,9 +8,9 @@ use Knp\DoctrineBehaviors\Tests\DatabaseLoader; use Knp\DoctrineBehaviors\Tests\Provider\TestLocaleProvider; use Knp\DoctrineBehaviors\Tests\Provider\TestUserProvider; +use Knp\DoctrineBehaviors\Security\Security; use Psr\Log\Test\TestLogger; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symfony\Component\Security\Core\Security; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $containerConfigurator): void {