Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SF63 deprecations (security) #5

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/Provider/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions src/Security/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Security;

use Symfony\Bundle\SecurityBundle\Security as SecurityBundle;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security as SecurityCore;

if (class_exists(SecurityBundle::class)) {
final class Security {
public function __construct(
private SecurityBundle $security,
) {
}

public function getToken(): ?TokenInterface
{
return $this->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__));
}
2 changes: 1 addition & 1 deletion tests/config/config_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down