From e0e4dd206349830e63184eb5d6fe514efc05af37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Fri, 27 Nov 2015 14:51:13 +0100 Subject: [PATCH] =?UTF-8?q?S=C3=A9pare=20la=20gestion=20des=20informations?= =?UTF-8?q?=20de=20cr=C3=A9ation=20du=20manager.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity/Repository/UploadedFileRepository.php | 4 +- Entity/UploadedFile.php | 40 +++++++++++--- Listener/CreationDataListener.php | 56 ++++++++++++++++++++ Model/FileManagerInterface.php | 2 +- Model/UploadedFileInterface.php | 27 ++++++++++ Resources/config/services.yml | 12 ++++- 6 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 Listener/CreationDataListener.php diff --git a/Entity/Repository/UploadedFileRepository.php b/Entity/Repository/UploadedFileRepository.php index 14d362d8..eb7f60bc 100644 --- a/Entity/Repository/UploadedFileRepository.php +++ b/Entity/Repository/UploadedFileRepository.php @@ -58,9 +58,9 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte $this->eventDispatcher = $eventDispatcher; } - public function create($filename, $size, $mimeType, $lastModified = null, $createdBy = null, $createdFrom = null) + public function create($filename, $size, $mimeType, $lastModified = null) { - $file = new UploadedFile($createdBy, $createdFrom); + $file = new UploadedFile(); $file ->setFilesystem($this->filesystem) diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index 9fbc398e..23ce3ec2 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -19,7 +19,10 @@ use Rhumsaa\Uuid\Uuid; /** * @ORM\Entity(repositoryClass="Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository") - * @ORM\EntityListeners({"Irstea\FileUploadBundle\Listener\UploadedFileListener"}) + * @ORM\EntityListeners({ + * "Irstea\FileUploadBundle\Listener\UploadedFileListener", + * "Irstea\FileUploadBundle\Listener\CreationDataListener" + * }) */ class UploadedFile implements UploadedFileInterface { @@ -114,19 +117,13 @@ class UploadedFile implements UploadedFileInterface private $localTempPath = null; /** Crée un UploadedFile. - * - * @param string $createdBy Nom du créateur. - * @param string $createdFrom Adresse UP du créateur. * * @internal */ - public function __construct($createdBy = null, $createdFrom = null) + public function __construct() { $this->id = Uuid::uuid4()->toString(); $this->path = self::ORPHAN_PREFIX.$this->id; - $this->createdAt = new DateTime('now'); - $this->createdBy = $createdBy; - $this->createdFrom = $createdFrom; } /** @@ -604,6 +601,33 @@ class UploadedFile implements UploadedFileInterface return $this->createdFrom; } + /** + * {@inheritdoc} + */ + public function setCreatedAt(DateTime $createdAt) + { + $this->createdAt = $createdAt; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setCreatedBy($createdBy) + { + $this->createdBy = $createdBy; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setCreatedFrom($createdFrom) + { + $this->createdFrom = $createdFrom; + return $this; + } + /** * {@inheritdoc} */ diff --git a/Listener/CreationDataListener.php b/Listener/CreationDataListener.php new file mode 100644 index 00000000..cae9accb --- /dev/null +++ b/Listener/CreationDataListener.php @@ -0,0 +1,56 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Listener; + +use DateTime; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Gaufrette\Exception\FileNotFound; +use Gaufrette\Filesystem; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; + +/** + * Ajoute les informations sur la date, l'utilisateur connecté et l'adresse IP du client dans le fichier. + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class CreationDataListener +{ + /** + * @var TokenStorageInterface + */ + private $tokenStorage; + + /** + * @var RequestStack + */ + private $requestStack; + + public function __construct(TokenStorageInterface $tokenStorage, RequestStack $requestStack) + { + $this->tokenStorage = $tokenStorage; + $this->requestStack = $requestStack; + } + + /** + * @param LifecycleEventArgs $event + */ + public function prePersist(UploadedFileInterface $file, LifecycleEventArgs $event) + { + $file->setCreatedAt(new DateTime("now")); + + if ($request = $this->requestStack->getCurrentRequest()) { + $file->setCreatedFrom($request->getClientIp()); + } + + if ($token = $this->tokenStorage->getToken()) { + $file->setCreatedBy($token->getUsername()); + } + } +} diff --git a/Model/FileManagerInterface.php b/Model/FileManagerInterface.php index 0a0865e3..4616577b 100644 --- a/Model/FileManagerInterface.php +++ b/Model/FileManagerInterface.php @@ -39,7 +39,7 @@ interface FileManagerInterface * * @api */ - public function create($name, $size, $mimeType, $lastModified = null, $createdBy = null, $createdFrom = null); + public function create($name, $size, $mimeType, $lastModified = null); /** Supprime un fichier uploadé. * diff --git a/Model/UploadedFileInterface.php b/Model/UploadedFileInterface.php index a3eb621b..324b9708 100644 --- a/Model/UploadedFileInterface.php +++ b/Model/UploadedFileInterface.php @@ -208,6 +208,33 @@ interface UploadedFileInterface */ public function getEtat(); + /** Définit la date de création du fichier. + * + * @param DateTime $time + * @return self + * + * @api + */ + public function setCreatedAt(\DateTime $time); + + /** Définit le nom de l'utilisateur ayant uploadé le fichier. + * + * @param string $username + * @return self + * + * @api + */ + public function setCreatedBy($username); + + /** Retourne l'adresse IP du client ayant uploadé le fichier. + * + * @param string $ipAddress + * @return self + * + * @api + */ + public function setCreatedFrom($ipAddress); + /** Retourne la date de création du fichier. * * @return DateTime diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 869289a3..13c6b1cd 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -3,6 +3,7 @@ parameters: irstea_file_upload.upload_controller.class: Irstea\FileUploadBundle\Controller\UploadController irstea_file_upload.entity_listener.class: Irstea\FileUploadBundle\Listener\UploadedFileListener + irstea_file_upload.creation_data_listener.class: Irstea\FileUploadBundle\Listener\CreationDataListener irstea_file_upload.field_type.class: Irstea\FileUploadBundle\Form\Type\FileUploadType irstea_file_upload.filesystem.name: irstea_file_upload @@ -44,7 +45,7 @@ services: tags: - { name: form.type, alias: file_upload } - # Le listener + # Le listener de mise à jour du filesystem irstea_file_upload.entity_listener: class: %irstea_file_upload.entity_listener.class% arguments: @@ -52,6 +53,15 @@ services: tags: - { name: doctrine.orm.entity_listener } + # Le listener pour les informations de création + irstea_file_upload.creation_data_listener: + class: %irstea_file_upload.creation_data_listener.class% + arguments: + - @security.token_storage + - @request_stack + tags: + - { name: doctrine.orm.entity_listener } + # Le backend de stockage des fichiers irstea_file_upload.filesystem: class: Gaufrette\Filesystem -- GitLab