diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..13566b81b018ad684f3a35fee301741b2734c8f4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/mail.iml b/.idea/mail.iml new file mode 100644 index 0000000000000000000000000000000000000000..a85ab09b8e9c573c485b5d01162f2d70643cff3b --- /dev/null +++ b/.idea/mail.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="WEB_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..066fe240099aec6df1e597913c598c448d9d8aff --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/mail.iml" filepath="$PROJECT_DIR$/.idea/mail.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000000000000000000000000000000000000..b673eaa7c0213687d02d9dc9ac191125e4719b66 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="MessDetectorOptionsConfiguration"> + <option name="transferred" value="true" /> + </component> + <component name="PHPCSFixerOptionsConfiguration"> + <option name="transferred" value="true" /> + </component> + <component name="PHPCodeSnifferOptionsConfiguration"> + <option name="transferred" value="true" /> + </component> + <component name="PhpProjectSharedConfiguration" php_language_level="8.0" /> + <component name="PhpStanOptionsConfiguration"> + <option name="transferred" value="true" /> + </component> + <component name="PsalmOptionsConfiguration"> + <option name="transferred" value="true" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000000000000000000000000000000000000..decba0aa6734f6dc012f078791d40740ffe341dc --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,5 @@ +6.0.6 + +* Possibilité de désactiver l'envoi de mail +* "do_not_send" est renommé en "redirect" +* un nouveau paramètre "do_not_send" n'envoie pas le mail \ No newline at end of file diff --git a/SQL/001_tables.sql b/SQL/001_tables.sql index 95f905611f2110cf11c5c2a661ab70cae4c86a15..adb3f9c89be1cf6f3b2afaea7d2e08464fc4f509 100644 --- a/SQL/001_tables.sql +++ b/SQL/001_tables.sql @@ -1,17 +1,14 @@ -create table unicaen_mail_mail +create table if not exists unicaen_mail_mail ( - id serial not null constraint umail_pkey primary key, - date_envoi timestamp not null, - status_envoi varchar(256) not null, - destinataires text not null, - destinataires_initials text, - sujet text, - corps text, - mots_clefs text, - log text +id serial not null constraint umail_pkey primary key, +date_envoi timestamp not null, +status_envoi varchar(256) not null, +destinataires text not null, +destinataires_initials text, +sujet text, +corps text, +mots_clefs text, +log text ); -create unique index ummail_id_uindex on unicaen_mail_mail (id); - - - +create unique index if not exists ummail_id_uindex on unicaen_mail_mail (id); \ No newline at end of file diff --git a/SQL/002_privileges.sql b/SQL/002_privileges.sql index 8bb13278c60346b4e143a36f431bf003ea6c137f..8e650cbfbbe73791ce51546a649e5d2b1c8b6627 100644 --- a/SQL/002_privileges.sql +++ b/SQL/002_privileges.sql @@ -1,8 +1,39 @@ -- MAIL -INSERT INTO unicaen_privilege_categorie (id ,code, libelle, ordre, namespace) VALUES (next_val('unicaen_privilege_categorie_id_seq'), 'mail', 'UnicaenMail - Gestion des mails', 9000, 'UnicaenMail\Provider\Privilege'); -INSERT INTO unicaen_privilege_privilege (id, categorie_id, code, libelle, ordre) VALUES (next_val('unicaen_privilege_privilege_id_seq'), current_val('unicaen_privilege_categorie_id_seq'), 'mail_index', 'Affichage de l''index', 10); -INSERT INTO unicaen_privilege_privilege (id, categorie_id, code, libelle, ordre) VALUES (next_val('unicaen_privilege_privilege_id_seq'), current_val('unicaen_privilege_categorie_id_seq'), 'mail_afficher', 'Afficher un mail', 20); -INSERT INTO unicaen_privilege_privilege (id, categorie_id, code, libelle, ordre) VALUES (next_val('unicaen_privilege_privilege_id_seq'), current_val('unicaen_privilege_categorie_id_seq'), 'mail_reenvoi', 'Ré-envoi d''un mail', 30); -INSERT INTO unicaen_privilege_privilege (id, categorie_id, code, libelle, ordre) VALUES (next_val('unicaen_privilege_privilege_id_seq'), current_val('unicaen_privilege_categorie_id_seq'), 'mail_supprimer', 'Suppression d''un mail', 40); -INSERT INTO unicaen_privilege_privilege (id, categorie_id, code, libelle, ordre) VALUES (next_val('unicaen_privilege_privilege_id_seq'), current_val('unicaen_privilege_categorie_id_seq'), 'mail_test', 'Envoi d''un mail de test', 100); \ No newline at end of file +INSERT INTO UNICAEN_PRIVILEGE_CATEGORIE ( + CODE, + LIBELLE, + NAMESPACE, + ORDRE) + values + ('mail', 'UnicaenMail - Gestion des mails', 'UnicaenMail\Provider\Privilege', 1051) + ON CONFLICT (CODE) DO + UPDATE SET + LIBELLE=excluded.LIBELLE, + NAMESPACE=excluded.NAMESPACE, + ORDRE=excluded.ORDRE; + +WITH d(code, lib, ordre) AS ( + SELECT 'mail_index', 'Affichage de l''index', 1 UNION + SELECT 'mail_afficher', 'Afficher un mail', 2 UNION + SELECT 'mail_reenvoi', 'Ré-envoi d''un mail', 30 UNION + SELECT 'mail_supprimer', 'Suppression d''un mail', 4 UNION + SELECT 'mail_test', 'Envoi d''un mail de test', 5 +) +INSERT INTO unicaen_privilege_privilege(CATEGORIE_ID, CODE, LIBELLE, ORDRE) + SELECT cp.id, d.code, d.lib, d.ordre + FROM d + JOIN unicaen_privilege_categorie cp ON cp.CODE = 'mail' + ON CONFLICT (CATEGORIE_ID, CODE) DO + UPDATE SET + LIBELLE=excluded.LIBELLE, + ORDRE=excluded.ORDRE; + +insert into unicaen_privilege_privilege_role_linker + (role_id, privilege_id) + (select role.id, privilege.id from unicaen_utilisateur_role role, + unicaen_privilege_privilege privilege + join unicaen_privilege_categorie cp on privilege.categorie_id = cp.id + where role.role_id = 'Admin_tech' + and cp.code in('mail')) + on conflict do nothing; \ No newline at end of file diff --git a/config/unicaen-mail.local.php.dist b/config/unicaen-mail.local.php.dist index c00789d58257c2a8422f0ab5a0354079d5b7145c..d382c2a001117e6347932a02e17511a7f99d5e0a 100644 --- a/config/unicaen-mail.local.php.dist +++ b/config/unicaen-mail.local.php.dist @@ -1,13 +1,15 @@ <?php /** * Configuration locale du module UnicaenMail. + * + * If you have a ./config/autoload/ directory set up for your project, + * drop this config file in it and change the values as you wish. */ use UnicaenMail\Entity\Db\Mail; return [ 'unicaen-mail' => [ - /** * Classe de entité **/ @@ -21,16 +23,36 @@ return [ 'port' => 'XX', ], /** - * Adresses des redirection si do_not_send est à true + * Adresses des redirections si do_not_send est à true */ - 'redirect_to' => ['XXX@XXX.XX', ], - 'do_not_send' => true, - /** - * Configuration de l'expéditeur - */ - 'subject_prefix' => 'XXX', - 'from_name' => 'XXX : XXXX XX XXXXXXX', - 'from_email' => 'ne-pas-repondre@XXXX.XX' + 'module' => [ + 'default' => [ + 'redirect_to' => ['XXX@XXX.XX',], + 'do_not_send' => true, + 'redirect' => true, + 'subject_prefix' => 'XXX', + 'from_name' => 'XXX | XXXXX', + 'from_email' => 'XXX@XXX.XX', + + ], + /** On peut ajouter un clef de module pour la référencer dans la méthode sendMail afin d'exploiter les données passée en valeur. */ +// 'MonPetitModule' => [ +// 'redirect_to' => ['XXX@XXX.XX',], +// 'do_not_send' => true, +// 'subject_prefix' => 'Mes petit module', +// 'from_name' => 'mon-ptit-module', +// 'from_email' => 'assistance.mon-petit-module@XXX.XX', +// ] + ] ], -]; \ No newline at end of file + + 'server_url' => 'https://xxx.xxx', +// 'service_manager' => [ +// 'delegators' => [ +// TreeRouteStack::class => [ +// TreeRouteStackConsoleDelegatorFactory::class, +// ], +// ] +// ], +]; diff --git a/readme.md b/readme.md index 56d1989b8194f6cbaef02a3591f0143e7ffab4fd..3224f968cedb9b6372d4e02fc7b33f03eb3177c1 100644 --- a/readme.md +++ b/readme.md @@ -90,6 +90,7 @@ return [ */ 'redirect_to' => ['john-doe@mail.fr', ], 'do_not_send' => true, + 'redirect' => true, /** * Configuration de l'expéditeur diff --git a/src/UnicaenMail/Controller/MailController.php b/src/UnicaenMail/Controller/MailController.php index 281218812b08af5d815a15d8e1ee5c76d2b70c18..35a99292c02f7df116440dbba4f5de5641c219a6 100644 --- a/src/UnicaenMail/Controller/MailController.php +++ b/src/UnicaenMail/Controller/MailController.php @@ -39,8 +39,10 @@ class MailController extends AbstractActionController { return ['title' => "Envoyer un mail de test", 'error' => "L'adresse mail saisie n'est pas valide."]; } $mail = $this->getMailService()->sendMail($data['mail-to'], 'Mail de test', 'Ceci est un mail de test. <br/> <hr/>Merci de ne pas en tenir compte.'); - $mail->setMotsClefs(['TEST']); - $this->getMailService()->update($mail); + if ($mail){ + $mail->setMotsClefs(['TEST']); + $this->getMailService()->update($mail); + } } return ['title' => "Envoyer un mail de test"]; } diff --git a/src/UnicaenMail/Entity/Db/Mapping/UnicaenMail.Entity.Db.Mail.dcm.xml b/src/UnicaenMail/Entity/Db/Mapping/UnicaenMail.Entity.Db.Mail.dcm.xml index c647710a21ce0fe0b0b4a1575938fb14ce52f1e7..c6e926bfd676eb1e29f18190a90369c77aaf49f2 100644 --- a/src/UnicaenMail/Entity/Db/Mapping/UnicaenMail.Entity.Db.Mail.dcm.xml +++ b/src/UnicaenMail/Entity/Db/Mapping/UnicaenMail.Entity.Db.Mail.dcm.xml @@ -3,7 +3,7 @@ <entity name="UnicaenMail\Entity\Db\Mail" table="unicaen_mail_mail"> <id name="id" type="integer" column="id"> - <generator strategy="IDENTITY"/> + <generator strategy="AUTO"/> </id> <field name="dateEnvoi" type="datetime" column="date_envoi" nullable="false"/> diff --git a/src/UnicaenMail/Exception/NotFoundConfigException.php b/src/UnicaenMail/Exception/NotFoundConfigException.php new file mode 100644 index 0000000000000000000000000000000000000000..3c343a1c99511a6cb49f3a349c694cbae4bf59f6 --- /dev/null +++ b/src/UnicaenMail/Exception/NotFoundConfigException.php @@ -0,0 +1,7 @@ +<?php + +namespace UnicaenMail\Exception; + +use Exception; + +class NotFoundConfigException extends Exception {} \ No newline at end of file diff --git a/src/UnicaenMail/Service/Mail/MailService.php b/src/UnicaenMail/Service/Mail/MailService.php index 37aedd1fed551c9cc8902100ece0571ee5cb03ea..e8ccd99efe763939cbbaf8110ffc9634989f7c2d 100644 --- a/src/UnicaenMail/Service/Mail/MailService.php +++ b/src/UnicaenMail/Service/Mail/MailService.php @@ -16,6 +16,7 @@ use Laminas\Mime\Message as MimeMessage; use Laminas\Mime\Mime; use Laminas\Mime\Part; use Laminas\Mvc\Controller\AbstractActionController; +use UnicaenMail\Exception\NotFoundConfigException; /** * @property EntityManager $objectManager @@ -24,6 +25,7 @@ class MailService { use ProvidesObjectManager; private ?string $entityClass = null; + private array $config = []; /** * @param string $entityClass @@ -35,6 +37,11 @@ class MailService { return $this; } + public function setConfig(array $config) : void + { + $this->config = $config; + } + public function createMailEntity() : Object { $entity = new $this->entityClass(); @@ -42,45 +49,10 @@ class MailService { } private ?TransportInterface $transport; - private array $redirectTo; - private bool $doNotSend; - - /** information sur l'expediteur */ - private ?string $fromName; - private ?string $fromEmail; - private ?string $subjectPrefix; - public function __construct(TransportInterface $transport, $redirectTo, $doNotSend, $fromName, $fromEmail, $subjectPrefix) + public function __construct(TransportInterface $transport) { $this->transport = $transport; - $this->redirectTo = $redirectTo; - $this->doNotSend = $doNotSend; - $this->fromName=$fromName; - $this->fromEmail=$fromEmail; - $this->subjectPrefix=$subjectPrefix; - } - - public function setRedirectTo(?array $redirectTo): void - { - $this->redirectTo = $redirectTo; - } - - public function setDoNotSend(bool $doNotSend): void - { - $this->doNotSend = $doNotSend; - } - - public function setFromName(?string $fromName): void - { - $this->fromName = $fromName; - } - public function setFromEmail(?string $fromEmail): void - { - $this->fromEmail = $fromEmail; - } - public function setSubjectPrefix(?string $subjectPrefix): void - { - $this->subjectPrefix = $subjectPrefix; } /** GESTION DES ENTITES *******************************************************************************************/ @@ -156,7 +128,7 @@ class MailService { try { $result = $qb->getQuery()->getOneOrNullResult(); } catch (NonUniqueResultException $e) { - throw new RuntimeException("Plusieurs Mail partagent le même id [".$id."]"); + throw new RuntimeException("Plusieurs Mail partagent le même id [".$id."]",0,$e); } return $result; } @@ -171,14 +143,38 @@ class MailService { /** FACADE ********************************************************************************************************/ /** - * @param $to - * @param $subject - * @param $texte - * @param null $attachement_path - * @return Mail + * @throws NotFoundConfigException + */ + public function fetchValueFromConfig(string $key, ?string $module = null) + { + $config = $this->config['module']; + $value = ($module)?$config[$module][$key]:null; + if ($value === null) $value = $config['default'][$key]; + if ($value === null) $value = $config[$key]; + + if ($value === null) { + throw new NotFoundConfigException("Aucun valeur de trouver dans la configuration de UnicaenMail pour la clef [".$key."]"); + } + return $value; + } + + /** + * @throws NotFoundConfigException */ - public function sendMail($to, $subject, $texte, $attachement_path = null) : Mail + public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null) : ?Mail { + $fromEmail = $this->fetchValueFromConfig('from_email', $module); + $fromName = $this->fetchValueFromConfig('from_name', $module); + $doNotSend = $this->fetchValueFromConfig('do_not_send', $module); + $redirect = $this->fetchValueFromConfig('redirect', $module); + $redirectTo = $this->fetchValueFromConfig('redirect_to', $module); + $subjectPrefix = $this->fetchValueFromConfig('subject_prefix', $module); + + if ($doNotSend === true) + { + return null; + } + if (is_string($to)) $to=explode(',', $to); if (!is_array($to)) $to = [$to]; @@ -187,36 +183,34 @@ class MailService { $mail->setDateEnvoi(new DateTime()); $mail->setStatusEnvoi(Mail::PENDING); $mail->setDestinataires(is_array($to) ? implode(",", $to) : $to); - $initialTo = $to; - if ($this->doNotSend) { + if ($redirect) { $mail->setDestinatairesInitials(implode(",",$to)); - $mail->setDestinataires(implode(",",$this->redirectTo)); - $to = $this->redirectTo; + $mail->setDestinataires(implode(",", $redirectTo)); } else { $mail->setDestinataires(implode(",",$to)); } $mail->setSujet($subject); - $sujet = '['.$this->subjectPrefix.'] ' . $subject; - if ($this->doNotSend) { + $sujet = '['.$subjectPrefix.'] ' . $subject; + if ($redirect) { $sujet .= ' {REDIR}'; } -// echo substr($subject, 88) ." : ini=".count($initialTo) . "/ cur=". count($to)."\n"; -// foreach ($initialTo as $item) echo $item . "\n"; -// echo "\n"; - $message = (new Message())->setEncoding('UTF-8'); - $message->setFrom($this->fromEmail, $this->fromName); - $message->setTo($to); + $message->setFrom($fromEmail, $fromName); + if ($redirect) { + $message->setTo($redirectTo); + } else { + $message->setTo($to); + } $message->setSubject($sujet); - $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$this->subjectPrefix.". </i></p>" . $texte; + $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$subjectPrefix.". </i></p>" . $texte; $mail->setCorps($texte); $this->create($mail); - if ($this->doNotSend) { + if ($redirect) { $texte .= "<br/><br/><hr/><br/>"; $texte .= "Initialement envoyé à :"; $texte .= "<ul>"; @@ -248,6 +242,12 @@ class MailService { $body->setParts($parts); $message->setBody($body); +// var_dump($fromEmail); +// var_dump($fromName); +// var_dump($doNotSend); +// var_dump($redirectTo); +// var_dump($subjectPrefix); +// die(); $this->transport->send($message); $mail->setStatusEnvoi(Mail::SUCCESS); @@ -256,6 +256,7 @@ class MailService { return $mail; } + /** TODO : Le reenvoi ne tient pas compte du module ... */ public function reenvoi(Mail $mail) : Mail { //todo les pieces jointes diff --git a/src/UnicaenMail/Service/Mail/MailServiceFactory.php b/src/UnicaenMail/Service/Mail/MailServiceFactory.php index f4c85506018fcb767c223d36ea50fdf973cca464..b87c58f3048afc2be16985235f5cb6a51fa4fdc9 100644 --- a/src/UnicaenMail/Service/Mail/MailServiceFactory.php +++ b/src/UnicaenMail/Service/Mail/MailServiceFactory.php @@ -27,10 +27,8 @@ class MailServiceFactory { */ $entityManager = $container->get('doctrine.entitymanager.orm_default'); - $service = new MailService($transport, - $config['redirect_to'], $config['do_not_send'], - $config['from_name'], $config['from_email'], $config['subject_prefix'] - ); + $service = new MailService($transport); + $service->setConfig($config); $service->setEntityClass($config['mail_entity_class']); $service->setObjectManager($entityManager); return $service;