From 6e8c3965ee83c118bc826271fec8e9a6975b8a70 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 14 May 2024 15:21:05 +0200 Subject: [PATCH] feat: Made Gravatar optional with roadiz_core.useGravatar config option --- config/packages/roadiz_core.yaml | 1 + lib/RoadizCoreBundle/config/services.yaml | 1 + .../src/DependencyInjection/Configuration.php | 3 ++ .../RoadizCoreExtension.php | 1 + .../UserLifeCycleSubscriber.php | 32 ++++++------------- lib/RoadizRozierBundle/config/services.yaml | 1 + .../src/Controllers/Users/UsersController.php | 15 ++++++++- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/config/packages/roadiz_core.yaml b/config/packages/roadiz_core.yaml index 20295732..d2b7229a 100644 --- a/config/packages/roadiz_core.yaml +++ b/config/packages/roadiz_core.yaml @@ -5,6 +5,7 @@ roadiz_core: staticDomainName: ~ documentsLibDir: 'lib/Documents/src' useNativeJsonColumnType: true + useGravatar: false medias: unsplash_client_id: '%env(string:APP_UNSPLASH_CLIENT_ID)%' soundcloud_client_id: '%env(string:APP_SOUNDCLOUD_CLIENT_ID)%' diff --git a/lib/RoadizCoreBundle/config/services.yaml b/lib/RoadizCoreBundle/config/services.yaml index 5733014b..656bbae2 100644 --- a/lib/RoadizCoreBundle/config/services.yaml +++ b/lib/RoadizCoreBundle/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/lib/RoadizCoreBundle/src/DependencyInjection/Configuration.php b/lib/RoadizCoreBundle/src/DependencyInjection/Configuration.php index ff7e7d64..8c5134ea 100644 --- a/lib/RoadizCoreBundle/src/DependencyInjection/Configuration.php +++ b/lib/RoadizCoreBundle/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/lib/RoadizCoreBundle/src/DependencyInjection/RoadizCoreExtension.php b/lib/RoadizCoreBundle/src/DependencyInjection/RoadizCoreExtension.php index e0608f74..94eb4efa 100644 --- a/lib/RoadizCoreBundle/src/DependencyInjection/RoadizCoreExtension.php +++ b/lib/RoadizCoreBundle/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/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php index b7c59b99..0e3b98f8 100644 --- a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php +++ b/lib/RoadizCoreBundle/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()); } } diff --git a/lib/RoadizRozierBundle/config/services.yaml b/lib/RoadizRozierBundle/config/services.yaml index e306cbeb..4dfebde6 100644 --- a/lib/RoadizRozierBundle/config/services.yaml +++ b/lib/RoadizRozierBundle/config/services.yaml @@ -13,6 +13,7 @@ services: $soundcloudClientId: '%roadiz_core.medias.soundcloud_client_id%' $allowNodeTypeEdition: '%kernel.debug%' $forceSslOnRedirectUri: '%roadiz_rozier.open_id.force_ssl_on_redirect_uri%' + $useGravatar: '%roadiz_core.use_gravatar%' RZ\Roadiz\RozierBundle\: resource: '../src/' diff --git a/lib/Rozier/src/Controllers/Users/UsersController.php b/lib/Rozier/src/Controllers/Users/UsersController.php index 81f6cac7..589af201 100644 --- a/lib/Rozier/src/Controllers/Users/UsersController.php +++ b/lib/Rozier/src/Controllers/Users/UsersController.php @@ -4,12 +4,15 @@ namespace Themes\Rozier\Controllers\Users; +use JMS\Serializer\SerializerInterface; use RZ\Roadiz\Core\AbstractEntities\PersistableInterface; use RZ\Roadiz\CoreBundle\Entity\Role; use RZ\Roadiz\CoreBundle\Entity\User; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Themes\Rozier\Controllers\AbstractAdminWithBulkController; use Themes\Rozier\Forms\UserDetailsType; use Themes\Rozier\Forms\UserType; @@ -17,6 +20,16 @@ class UsersController extends AbstractAdminWithBulkController { + public function __construct( + FormFactoryInterface $formFactory, + SerializerInterface $serializer, + UrlGeneratorInterface $urlGenerator, + private readonly bool $useGravatar + ) { + parent::__construct($formFactory, $serializer, $urlGenerator); + } + + protected function supports(PersistableInterface $item): bool { return $item instanceof User; @@ -123,7 +136,7 @@ protected function createUpdateEvent(PersistableInterface $item) /* * If pictureUrl is empty, use default Gravatar image. */ - if ($item->getPictureUrl() == '') { + if ($item->getPictureUrl() == '' && $this->useGravatar) { $item->setPictureUrl($item->getGravatarUrl()); }