From 63e0453244b69d60e4fc96f5f9b9b964390b522c Mon Sep 17 00:00:00 2001 From: Anton_sz Date: Thu, 7 Dec 2023 00:45:16 +0100 Subject: [PATCH 1/3] Possibility to use service as person_fn rather than a static function --- Factories/RollbarHandlerFactory.php | 27 ++++++++++++--------------- Resources/config/services.yml | 9 ++++++++- Service/PersonProvider.php | 27 +++++++++++++++++++++++++++ Service/PersonProviderInterface.php | 8 ++++++++ composer.json | 3 ++- 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 Service/PersonProvider.php create mode 100644 Service/PersonProviderInterface.php diff --git a/Factories/RollbarHandlerFactory.php b/Factories/RollbarHandlerFactory.php index 34135fa..660542c 100755 --- a/Factories/RollbarHandlerFactory.php +++ b/Factories/RollbarHandlerFactory.php @@ -5,16 +5,15 @@ use Psr\Log\LogLevel; use Monolog\Handler\RollbarHandler; use Rollbar\Rollbar; -use Rollbar\Symfony\RollbarBundle\DependencyInjection\RollbarExtension; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Rollbar\Symfony\RollbarBundle\Service\PersonProviderInterface; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Serializer\SerializerInterface; +use Throwable; class RollbarHandlerFactory { - public function __construct(ContainerInterface $container) + public function __construct(array $config, SerializerInterface $serializer, PersonProviderInterface $personProvider) { - $config = $container->getParameter(RollbarExtension::ALIAS . '.config'); - if (isset($_ENV['ROLLBAR_TEST_TOKEN']) && $_ENV['ROLLBAR_TEST_TOKEN']) { $config['access_token'] = $_ENV['ROLLBAR_TEST_TOKEN']; } @@ -22,18 +21,16 @@ public function __construct(ContainerInterface $container) if (!empty($config['person_fn']) && is_callable($config['person_fn'])) { $config['person'] = null; } elseif (empty($config['person'])) { - $config['person_fn'] = static function () use ($container) { + $config['person_fn'] = static function () use ($personProvider, $serializer) { try { - $token = $container->get('security.token_storage')->getToken(); - - if ($token) { - $user = $token->getUser(); - $serializer = $container->get('serializer'); - return \json_decode($serializer->serialize($user, 'json'), true, 512, JSON_THROW_ON_ERROR); - } - } catch (\Throwable $exception) { - // Ignore + $person = call_user_func([$personProvider, 'getUserInfo']); + + return \json_decode($serializer->serialize($person, 'json'), true, 512, JSON_THROW_ON_ERROR); + } catch (Throwable) { + // do nothing } + + return []; }; } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 37f1c65..85ff9b9 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,8 +1,15 @@ services: + rollbar.person-provider: + class: Rollbar\Symfony\RollbarBundle\Service\PersonProvider + arguments: + - '@security.token_storage' + - '@serializer.normalizer.object' Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory: arguments: - - '@service_container' + - '%rollbar.config%' + - '@serializer' + - '@rollbar.person-provider' Rollbar\Monolog\Handler\RollbarHandler: factory: ['@Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory', createRollbarHandler] diff --git a/Service/PersonProvider.php b/Service/PersonProvider.php new file mode 100644 index 0000000..5151f2b --- /dev/null +++ b/Service/PersonProvider.php @@ -0,0 +1,27 @@ +tokenStorage->getToken(); + + if ($token) { + $user = $token->getUser(); + + return $this->normalizer->normalize($user, 'json'); + } + + return []; + } +} diff --git a/Service/PersonProviderInterface.php b/Service/PersonProviderInterface.php new file mode 100644 index 0000000..0859447 --- /dev/null +++ b/Service/PersonProviderInterface.php @@ -0,0 +1,8 @@ + Date: Sat, 9 Mar 2024 20:46:30 +0100 Subject: [PATCH 2/3] Fix composer json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index bfe5114..44b88c9 100755 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ "symfony/http-kernel": "^5.4|^6.2|^7.0", "symfony/monolog-bundle": "^3.8", "symfony/serializer": "^5.4|^6.2|^7.0", - "symfony/security-core": "^7.0" - "ext-json": "*", + "symfony/security-core": "^7.0", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^9.6|^10.1", From 976f284eef4b6ed838abf42961e6e586ec046e89 Mon Sep 17 00:00:00 2001 From: Anton_sz Date: Sun, 10 Mar 2024 01:44:51 +0100 Subject: [PATCH 3/3] Add tests for new PersonProvider --- Factories/RollbarHandlerFactory.php | 3 +- Service/PersonProvider.php | 7 +- Service/PersonProviderInterface.php | 2 +- Tests/Fixtures/App/AppKernel.php | 5 +- Tests/Fixtures/App/config/config.yml | 6 ++ Tests/Service/PersonProviderTest.php | 103 +++++++++++++++++++++++++++ composer.json | 2 +- 7 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 Tests/Service/PersonProviderTest.php diff --git a/Factories/RollbarHandlerFactory.php b/Factories/RollbarHandlerFactory.php index 660542c..c45e30a 100755 --- a/Factories/RollbarHandlerFactory.php +++ b/Factories/RollbarHandlerFactory.php @@ -9,6 +9,7 @@ use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Serializer\SerializerInterface; use Throwable; +use function json_decode; class RollbarHandlerFactory { @@ -25,7 +26,7 @@ public function __construct(array $config, SerializerInterface $serializer, Pers try { $person = call_user_func([$personProvider, 'getUserInfo']); - return \json_decode($serializer->serialize($person, 'json'), true, 512, JSON_THROW_ON_ERROR); + return json_decode($serializer->serialize($person, 'json'), true, 512, JSON_THROW_ON_ERROR); } catch (Throwable) { // do nothing } diff --git a/Service/PersonProvider.php b/Service/PersonProvider.php index 5151f2b..9bf65e6 100644 --- a/Service/PersonProvider.php +++ b/Service/PersonProvider.php @@ -10,9 +10,10 @@ class PersonProvider implements PersonProviderInterface public function __construct( private TokenStorageInterface $tokenStorage, private NormalizerInterface $normalizer, - ) {} + ) { + } - public function getUserInfo(): array + public function getUserInfo(): array|string|int|float|bool|\ArrayObject|null { $token = $this->tokenStorage->getToken(); @@ -22,6 +23,6 @@ public function getUserInfo(): array return $this->normalizer->normalize($user, 'json'); } - return []; + return null; } } diff --git a/Service/PersonProviderInterface.php b/Service/PersonProviderInterface.php index 0859447..9207578 100644 --- a/Service/PersonProviderInterface.php +++ b/Service/PersonProviderInterface.php @@ -4,5 +4,5 @@ interface PersonProviderInterface { - public function getUserInfo(): array; + public function getUserInfo(): array|string|int|float|bool|\ArrayObject|null; } diff --git a/Tests/Fixtures/App/AppKernel.php b/Tests/Fixtures/App/AppKernel.php index 485936e..029bce3 100755 --- a/Tests/Fixtures/App/AppKernel.php +++ b/Tests/Fixtures/App/AppKernel.php @@ -2,9 +2,11 @@ namespace Tests\Fixtures\App; +use Exception; use Rollbar\Symfony\RollbarBundle\RollbarBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\MonologBundle\MonologBundle; +use Symfony\Bundle\SecurityBundle\SecurityBundle; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; @@ -14,6 +16,7 @@ public function registerBundles(): iterable { return [ new FrameworkBundle(), + new SecurityBundle(), new MonologBundle(), new RollbarBundle(), ]; @@ -25,7 +28,7 @@ public function getProjectDir(): string } /** - * @throws \Exception + * @throws Exception */ public function registerContainerConfiguration(LoaderInterface $loader): void { diff --git a/Tests/Fixtures/App/config/config.yml b/Tests/Fixtures/App/config/config.yml index bbaeb5d..f3121cc 100755 --- a/Tests/Fixtures/App/config/config.yml +++ b/Tests/Fixtures/App/config/config.yml @@ -9,3 +9,9 @@ framework: router: resource: "%kernel.project_dir%/config/routing.yml" strict_requirements: ~ + +security: + firewalls: + main: + pattern: ^/dashboard + security: false diff --git a/Tests/Service/PersonProviderTest.php b/Tests/Service/PersonProviderTest.php new file mode 100644 index 0000000..5abec1d --- /dev/null +++ b/Tests/Service/PersonProviderTest.php @@ -0,0 +1,103 @@ +tokenStorage = $this->getMockBuilder(TokenStorageInterface::class) + ->onlyMethods(['getToken', 'setToken']) + ->getMock(); + + $this->normalizer = $this->getMockBuilder(NormalizerInterface::class) + ->disableOriginalConstructor() + ->onlyMethods(['normalize', 'supportsNormalization']) + ->getMock(); + + $this->personProvider = new PersonProvider($this->tokenStorage, $this->normalizer); + } + + public function testNullToken(): void + { + $this->tokenStorage->method('getToken') + ->willReturn(null); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals(null, $info); + } + + public function testNullUser(): void + { + $token = $this->getMockBuilder(UsernamePasswordToken::class) + ->disableOriginalConstructor() + ->onlyMethods(['getUser']) + ->getMock(); + + $token->method('getUser') + ->willReturn(null); + + $this->tokenStorage->method('getToken') + ->willReturn($token); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals(null, $info); + } + + public static function normalizedUserData(): Generator + { + yield 'normalized data: null' => [null, null]; + yield 'normalized data: int' => [12, 12]; + yield 'normalized data: float' => [12.153, 12.153]; + yield 'normalized data: string' => ['john.doe@example.com', 'john.doe@example.com']; + yield 'normalized data: array' => [ + ['email' => 'john.doe@example.com', 'name' => 'John'], + ['email' => 'john.doe@example.com', 'name' => 'John'], + ]; + yield 'normalized data: ArrayObject' => [ + new ArrayObject(['email' => 'john.doe@example.com', 'name' => 'John']), + new ArrayObject(['email' => 'john.doe@example.com', 'name' => 'John']), + ]; + yield 'normalized data: bool' => [true, true]; + } + + /** + * @dataProvider normalizedUserData + */ + public function testUserInTokenStorage($normalizedData, $expected): void + { + $token = $this->getMockBuilder(UsernamePasswordToken::class) + ->disableOriginalConstructor() + ->onlyMethods(['getUser']) + ->getMock(); + + $token->method('getUser') + ->willReturn(new InMemoryUser('john-doe', 'pass')); + + $this->tokenStorage->method('getToken') + ->willReturn($token); + + $this->normalizer->method('normalize') + ->willReturn($normalizedData); + + $info = $this->personProvider->getUserInfo(); + + $this->assertEquals($expected, $info); + } +} diff --git a/composer.json b/composer.json index 44b88c9..4cd8eb3 100755 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "symfony/http-kernel": "^5.4|^6.2|^7.0", "symfony/monolog-bundle": "^3.8", "symfony/serializer": "^5.4|^6.2|^7.0", - "symfony/security-core": "^7.0", + "symfony/security-bundle": "^5.4|^6.4", "ext-json": "*" }, "require-dev": {