Skip to content

Commit

Permalink
[All] Use our Security class so we don't rely on symfony/security-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Dec 20, 2023
1 parent 6a57c2c commit fcb78db
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration;

use Draw\Component\Security\Core\Security;
use Draw\Component\Workflow\EventListener\AddTransitionNameToContextListener;
use Draw\Component\Workflow\EventListener\AddUserToContextListener;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Security\Core\Security;

class WorkflowIntegration implements IntegrationInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Draw\Component\Security\Core\Authorization\Voter\AbstainRoleHierarchyVoter;
use Draw\Component\Security\Core\EventListener\SystemConsoleAuthenticatorListener;
use Draw\Component\Security\Core\EventListener\SystemMessengerAuthenticatorListener;
use Draw\Component\Security\Core\Security;
use Draw\Component\Security\Http\EventListener\RoleRestrictedAuthenticatorListener;
use Draw\Component\Security\Jwt\JwtEncoder;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -68,6 +69,12 @@ public static function provideTestLoad(): iterable
RoleRestrictedAuthenticatorListener::class,
]
),
new ServiceConfiguration(
'draw.security.core.security',
[
Security::class,
]
),
];

yield 'default' => [
Expand Down
32 changes: 32 additions & 0 deletions packages/security/Core/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Draw\Component\Security\Core;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class Security
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private AuthorizationCheckerInterface $authorizationChecker
) {
}

public function getUser(): ?UserInterface
{
return $this->getToken()?->getUser();
}

public function isGranted(mixed $attributes, mixed $subject = null): bool
{
return $this->authorizationChecker->isGranted($attributes, $subject);
}

public function getToken(): ?TokenInterface
{
return $this->tokenStorage->getToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Draw\Component\Security\Http\Authenticator;

use Draw\Component\Messenger\Searchable\EnvelopeFinder;
use Draw\Component\Security\Core\Security;
use Draw\Component\Security\Http\Message\AutoConnectInterface;
use Draw\Contracts\Messenger\Exception\MessageNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Component\Security\Tests\Http\Authenticator;

use Draw\Component\Messenger\Searchable\EnvelopeFinder;
use Draw\Component\Security\Core\Security;
use Draw\Component\Security\Http\Authenticator\MessageAuthenticator;
use Draw\Component\Security\Http\Message\AutoConnectInterface;
use Draw\Component\Tester\MockTrait;
Expand All @@ -14,7 +15,6 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Draw\Bundle\SonataExtraBundle\EventListener;

use Draw\Component\Security\Core\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;

class SessionTimeoutRequestListener implements EventSubscriberInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use Doctrine\Persistence\ManagerRegistry;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDelete;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDeleteRelationLoader;
use Draw\Component\Security\Core\Security;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\Security\Core\Security;

#[AutoconfigureTag(
'sonata.admin.extension',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Bundle\SonataExtraBundle\Tests\EventListener;

use Draw\Bundle\SonataExtraBundle\EventListener\SessionTimeoutRequestListener;
use Draw\Component\Security\Core\Security;
use Draw\Component\Tester\MockTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -20,7 +21,6 @@
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;

#[CoversClass(SessionTimeoutRequestListener::class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Draw\Bundle\SonataExtraBundle\PreventDelete\Extension\PreventDeleteExtension;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDeleteRelationLoader;
use Draw\Component\Core\Reflection\ReflectionAccessor;
use Draw\Component\Security\Core\Security;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Security\Core\Security;

class PreventDeleteExtensionTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions packages/sonata-extra-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"require-dev": {
"draw/tester": "^0.10",
"draw/security": "^0.10",
"phpunit/phpunit": "^9.0 || ^10.0",
"sonata-project/admin-bundle": "^4.8",
"sonata-project/doctrine-orm-admin-bundle": "^4.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Draw\Bundle\SonataIntegrationBundle\Tests\User\Action;

use Draw\Bundle\SonataIntegrationBundle\User\Action\TwoFactorAuthenticationResendCodeAction;
use Draw\Component\Security\Core\Security;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;

#[CoversClass(TwoFactorAuthenticationResendCodeAction::class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Draw\Bundle\SonataIntegrationBundle\User\Action;

use Draw\Component\Security\Core\Security;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;

class TwoFactorAuthenticationResendCodeAction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Draw\Bundle\SonataIntegrationBundle\User\Extension;

use Draw\Bundle\SonataIntegrationBundle\User\Controller\TwoFactorAuthenticationController;
use Draw\Component\Security\Core\Security;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Security\Core\Security;

class TwoFactorAuthenticationExtension extends AbstractAdminExtension
{
Expand Down
1 change: 1 addition & 0 deletions packages/sonata-integration-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"draw/application": "^0.10",
"draw/console": "^0.10",
"draw/messenger": "^0.10",
"draw/security": "^0.10",
"draw/tester": "^0.10",
"draw/user-bundle": "^0.10",
"firebase/php-jwt": "^6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Draw\Bundle\UserBundle\Entity\SecurityUserInterface;
use Draw\Bundle\UserBundle\Event\UserRequestInterceptionEvent;
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\ByTimeBaseOneTimePasswordInterface;
use Draw\Component\Security\Core\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;

class TwoFactorAuthenticationListener implements EventSubscriberInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Draw\Bundle\UserBundle\Event\UserRequestInterceptedEvent;
use Draw\Bundle\UserBundle\Event\UserRequestInterceptionEvent;
use Draw\Component\Security\Core\Security;
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class UserRequestInterceptorListener implements EventSubscriberInterface
Expand Down
2 changes: 1 addition & 1 deletion packages/user-bundle/Feed/FlashUserFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Draw\Bundle\UserBundle\Feed;

use Draw\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\ByTimeBaseOneTimePasswordTrait;
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\ConfigurationTrait;
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\TwoFactorAuthenticationUserInterface;
use Draw\Component\Security\Core\Security;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;

class TwoFactorAuthenticationListenerTest extends TestCase
Expand Down
1 change: 1 addition & 0 deletions packages/user-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"draw/framework-extra-bundle": "^0.10",
"draw/mailer": "^0.10",
"draw/messenger": "^0.10",
"draw/security": "^0.10",
"draw/tester": "^0.10",
"endroid/qr-code": "^4.8",
"firebase/php-jwt": "^6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Draw\Component\Workflow\EventListener;

use Draw\Component\Security\Core\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\TransitionEvent;

class AddUserToContextListener implements EventSubscriberInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Draw\Component\Workflow\Tests\EventListener;

use Draw\Component\Security\Core\Security;
use Draw\Component\Workflow\EventListener\AddUserToContextListener;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Marking;
Expand Down
1 change: 1 addition & 0 deletions packages/workflow/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"symfony/workflow": "^6.4.0"
},
"require-dev": {
"draw/security": "^0.10",
"phpunit/phpunit": "^9.0 || ^10.0",
"symfony/security-core": "^6.4.0"
},
Expand Down

0 comments on commit fcb78db

Please sign in to comment.