diff --git a/config/module.config.php b/config/module.config.php index 93a8a16db4d1547cdbea0f54ee3362eb8119d6a8..91ca4b45ae588fe46ee8957a6f805ee1f245d312 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -289,7 +289,6 @@ return [ MailerService::class => MailerServiceFactory::class, - 'UnicaenApp\HistoriqueListener' => HistoriqueListenerFactory::class, 'instadia' => InstadiaServiceFactory::class, ], 'shared' => [ diff --git a/doc/Historique.dokuwiki b/doc/Historique.dokuwiki index 9e92fcd933ce09bb6d46472d4ede976e5e9daff2..258820aa4bfdc505634e868608e349bdffda1a58 100644 --- a/doc/Historique.dokuwiki +++ b/doc/Historique.dokuwiki @@ -42,7 +42,7 @@ Pour que l'historique fonctionne, il faut : ==== Listener ==== Doctrine doit se charger, par l'entremise du Listener de peupler automatiquement les champs liés aux historiques lorsque l'entité est enregistrée en base. -Un Listener tout prêt est disponible : il s'agit de <php>UnicaenApp\ORM\Event\Listeners\HistoriqueListener</php>. +Un Listener tout prêt est disponible : il s'agit de <php>UnicaenAuth\ORM\Event\Listeners\HistoriqueListener</php>. Il n'est cependant pas activé par défaut. Pour l'activer, il suffit d'ajouter à la configuration du projet les données suivantes : <code php> diff --git a/doc/Historique.md b/doc/Historique.md index 316eabc98ed91c66c5534c8b2a83b3b27058ad67..5e4b15ae51c1255d9a9a77ec26fba9885f7446fe 100644 --- a/doc/Historique.md +++ b/doc/Historique.md @@ -75,7 +75,7 @@ automatiquement les champs liés aux historiques lorsque l\'entité est enregistrée en base. Un Listener tout prêt est disponible : il s\'agit de -`UnicaenApp\ORM\Event\Listeners\HistoriqueListener`{.php}. Il n\'est +`UnicaenAuth\ORM\Event\Listeners\HistoriqueListener`{.php}. Il n\'est cependant pas activé par défaut. Pour l\'activer, il suffit d\'ajouter à la configuration du projet les données suivantes : diff --git a/src/UnicaenApp/Entity/HistoriqueAwareTrait.php b/src/UnicaenApp/Entity/HistoriqueAwareTrait.php index 79d6f0128211188665371abf1056edc9ebb9f260..53b7e3104927ebdc6497eb5822d12539d11ce4c1 100644 --- a/src/UnicaenApp/Entity/HistoriqueAwareTrait.php +++ b/src/UnicaenApp/Entity/HistoriqueAwareTrait.php @@ -4,7 +4,6 @@ namespace UnicaenApp\Entity; use DateTime; use UnicaenApp\Exception\RuntimeException; -use UnicaenApp\ORM\Event\Listeners\HistoriqueListener; /** * Code commun aux entités possédant une gestion d'historique. @@ -191,7 +190,6 @@ trait HistoriqueAwareTrait * Marque cet enregistrement comme historisé. * * @see HistoriqueAwareInterface - * @see HistoriqueListener * * @param UserInterface|null $destructeur Auteur de la suppression ; si null, peut être renseigné * automatiquement (cf. HistoriqueListener) si la classe implémente diff --git a/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListener.php b/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListener.php deleted file mode 100644 index b26a6dccc26f681f5b19ac0e404398b745fc0c8e..0000000000000000000000000000000000000000 --- a/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListener.php +++ /dev/null @@ -1,166 +0,0 @@ -<?php - -namespace UnicaenApp\ORM\Event\Listeners; - -use Doctrine\Common\EventSubscriber; -use Doctrine\ORM\Event\LifecycleEventArgs; -use Doctrine\ORM\Event\PreUpdateEventArgs; -use Doctrine\ORM\Events; -use RuntimeException; -use UnicaenApp\Entity\HistoriqueAwareInterface; -use UnicaenAuth\Entity\Db\AbstractUser; -use Zend\Authentication\AuthenticationService; - -/** - * Listener Doctrine. - * - * Renseigne si besoin l'heure et l'auteur de la création/modification - * de toute entité dont la classe implémente HistoriqueAwareInterface. - * - * Déclenchement : avant que l'enregistrement ne soit persisté (création) ou mis à jour (update). - * - * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> - * @see HistoriqueAwareInterface - */ -class HistoriqueListener implements EventSubscriber -{ - /** - * @var AuthenticationService - */ - private $authenticationService; - - /** - * @var mixed - */ - protected $identity; - - /** - * @param AuthenticationService $authenticationService - */ - public function setAuthenticationService(AuthenticationService $authenticationService) - { - $this->authenticationService = $authenticationService; - } - - /** - * @param LifecycleEventArgs $args - * @throws RuntimeException Aucun utilisateur disponible pour en faire l'auteur de la création/modification - */ - protected function updateHistorique(LifecycleEventArgs $args) - { - $entity = $args->getEntity(); - - // l'entité doit implémenter l'interface requise - if (! $entity instanceof HistoriqueAwareInterface) { - return; - } - - $now = new \DateTime(); - - if (null === $entity->getHistoCreation()) { - $entity->setHistoCreation($now); - } - - // on tente d'abord d'obtenir l'utilisateur connecté pour en faire l'auteur de la création/modification. - $user = $this->getAuthenticatedUser(); - // si aucun utilisateur connecté n'est disponible, on utilise l'éventuel auteur existant - if (null === $user) { - $user = $entity->getHistoCreateur(); - } - // si nous ne disposons d'aucun utilisateur, basta! - if (null === $user) { - throw new RuntimeException("Aucun utilisateur disponible pour en faire l'auteur de la création/modification."); - } - - if (null === $entity->getHistoCreateur()) { - $entity->setHistoCreateur($user); - } - - $entity->setHistoModificateur($user); - $entity->setHistoModification($now); - /* ce bloc a été mis en commentaire car il est inutile: cf. 2 lignes précédentes ! - if (null === $entity->getHistoDestruction() && null === $entity->getHistoDestructeur()) { - $entity - ->setHistoModification($now) - ->setHistoModificateur($user); - } - */ - - if (null !== $entity->getHistoDestruction() && null === $entity->getHistoDestructeur()) { - $entity->setHistoDestructeur($user); - } - } - - /** - * Recherche l'utilisateur connecté pour l'utiliser comme auteur de la création/modification. - * - * @return AbstractUser - */ - private function getAuthenticatedUser() - { - $user = null; - - if (($identity = $this->getIdentity())) { - if (isset($identity['db']) && $identity['db'] instanceof AbstractUser) { - /* @var $user AbstractUser */ - $user = $identity['db']; - } - } - - return $user; - } - - /** - * @param LifecycleEventArgs $args - */ - public function prePersist(LifecycleEventArgs $args) - { - $this->updateHistorique($args); - } - - /** - * @param PreUpdateEventArgs $args - */ - public function preUpdate(PreUpdateEventArgs $args) - { - $this->updateHistorique($args); - } - - /** - * Injecte l'identité authentifiée courante. - * - * @param mixed $identity - * @return self - */ - public function setIdentity($identity) - { - $this->identity = $identity; - - return $this; - } - - /** - * Retourne l'identité authentifiée courante. - * - * @return mixed - */ - public function getIdentity() - { - if (null === $this->identity) { - $authenticationService = $this->authenticationService; - if ($authenticationService->hasIdentity()) { - $this->identity = $authenticationService->getIdentity(); - } - } - - return $this->identity; - } - - /** - * {@inheritdoc} - */ - public function getSubscribedEvents() - { - return [Events::prePersist, Events::preUpdate]; - } -} \ No newline at end of file diff --git a/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListenerFactory.php b/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListenerFactory.php deleted file mode 100644 index 18c1e1de86ddb04596cbaa358286b2575e6a5235..0000000000000000000000000000000000000000 --- a/src/UnicaenApp/ORM/Event/Listeners/HistoriqueListenerFactory.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace UnicaenApp\ORM\Event\Listeners; - -use Interop\Container\ContainerInterface; -use Zend\Authentication\AuthenticationService; -use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\ServiceLocatorInterface; - -/** - * Description of MouchardServiceFactory - * - * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> - */ -class HistoriqueListenerFactory implements FactoryInterface -{ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, '?'); - } - - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) - { - /** @var AuthenticationService $authenticationService */ - $authenticationService = $container->get('Zend\Authentication\AuthenticationService'); - - $listener = new HistoriqueListener(); - $listener->setAuthenticationService($authenticationService); - - return $listener; - } -}