Notification email pour congés #5

Merged
afornerot merged 3 commits from notification into master 2020-06-16 11:16:01 +02:00
14 changed files with 295 additions and 1 deletions

View File

@@ -32,6 +32,7 @@
<variable type='string' name='schedule_allow_hosts' description="Hôtes authorisés à utiliser la base de données" multi='True'/>
<variable type='string' name='schedule_dbuser' description='Utilisateur du serveur de base de données'/>
<variable type='string' name='schedule_dbpass' description='Fichier de mot de passe du serveur'/>
<variable type='mail' name='schedule_email_global_notif' description='Email pour envoie de notifications'/>
</family>
</variables>

View File

@@ -32,6 +32,12 @@ APP_NAME=Schedule
APP_ENV=PROD
APP_CRON=false
# MAIL sendmail / smtp
MAILER_METHOD=sendmail
MAILER_URL=
MAILER_NOREPLY=noreply@noreply.fr
MAILER_DEFAULT_NOTIF=
# BDD
DATABASE_NAME=
DATABASE_USER=
@@ -46,3 +52,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=

View File

@@ -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.*",

View File

@@ -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],
];

View File

@@ -0,0 +1,5 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool:
type: file
path: '%kernel.project_dir%/var/spoolmail'

View File

@@ -0,0 +1,5 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool:
type: file
path: '%kernel.project_dir%/var/spoolmail'

View File

@@ -9,6 +9,9 @@ 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)%'
appMailnotif: '%env(resolve:MAILER_DEFAULT_NOTIF)%'
casHost: '%env(resolve:CAS_HOST)%'
casPort: '%env(resolve:CAS_PORT)%'
casPath: '%env(resolve:CAS_PATH)%'
@@ -59,3 +62,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"]

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
class SendMailCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->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('<fg=red>'.$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");
}
}

View File

@@ -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);
}

View File

@@ -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=[];

View File

@@ -0,0 +1,98 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
class notificationService
{
private $em;
protected $mailer;
protected $twig;
public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer, \Twig\Environment $twig)
{
$this->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);
$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(),
'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)
//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);
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);
$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(),
'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($this->container->getParameter('appMailnotif'))
->setCc($tbemails)
->setBody($bodyHtml, 'text/html');
$response = $this->mailer->send($message);
return $response;
}
}

View File

@@ -0,0 +1,25 @@
{% block body %}
{% autoescape %}
<h2>Date de la demande : {{ date|date("d/m/Y H:i") }} </h2>
<p>
<b>Utilisateur =</b> {{ username }}
</p>
<p>
<b>Début =</b> {{ start|date("d/m/Y H:i") }}
</p>
<p>
<b>Fin =</b> {{ end|date("d/m/Y H:i") }}
</p>
<p>
<b>Durée =</b> {{ duration }}
</p>
<p>
<b>Type =</b> Congé
</p>
<p>
<b>Lien pour valider =</b> {{ valid_link }}
</p>
{% endautoescape %}
{% endblock %}

View File

@@ -0,0 +1,23 @@
{% block body %}
{% autoescape %}
<h2>VALIDATION</h2>
<p>
<b>Utilisateur =</b> {{ username }}
</p>
<p>
<b>Début =</b> {{ start|date("d/m/Y H:i") }}
</p>
<p>
<b>Fin =</b> {{ end|date("d/m/Y H:i") }}
</p>
<p>
<b>Durée =</b> {{ duration }}
</p>
<p>
<b>Type =</b> Congé
</p>
{% endautoescape %}
{% endblock %}

View File

@@ -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