From 20d42d319acd76d163a3970715f23d5d482102a8 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Fri, 12 Jun 2020 15:18:53 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Envoie=20de=20notification=20=C3=A0=20la=20?= =?UTF-8?q?pose=20et=20la=20vadliation=20de=20cong=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schedule-2.0/.env | 11 +++ src/schedule-2.0/config/bundles.php | 1 + .../config/packages/swiftmailer.yaml | 5 ++ .../config/packages/test/swiftmailer.yaml | 5 ++ src/schedule-2.0/config/services.yaml | 11 +++ .../src/Command/SendMailCommand.php | 87 +++++++++++++++++++ .../src/Controller/EventController.php | 11 +++ .../src/Controller/ValidationController.php | 9 +- .../src/Service/notificationService.php | 75 ++++++++++++++++ .../Notif/attentevalidation.html.twig | 25 ++++++ .../templates/Notif/validation.html.twig | 23 +++++ 11 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 src/schedule-2.0/config/packages/swiftmailer.yaml create mode 100644 src/schedule-2.0/config/packages/test/swiftmailer.yaml create mode 100644 src/schedule-2.0/src/Command/SendMailCommand.php create mode 100644 src/schedule-2.0/src/Service/notificationService.php create mode 100644 src/schedule-2.0/templates/Notif/attentevalidation.html.twig create mode 100644 src/schedule-2.0/templates/Notif/validation.html.twig diff --git a/src/schedule-2.0/.env b/src/schedule-2.0/.env index 32372c0..3c5f4b5 100644 --- a/src/schedule-2.0/.env +++ b/src/schedule-2.0/.env @@ -32,6 +32,11 @@ APP_NAME=Schedule APP_ENV=PROD APP_CRON=false +# MAIL sendmail / smtp +MAILER_METHOD=sendmail +MAILER_URL= +MAILER_NOREPLY=noreply@noreply.fr + # BDD DATABASE_NAME= DATABASE_USER= @@ -46,3 +51,9 @@ CAS_USERNAME=username CAS_EMAIL=email CAS_LASTNAME=lastname CAS_FIRSTNAME=firstname + +###> symfony/swiftmailer-bundle ### +# For Gmail as a transport, use: "gmail://username:password@localhost" +# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" +# Delivery is disabled by default via "null://localhost" +MAILER_URL= \ No newline at end of file diff --git a/src/schedule-2.0/config/bundles.php b/src/schedule-2.0/config/bundles.php index 36f3dcf..40aec7b 100644 --- a/src/schedule-2.0/config/bundles.php +++ b/src/schedule-2.0/config/bundles.php @@ -19,4 +19,5 @@ return [ Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle::class => ['all' => true], Oneup\UploaderBundle\OneupUploaderBundle::class => ['all' => true], Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true], + Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], ]; diff --git a/src/schedule-2.0/config/packages/swiftmailer.yaml b/src/schedule-2.0/config/packages/swiftmailer.yaml new file mode 100644 index 0000000..db8d136 --- /dev/null +++ b/src/schedule-2.0/config/packages/swiftmailer.yaml @@ -0,0 +1,5 @@ +swiftmailer: + url: '%env(MAILER_URL)%' + spool: + type: file + path: '%kernel.project_dir%/var/spoolmail' \ No newline at end of file diff --git a/src/schedule-2.0/config/packages/test/swiftmailer.yaml b/src/schedule-2.0/config/packages/test/swiftmailer.yaml new file mode 100644 index 0000000..db8d136 --- /dev/null +++ b/src/schedule-2.0/config/packages/test/swiftmailer.yaml @@ -0,0 +1,5 @@ +swiftmailer: + url: '%env(MAILER_URL)%' + spool: + type: file + path: '%kernel.project_dir%/var/spoolmail' \ No newline at end of file diff --git a/src/schedule-2.0/config/services.yaml b/src/schedule-2.0/config/services.yaml index 1ac6453..16a1acd 100644 --- a/src/schedule-2.0/config/services.yaml +++ b/src/schedule-2.0/config/services.yaml @@ -9,6 +9,8 @@ parameters: appName: '%env(resolve:APP_NAME)%' appEnv: '%env(resolve:APP_ENV)%' appCron: '%env(resolve:APP_CRON)%' + appMailmethod: '%env(resolve:MAILER_METHOD)%' + appMailnoreply: '%env(resolve:MAILER_NOREPLY)%' casHost: '%env(resolve:CAS_HOST)%' casPort: '%env(resolve:CAS_PORT)%' casPath: '%env(resolve:CAS_PATH)%' @@ -59,3 +61,12 @@ services: tags: - { name: kernel.event_listener, event: kernel.request, method: onDomainParse } + app.sendmail.transport: + class: Swift_SendmailTransport + public: true + arguments: ['/usr/sbin/sendmail -t'] + + app.mail.service: + public: true + class: App\Service\mailService + arguments: ["@mailer", "@twig"] diff --git a/src/schedule-2.0/src/Command/SendMailCommand.php b/src/schedule-2.0/src/Command/SendMailCommand.php new file mode 100644 index 0000000..c3b2833 --- /dev/null +++ b/src/schedule-2.0/src/Command/SendMailCommand.php @@ -0,0 +1,87 @@ +container = $container; + $this->em = $em; + } + + protected function configure() + { + $this + ->setName('app:sendMail') + ->setDescription('Envoi des mails') + ->setHelp('Envoi des mails') + ->addArgument('message-limit', InputArgument::OPTIONAL, 'limit message Mail') + ->addArgument('env', InputArgument::OPTIONAL, 'env Mail') + ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') + ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + $this->filesystem = new Filesystem(); + $this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/"; + + $this->writelnred(''); + $this->writelnred('== app:sendMail'); + $this->writelnred('=========================================================================================================='); + + $appMailmethod=$this->container->getParameter("appMailmethod"); + + $command = $this->getApplication()->find("swiftmailer:spool:send"); + $tbparameter["--message-limit"]=100; + $tbparameter["--env"]="prod"; + $tbparameter["--time-limit"]=5; + if($appMailmethod=="sendmail") $tbparameter["--transport"]="app.sendmail.transport"; + + + $parameter = new ArrayInput($tbparameter); + try{ + $returnCode = $command->run($parameter, $output); + } + catch(\Exception $e) { + $this->writeln(""); + $this->writelnred("Impossible d'envoyer des mails"); + $this->writeln(""); + return 0; + } + $this->writeln(""); + + return 1; + + } + + private function writelnred($string) { + $this->output->writeln(''.$string.''); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + private function writeln($string) { + $this->output->writeln($string); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + +} diff --git a/src/schedule-2.0/src/Controller/EventController.php b/src/schedule-2.0/src/Controller/EventController.php index 1e7e4cd..bc3b5c1 100755 --- a/src/schedule-2.0/src/Controller/EventController.php +++ b/src/schedule-2.0/src/Controller/EventController.php @@ -17,6 +17,11 @@ class EventController extends AbstractController private $route = "app_event"; private $render = "Event/"; private $entity = "App:Event"; + private $notificator; + + public function __construct(\App\Service\notificationService $notificator) { + $this->notificator = $notificator; + } public function list(Request $request) { @@ -245,6 +250,12 @@ class EventController extends AbstractController $em->persist($event); $em->flush(); + if($task->getNature()->getIsvacation()){ + $idevent=$event->getId(); + $valid_url = $this->generateUrl('app_validationholiday'); + $this->notificator->sendNotifAttenteValid("Congés en attente de validation", $iduser, $idevent, $valid_url); + + } $output=$this->formatEvent($event); } diff --git a/src/schedule-2.0/src/Controller/ValidationController.php b/src/schedule-2.0/src/Controller/ValidationController.php index 66bc702..f8cc772 100755 --- a/src/schedule-2.0/src/Controller/ValidationController.php +++ b/src/schedule-2.0/src/Controller/ValidationController.php @@ -10,7 +10,11 @@ use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; class ValidationController extends AbstractController { private $knpSnappy; - public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; } + private $notificator; + public function __construct(\Knp\Snappy\Pdf $knpSnappy, \App\Service\notificationService $notificator) { + $this->knpSnappy = $knpSnappy; + $this->notificator = $notificator; + } public function validation(Request $request) { @@ -510,6 +514,9 @@ class ValidationController extends AbstractController $event->setValidateholiday(true); $em->persist($event); $em->flush(); + $iduser=$this->get("session")->get("iduser"); + $idevent=$event->getId(); + $this->notificator->sendNotifValid("Congé validé", $iduser, $event); } $output=[]; diff --git a/src/schedule-2.0/src/Service/notificationService.php b/src/schedule-2.0/src/Service/notificationService.php new file mode 100644 index 0000000..4c70c68 --- /dev/null +++ b/src/schedule-2.0/src/Service/notificationService.php @@ -0,0 +1,75 @@ +mailer = $mailer; + $this->twig = $twig; + $this->em = $em; + } + + public function sendNotifAttenteValid($subject, $iduser, $idevent, $valid_url) + { + $template = $this->twig->load('Notif/attentevalidation.html.twig'); + + + $user=$this->em->getRepository("App:User")->find($iduser); + $event=$this->em->getRepository("App:Event")->find($idevent); + + $parameters=[ + 'date' => new \DateTime(), + 'username' => $user->getUsername(), + 'start' => $event->getStart(), + 'end' => $event->getEnd(), + 'duration' => $event->getDuration(), + 'valid_link' => $valid_url, + ]; + $bodyHtml = $template->renderBlock('body', $parameters); + + $message = (new \Swift_Message()) + ->setFrom('schedule@cadoles.com') + ->setSubject($user->getUsername() . " = " . $subject) + ->setTo('conges@cadoles.com') + ->setBody($bodyHtml, 'text/html'); + + $response = $this->mailer->send($message); + + return $response; + } + public function sendNotifValid($subject, $iduser, $idevent) + { + $template = $this->twig->load('Notif/validation.html.twig'); + + + $user=$this->em->getRepository("App:User")->find($iduser); + $event=$this->em->getRepository("App:Event")->find($idevent); + + $parameters=[ + 'date' => new \DateTime(), + 'username' => $user->getUsername(), + 'start' => $event->getStart(), + 'end' => $event->getEnd(), + 'duration' => $event->getDuration(), + ]; + $bodyHtml = $template->renderBlock('body', $parameters); + + $message = (new \Swift_Message()) + ->setFrom('schedule@cadoles.com') + ->setSubject($user->getUsername() . " = " . $subject) + ->setTo('conges@cadoles.com') + ->setBody($bodyHtml, 'text/html'); + + $response = $this->mailer->send($message); + + + return $response; + } +} diff --git a/src/schedule-2.0/templates/Notif/attentevalidation.html.twig b/src/schedule-2.0/templates/Notif/attentevalidation.html.twig new file mode 100644 index 0000000..a224421 --- /dev/null +++ b/src/schedule-2.0/templates/Notif/attentevalidation.html.twig @@ -0,0 +1,25 @@ +{% block body %} +{% autoescape %} +

