Skip to content
Snippets Groups Projects
Commit b883b8bf authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Merge branch 'master' of https://git.unicaen.fr/lib/unicaen/mail into HEAD

parents 1ab1e73a 5df5ad17
No related branches found
No related tags found
No related merge requests found
Pipeline #35623 passed
7.1.3
-----
- Mise en place de la commande mail:purge pour supprimer les mails envoyée avant une date
7.1.2 7.1.2
----- -----
- Correction pour caster la variable redirectTo en Array - Correction pour caster la variable redirectTo en Array
......
...@@ -10,6 +10,8 @@ use UnicaenMail\View\Helper\MailViewHelper; ...@@ -10,6 +10,8 @@ use UnicaenMail\View\Helper\MailViewHelper;
use UnicaenPrivilege\Guard\PrivilegeController; use UnicaenPrivilege\Guard\PrivilegeController;
use Laminas\Router\Http\Literal; use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment; use Laminas\Router\Http\Segment;
use UnicaenMail\Command\MailPurgeCommand;
use UnicaenMail\Command\MailPurgeCommandFactory;
return array( return array(
'bjyauthorize' => [ 'bjyauthorize' => [
...@@ -73,6 +75,7 @@ return array( ...@@ -73,6 +75,7 @@ return array(
'options' => [ 'options' => [
'route' => '/mail', 'route' => '/mail',
'defaults' => [ 'defaults' => [
/** @see MailController::indexAction() */
'controller' => MailController::class, 'controller' => MailController::class,
'action' => 'index', 'action' => 'index',
], ],
...@@ -84,7 +87,7 @@ return array( ...@@ -84,7 +87,7 @@ return array(
'options' => [ 'options' => [
'route' => '/test', 'route' => '/test',
'defaults' => [ 'defaults' => [
'controller' => MailController::class, /** @see MailController::testAction() */
'action' => 'test', 'action' => 'test',
], ],
], ],
...@@ -94,7 +97,7 @@ return array( ...@@ -94,7 +97,7 @@ return array(
'options' => [ 'options' => [
'route' => '/afficher/:mail', 'route' => '/afficher/:mail',
'defaults' => [ 'defaults' => [
'controller' => MailController::class, /** @see MailController::afficherAction() */
'action' => 'afficher', 'action' => 'afficher',
], ],
], ],
...@@ -104,7 +107,7 @@ return array( ...@@ -104,7 +107,7 @@ return array(
'options' => [ 'options' => [
'route' => '/reenvoi/:mail', 'route' => '/reenvoi/:mail',
'defaults' => [ 'defaults' => [
'controller' => MailController::class, /** @see MailController::reenvoiAction() */
'action' => 'reenvoi', 'action' => 'reenvoi',
], ],
], ],
...@@ -114,7 +117,7 @@ return array( ...@@ -114,7 +117,7 @@ return array(
'options' => [ 'options' => [
'route' => '/supprimer/:mail', 'route' => '/supprimer/:mail',
'defaults' => [ 'defaults' => [
'controller' => MailController::class, /** @see MailController::supprimerAction() */
'action' => 'supprimer', 'action' => 'supprimer',
], ],
], ],
...@@ -133,6 +136,7 @@ return array( ...@@ -133,6 +136,7 @@ return array(
'service_manager' => [ 'service_manager' => [
'factories' => [ 'factories' => [
MailService::class => MailServiceFactory::class, MailService::class => MailServiceFactory::class,
MailPurgeCommand::class => MailPurgeCommandFactory::class,
], ],
], ],
...@@ -147,4 +151,10 @@ return array( ...@@ -147,4 +151,10 @@ return array(
'mails' => MailsViewHelper::class, 'mails' => MailsViewHelper::class,
], ],
], ],
'laminas-cli' => [
'commands' => [
'mail:purge' => MailPurgeCommand::class,
],
],
); );
...@@ -52,4 +52,6 @@ return [ ...@@ -52,4 +52,6 @@ return [
// ], // ],
// ] // ]
// ], // ],
// 'conservation-time' => new DateInterval('P2Y'),
]; ];
...@@ -60,6 +60,20 @@ $options['droits'][ ...@@ -60,6 +60,20 @@ $options['droits'][
] ]
``` ```
Purge des mails
---
La commande console `mail:purge` permet de supprimer les mails envoyées avant une date.
- La durée par défaut de conservertion est de 1 an, il s'agit d'un paramétre modifiable dans la configuration sous la forme de DateInterval :
```
'unicaen-mail' => [
'conservation-time' => new DateInterval('P2Y'),
],
```
1 option possibles :
- `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des mails (on ignore alors celle définie en conf).
Configuration Configuration
============= =============
...@@ -98,6 +112,11 @@ return [ ...@@ -98,6 +112,11 @@ return [
'subject_prefix' => '[Mon App]', 'subject_prefix' => '[Mon App]',
'from_name' => 'Mon application', 'from_name' => 'Mon application',
'from_email' => 'ne-pas-repondre@mail.fr' 'from_email' => 'ne-pas-repondre@mail.fr'
/**
* Durée de conservertions des mails
*/
'conservation-time' => new DateInterval('P2Y'),
], ],
]; ];
``` ```
......
<?php
namespace UnicaenMail\Command;
use DateInterval;
use DateTime;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use UnicaenEtat\Entity\Db\EtatInstance;
use UnicaenEtat\Service\EtatInstance\EtatInstanceServiceAwareTrait;
use UnicaenMail\Entity\Db\Mail;
use UnicaenMail\Service\Mail\MailServiceAwareTrait;
/***
* @desc Supprime toutes les instances d'état qui sont archivées depuis un certains temps
* paramétre à définir dans config unicaen-etat > conservation-time sour la forme d'un DateInterval
* Par défaut : 1 an
* TODO : permettre dans la conf de spécifier les types et catégories d'états qui doivent être purgée
* TODO : gérer des dates différents par état/types
* TODO : rendre des types/catégories non purgeable
*/
class MailPurgeCommand extends Command
{
use MailServiceAwareTrait;
protected static $defaultName = 'mail:purge';
protected ?DateInterval $conservationTime = null;
public function setConservationTime(DateInterval $conservationTime): static
{
$this->conservationTime = $conservationTime;
return $this;
}
public function getDateSuppression() : DateTime
{
$date = new DateTime();
$conservationTime = ($this->conservationTime) ?? new DateInterval('P1Y');
$date->sub($conservationTime);
return $date;
}
protected function configure() : static
{
$this->setDescription("Supprime les instances de mails qui ont été envoyée depuis 'longtemps'");
$this->addOption('time', '-t', InputOption::VALUE_OPTIONAL, "Intervale de temps que l'on souhaite conserver -- format : P1Y");
return $this;
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
try{
$time = $input->getOption('time');
if(isset($time) && $time != ""){
$this->conservationTime = new DateInterval($time);
}
}
catch (Exception $e){
$io->error($e->getMessage());
exit(-1);
}
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
try {
$io->title("Suppression des instances de mails");
$date = $this->getDateSuppression();
$qb =$this->getMailService()->createQueryBuilder();
$qb->andWhere("mail.dateEnvoi < :date");
$qb->setParameter("date", $date);
$mails = $qb->getQuery()->getResult();
if(!empty($mails)) {
$io->progressStart(sizeof($mails));
/** @var Mail $mail */
foreach ($mails as $mail) {
$this->getMailService()->delete($mail);
$io->progressAdvance();
}
$io->progressFinish();
}
else{
$io->text("Aucun mail à supprimer");
}
$io->success("Suppression terminée");
}
catch (Exception $e){
$io->error($e->getMessage());
return self::FAILURE;
}
return self::SUCCESS;
}
}
\ No newline at end of file
<?php
namespace UnicaenMail\Command;
use DateInterval;
use Exception;
use Mail\Service\Mail\MailService;
use Psr\Container\ContainerInterface;
class MailPurgeCommandFactory
{
/**
* @param ContainerInterface $container
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Exception
*/
public function __invoke(ContainerInterface $container): MailPurgeCommand
{
$command = new MailPurgeCommand();
$command->setMailService($container->get(MailService::class));
$config = $container->get('Configuration');
if (isset($config['unicaen-mail']['conservation-time'])) {
$conservationTime = $config['unicaen-mail']['conservation-time'];
if(!$conservationTime instanceof DateInterval) {
throw new Exception("Le paramètre de coniguration 'unicaen-mail > conservation-time' doit être un DateInterval");
}
$command->setConservationTime($conservationTime);
}
return $command;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment