From 711676f121fede65a089caa11383c65190c63615 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Tue, 14 May 2024 13:21:34 +0000 Subject: [PATCH] feat: Made Gravatar optional with roadiz_core.useGravatar config option --- config/services.yaml | 1 + src/DependencyInjection/Configuration.php | 3 ++ .../RoadizCoreExtension.php | 1 + .../UserLifeCycleSubscriber.php | 32 ++++++------------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 5733014b..656bbae2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -51,6 +51,7 @@ services: $recaptchaPublicKey: '%roadiz_core.medias.recaptcha_public_key%' $recaptchaPrivateKey: '%roadiz_core.medias.recaptcha_private_key%' $webResponseClass: '%roadiz_core.web_response_class%' + $useGravatar: '%roadiz_core.use_gravatar%' RZ\Roadiz\CoreBundle\: resource: '../src/' diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ff7e7d64..8c5134ea 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -50,6 +50,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->booleanNode('hideRoadizVersion') ->defaultValue(false) ->end() + ->booleanNode('useGravatar') + ->defaultTrue() + ->end() ->scalarNode('documentsLibDir')->defaultValue( 'vendor/roadiz/documents/src' )->info('Relative path to Roadiz Documents lib sources from project directory.')->end() diff --git a/src/DependencyInjection/RoadizCoreExtension.php b/src/DependencyInjection/RoadizCoreExtension.php index e0608f74..94eb4efa 100644 --- a/src/DependencyInjection/RoadizCoreExtension.php +++ b/src/DependencyInjection/RoadizCoreExtension.php @@ -57,6 +57,7 @@ public function load(array $configs, ContainerBuilder $container): void $container->setParameter('roadiz_core.app_namespace', $config['appNamespace']); $container->setParameter('roadiz_core.app_version', $config['appVersion']); + $container->setParameter('roadiz_core.use_gravatar', $config['useGravatar']); $container->setParameter('roadiz_core.health_check_token', $config['healthCheckToken']); $container->setParameter('roadiz_core.inheritance_type', $config['inheritance']['type']); $container->setParameter('roadiz_core.max_versions_showed', $config['maxVersionsShowed']); diff --git a/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php b/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php index b7c59b99..0e3b98f8 100644 --- a/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php +++ b/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php @@ -24,27 +24,13 @@ final class UserLifeCycleSubscriber implements EventSubscriber { - private UserViewer $userViewer; - private EventDispatcherInterface $dispatcher; - private PasswordHasherFactoryInterface $passwordHasherFactory; - private LoggerInterface $logger; - - /** - * @param UserViewer $userViewer - * @param EventDispatcherInterface $dispatcher - * @param PasswordHasherFactoryInterface $passwordHasherFactory - * @param LoggerInterface $logger - */ public function __construct( - UserViewer $userViewer, - EventDispatcherInterface $dispatcher, - PasswordHasherFactoryInterface $passwordHasherFactory, - LoggerInterface $logger + private readonly UserViewer $userViewer, + private readonly EventDispatcherInterface $dispatcher, + private readonly PasswordHasherFactoryInterface $passwordHasherFactory, + private readonly LoggerInterface $logger, + private readonly bool $useGravatar ) { - $this->userViewer = $userViewer; - $this->dispatcher = $dispatcher; - $this->logger = $logger; - $this->passwordHasherFactory = $passwordHasherFactory; } /** @@ -94,9 +80,11 @@ public function preUpdate(PreUpdateEventArgs $event): void $user->setPictureUrl($url); } catch (\Exception $e) { $user->setFacebookName(''); - $user->setPictureUrl($user->getGravatarUrl()); + if ($this->useGravatar) { + $user->setPictureUrl($user->getGravatarUrl()); + } } - } else { + } elseif ($this->useGravatar) { $user->setPictureUrl($user->getGravatarUrl()); } } @@ -201,7 +189,7 @@ public function prePersist(LifecycleEventArgs $event): void /* * Force a Gravatar image if not defined */ - if (empty($user->getPictureUrl())) { + if (empty($user->getPictureUrl()) && $this->useGravatar) { $user->setPictureUrl($user->getGravatarUrl()); } }