Date de la demande : {{ date|date("d/m/Y H:i") }}

+

+ Utilisateur = {{ username }} +

+

+ Début = {{ start|date("d/m/Y H:i") }} +

+

+ Fin = {{ end|date("d/m/Y H:i") }} +

+

+ Durée = {{ duration }} +

+

+ Type = Congé +

+

+ Lien pour valider = {{ valid_link }} +

+ +{% endautoescape %} +{% endblock %} + diff --git a/src/schedule-2.0/templates/Notif/validation.html.twig b/src/schedule-2.0/templates/Notif/validation.html.twig new file mode 100644 index 0000000..4a35676 --- /dev/null +++ b/src/schedule-2.0/templates/Notif/validation.html.twig @@ -0,0 +1,23 @@ +{% block body %} +{% autoescape %} +

VALIDATION

+

+ Utilisateur = {{ username }} +

+

+ Début = {{ start|date("d/m/Y H:i") }} +

+

+ Fin = {{ end|date("d/m/Y H:i") }} +

+

+ Durée = {{ duration }} +

+

+ Type = Congé +

+ + +{% endautoescape %} +{% endblock %} + -- 2.17.1 From efa4f1fc62c8755adb4af1a328bcbc26c419e3cc Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Fri, 12 Jun 2020 16:58:23 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Envoie=20des=20notifications=20=C3=A0=20tou?= =?UTF-8?q?s=20les=20valideurs,=20et=20variable=20pour=20messagerie=20glob?= =?UTF-8?q?ale=20de=20notification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dicos/25_schedule.xml | 1 + src/schedule-2.0/.env | 1 + src/schedule-2.0/config/services.yaml | 1 + .../src/Service/notificationService.php | 41 +++++++++++++++---- tmpl/schedule-env.local | 6 +++ 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/dicos/25_schedule.xml b/dicos/25_schedule.xml index fc07576..e4a3ff2 100644 --- a/dicos/25_schedule.xml +++ b/dicos/25_schedule.xml @@ -32,6 +32,7 @@ + diff --git a/src/schedule-2.0/.env b/src/schedule-2.0/.env index 3c5f4b5..c784546 100644 --- a/src/schedule-2.0/.env +++ b/src/schedule-2.0/.env @@ -36,6 +36,7 @@ APP_CRON=false MAILER_METHOD=sendmail MAILER_URL= MAILER_NOREPLY=noreply@noreply.fr +MAILER_DEFAULT_NOTIF= # BDD DATABASE_NAME= diff --git a/src/schedule-2.0/config/services.yaml b/src/schedule-2.0/config/services.yaml index 16a1acd..29a975b 100644 --- a/src/schedule-2.0/config/services.yaml +++ b/src/schedule-2.0/config/services.yaml @@ -11,6 +11,7 @@ parameters: appCron: '%env(resolve:APP_CRON)%' appMailmethod: '%env(resolve:MAILER_METHOD)%' appMailnoreply: '%env(resolve:MAILER_NOREPLY)%' + appMailnotif: '%env(resolve:MAILER_DEFAULT_NOTIF)%' casHost: '%env(resolve:CAS_HOST)%' casPort: '%env(resolve:CAS_PORT)%' casPath: '%env(resolve:CAS_PATH)%' diff --git a/src/schedule-2.0/src/Service/notificationService.php b/src/schedule-2.0/src/Service/notificationService.php index 4c70c68..1c754af 100644 --- a/src/schedule-2.0/src/Service/notificationService.php +++ b/src/schedule-2.0/src/Service/notificationService.php @@ -19,11 +19,27 @@ class notificationService public function sendNotifAttenteValid($subject, $iduser, $idevent, $valid_url) { $template = $this->twig->load('Notif/attentevalidation.html.twig'); - - $user=$this->em->getRepository("App:User")->find($iduser); $event=$this->em->getRepository("App:Event")->find($idevent); - + $users=$this->em->getRepository("App:User")->findAll(); + $tbemails=[]; + foreach($users as $usr) { + if(in_array("ROLE_VALIDATOR",$usr->getRoles())) { + array_push($tbemails,$usr->getEmail()); + } + // else{ + // if(in_array("ROLE_MASTER",$usr->getRoles())) { + // if ($usr->getService() == $user->getService() ){ + // $tmp=[ + // "id"=>$usr->getId(), + // "email"=>$usr->getEmail() + // ]; + // } + // array_push($tbemails,$tmp); + // } + // } + } + $parameters=[ 'date' => new \DateTime(), 'username' => $user->getUsername(), @@ -33,11 +49,12 @@ class notificationService 'valid_link' => $valid_url, ]; $bodyHtml = $template->renderBlock('body', $parameters); - + $message = (new \Swift_Message()) ->setFrom('schedule@cadoles.com') ->setSubject($user->getUsername() . " = " . $subject) - ->setTo('conges@cadoles.com') + //TODO envoyer à tt les users master associé au service de l'utilisateur et l'ensemble des user validator + ->setTo($tbemails) ->setBody($bodyHtml, 'text/html'); $response = $this->mailer->send($message); @@ -47,11 +64,16 @@ class notificationService public function sendNotifValid($subject, $iduser, $idevent) { $template = $this->twig->load('Notif/validation.html.twig'); - - $user=$this->em->getRepository("App:User")->find($iduser); $event=$this->em->getRepository("App:Event")->find($idevent); - + $users=$this->em->getRepository("App:User")->findAll(); + $tbemails=[]; + foreach($users as $usr) { + if(in_array("ROLE_VALIDATOR",$usr->getRoles())) { + array_push($tbemails,$usr->getEmail()); + } + + } $parameters=[ 'date' => new \DateTime(), 'username' => $user->getUsername(), @@ -64,7 +86,8 @@ class notificationService $message = (new \Swift_Message()) ->setFrom('schedule@cadoles.com') ->setSubject($user->getUsername() . " = " . $subject) - ->setTo('conges@cadoles.com') + ->setTo($this->container->getParameter('appMailnotif')) + ->setCc($tbemails) ->setBody($bodyHtml, 'text/html'); $response = $this->mailer->send($message); diff --git a/tmpl/schedule-env.local b/tmpl/schedule-env.local index 31f3ab6..7815fe1 100644 --- a/tmpl/schedule-env.local +++ b/tmpl/schedule-env.local @@ -4,6 +4,12 @@ APP_SECRET=%%pwdreader("","/var/www/html/schedule/.key") APP_AUTH=CAS +# MAIL sendmail / smtp +MAILER_METHOD=sendmail +MAILER_URL= +MAILER_NOREPLY=noreply@noreply.fr +MAILER_DEFAULT_NOTIF=%%getVar('schedule_email_global_notif', '') + # Bdd = Redefine local DATABASE_NAME=schedule DATABASE_USER=schedule -- 2.17.1 From 79b8fb9af54884f2187fdef2ac05fa8ac0959827 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Tue, 16 Jun 2020 11:11:19 +0200 Subject: [PATCH 3/3] add dependencies --- src/schedule-2.0/composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/schedule-2.0/composer.json b/src/schedule-2.0/composer.json index 6d855bb..81bd9ec 100644 --- a/src/schedule-2.0/composer.json +++ b/src/schedule-2.0/composer.json @@ -33,6 +33,7 @@ "symfony/profiler-pack": "^1.0", "symfony/security-bundle": "4.4.*", "symfony/serializer-pack": "*", + "symfony/swiftmailer-bundle": "^3.4", "symfony/translation": "4.4.*", "symfony/twig-pack": "*", "symfony/validator": "4.4.*", -- 2.17.1