Merge branch 'maintenance/deprecate-swiftmailer' into 'master'

DX: [mailer] deprecate the chill mailer based on swift mailer + fix RoleScopePickerType

Closes #90

See merge request Chill-Projet/chill-bundles!525
This commit is contained in:
Julien Fastré 2023-04-19 11:09:09 +00:00
commit 60ada2edce
12 changed files with 66 additions and 125 deletions

View File

@ -47,7 +47,6 @@
"symfony/monolog-bundle": "^3.5", "symfony/monolog-bundle": "^3.5",
"symfony/security-bundle": "^4.4", "symfony/security-bundle": "^4.4",
"symfony/serializer": "^5.3", "symfony/serializer": "^5.3",
"symfony/swiftmailer-bundle": "^3.5",
"symfony/templating": "^4.4", "symfony/templating": "^4.4",
"symfony/translation": "^4.4", "symfony/translation": "^4.4",
"symfony/twig-bundle": "^4.4", "symfony/twig-bundle": "^4.4",

View File

@ -120,7 +120,6 @@ class ListActivitiesByAccompanyingPeriodContext implements
public function contextGenerationDataDenormalize(DocGeneratorTemplate $template, $entity, array $data): array public function contextGenerationDataDenormalize(DocGeneratorTemplate $template, $entity, array $data): array
{ {
$denormalized = $this->accompanyingPeriodContext->contextGenerationDataDenormalize($template, $entity, $data); $denormalized = $this->accompanyingPeriodContext->contextGenerationDataDenormalize($template, $entity, $data);
foreach (['myActivitiesOnly', 'myWorksOnly'] as $k) { foreach (['myActivitiesOnly', 'myWorksOnly'] as $k) {

View File

@ -107,5 +107,4 @@ class AddressToReferenceMatcherController
true true
); );
} }
} }

View File

@ -60,7 +60,8 @@ class ScopePickerType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$items = array_filter( $items = array_values(
array_filter(
$this->authorizationHelper->getReachableScopes( $this->authorizationHelper->getReachableScopes(
$this->security->getUser(), $this->security->getUser(),
$options['role'] instanceof Role ? $options['role']->getRole() : $options['role'], $options['role'] instanceof Role ? $options['role']->getRole() : $options['role'],
@ -69,6 +70,7 @@ class ScopePickerType extends AbstractType
static function (Scope $s) { static function (Scope $s) {
return $s->isActive(); return $s->isActive();
} }
)
); );
if (0 === count($items)) { if (0 === count($items)) {

View File

@ -15,7 +15,10 @@ use Chill\MainBundle\Entity\User;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Swift_Mailer; use Swift_Mailer;
use Swift_Message; use Swift_Message;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment; use Twig\Environment;
@ -26,43 +29,34 @@ use function call_user_func;
* Classe d'aide pour l'envoi de notification. * Classe d'aide pour l'envoi de notification.
* *
* Héberge toutes les méthodes pour -écrire les URL en fonction de la langue de l'utilisateur. * Héberge toutes les méthodes pour -écrire les URL en fonction de la langue de l'utilisateur.
*
* @deprecated use the MailerInterface
*/ */
class Mailer class Mailer
{ {
/**
* @var Swift_Mailer
*/
protected $forcedMailer;
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
protected $logger; private $logger;
/**
* @var Swift_Mailer
*/
protected $mailer;
/** /**
* @var array * @var array
*/ */
protected $routeParameters; private $routeParameters;
/** /**
* @var RouterInterface * @var RouterInterface
*/ */
protected $router; private $router;
/** /**
* @var TranslatorInterface * @var TranslatorInterface
*/ */
protected $translator; private $translator;
/** private EngineInterface $twig;
* @var \Twig\Environment
*/ private MailerInterface $mailer;
protected $twig;
/** /**
* Mailer constructor. * Mailer constructor.
@ -70,11 +64,9 @@ class Mailer
* @param $routeParameters * @param $routeParameters
*/ */
public function __construct( public function __construct(
MailerInterface $mailer,
LoggerInterface $logger, LoggerInterface $logger,
Environment $twig, EngineInterface $twig,
Swift_Mailer $mailer,
// due to bug https://github.com/symfony/swiftmailer-bundle/issues/127
// \Swift_Transport $mailerTransporter,
RouterInterface $router, RouterInterface $router,
TranslatorInterface $translator, TranslatorInterface $translator,
$routeParameters $routeParameters
@ -82,7 +74,6 @@ class Mailer
$this->logger = $logger; $this->logger = $logger;
$this->twig = $twig; $this->twig = $twig;
$this->mailer = $mailer; $this->mailer = $mailer;
//$this->forcedMailer = new \Swift_Mailer($mailerTransporter);
$this->router = $router; $this->router = $router;
$this->translator = $translator; $this->translator = $translator;
$this->routeParameters = $routeParameters; $this->routeParameters = $routeParameters;
@ -115,20 +106,6 @@ class Mailer
return $content; return $content;
} }
/**
* @param $force
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function sendMessage(Swift_Message $message, $force)
{
if ($force) {
$this->forcedMailer->send($message);
} else {
$this->mailer->send($message);
}
}
/** /**
* Envoie une notification à un utilisateur. * Envoie une notification à un utilisateur.
* *
@ -155,23 +132,25 @@ class Mailer
$subject[2] ?? null $subject[2] ?? null
); );
$message = (new Swift_Message($subjectI18n)) $email = new Email();
->setFrom($fromEmail, $fromName) $email->addTo($to)->subject($subjectI18n);
->setTo($to);
foreach ($bodies as $contentType => $content) { foreach ($bodies as $contentType => $content) {
$message->setBody($content, $contentType); match ($contentType) {
'text/plain' => $email->text($content),
default => $email->text($content),
};
} }
if (null !== $callback) { if (null !== $callback) {
call_user_func($callback, $message); call_user_func($callback, $email);
} }
$this->logger->info('[notification] Sending notification', [ $this->logger->info('[notification] Sending notification', [
'to' => $message->getTo(), 'to' => $email->getTo(),
'subject' => $message->getSubject(), 'subject' => $email->getSubject()
]); ]);
$this->sendMessage($message, $force); $this->mailer->send($email);
} }
} }

View File

@ -14,6 +14,8 @@ namespace Chill\MainBundle\Security\PasswordRecover;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Notification\Mailer; use Chill\MainBundle\Notification\Mailer;
use DateTimeInterface; use DateTimeInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use function array_merge; use function array_merge;
@ -22,33 +24,26 @@ class RecoverPasswordHelper
{ {
public const RECOVER_PASSWORD_ROUTE = 'password_recover'; public const RECOVER_PASSWORD_ROUTE = 'password_recover';
/** private MailerInterface $mailer;
* @var Mailer
*/
protected $mailer;
protected $routeParameters;
/** /**
* @var TokenManager * @var TokenManager
*/ */
protected $tokenManager; private $tokenManager;
/** /**
* @var UrlGeneratorInterface * @var UrlGeneratorInterface
*/ */
protected $urlGenerator; private $urlGenerator;
public function __construct( public function __construct(
TokenManager $tokenManager, TokenManager $tokenManager,
UrlGeneratorInterface $urlGenerator, UrlGeneratorInterface $urlGenerator,
Mailer $mailer, MailerInterface $mailer,
array $routeParameters
) { ) {
$this->tokenManager = $tokenManager; $this->tokenManager = $tokenManager;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->routeParameters = $routeParameters;
} }
/** /**
@ -59,27 +54,14 @@ class RecoverPasswordHelper
*/ */
public function generateUrl(User $user, DateTimeInterface $expiration, $absolute = true, array $parameters = []) public function generateUrl(User $user, DateTimeInterface $expiration, $absolute = true, array $parameters = [])
{ {
$context = $this->urlGenerator->getContext(); return $this->urlGenerator->generate(
$previousHost = $context->getHost();
$previousScheme = $context->getScheme();
$context->setHost($this->routeParameters['host']);
$context->setScheme($this->routeParameters['scheme']);
$url = $this->urlGenerator->generate(
self::RECOVER_PASSWORD_ROUTE, self::RECOVER_PASSWORD_ROUTE,
array_merge( array_merge(
$this->tokenManager->generate($user, $expiration), $this->tokenManager->generate($user, $expiration),
$parameters $parameters
), ),
$absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH UrlGeneratorInterface::ABSOLUTE_URL
); );
// reset the host
$context->setHost($previousHost);
$context->setScheme($previousScheme);
return $url;
} }
public function sendRecoverEmail( public function sendRecoverEmail(
@ -91,26 +73,20 @@ class RecoverPasswordHelper
array $additionalUrlParameters = [], array $additionalUrlParameters = [],
$emailSubject = 'Recover your password' $emailSubject = 'Recover your password'
) { ) {
$content = $this->mailer->renderContentToUser( if (null === $user->getEmail() || '' === trim($user->getEmail())) {
$user, throw new \UnexpectedValueException("No emaail associated to the user");
$template, }
array_merge(
[ $email = (new TemplatedEmail())
->subject($emailSubject)
->to($user->getEmail())
->textTemplate($template)
->context([
'user' => $user, 'user' => $user,
'url' => $this->generateUrl($user, $expiration, true, $additionalUrlParameters), 'url' => $this->generateUrl($user, $expiration, true, $additionalUrlParameters),
], ...$templateParameters
$templateParameters ]);
)
);
$this->mailer->sendNotification( $this->mailer->send($email);
$user,
[$emailSubject],
[
'text/plain' => $content,
],
null,
$force
);
} }
} }

View File

@ -10,12 +10,6 @@ services:
Chill\MainBundle\Notification\Mailer: Chill\MainBundle\Notification\Mailer:
arguments: arguments:
$logger: '@Psr\Log\LoggerInterface'
$twig: '@Twig\Environment'
$mailer: '@swiftmailer.mailer.default'
# $mailerTransporter: '@swiftmailer.transport'
$router: '@Symfony\Component\Routing\RouterInterface'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
$routeParameters: '%chill_main.notifications%' $routeParameters: '%chill_main.notifications%'
Chill\MainBundle\Notification\NotificationHandlerManager: Chill\MainBundle\Notification\NotificationHandlerManager:

View File

@ -61,11 +61,7 @@ services:
arguments: arguments:
$secret: '%kernel.secret%' $secret: '%kernel.secret%'
Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper: Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper: ~
arguments:
$tokenManager: '@Chill\MainBundle\Security\PasswordRecover\TokenManager'
$mailer: '@Chill\MainBundle\Notification\Mailer'
$routeParameters: "%chill_main.notifications%"
Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEventSubscriber: Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEventSubscriber:
arguments: arguments:

View File

@ -69,7 +69,6 @@ final class Version20230306145728 extends AbstractMigration
$this->addSql('CREATE INDEX IDX_165051F63174800F ON chill_main_address (createdBy_id)'); $this->addSql('CREATE INDEX IDX_165051F63174800F ON chill_main_address (createdBy_id)');
$this->addSql('CREATE INDEX IDX_165051F665FF1AEC ON chill_main_address (updatedBy_id)'); $this->addSql('CREATE INDEX IDX_165051F665FF1AEC ON chill_main_address (updatedBy_id)');
$this->addSql('COMMENT ON COLUMN chill_main_address_reference.point IS \'(DC2Type:point)\''); $this->addSql('COMMENT ON COLUMN chill_main_address_reference.point IS \'(DC2Type:point)\'');
} }
public function down(Schema $schema): void public function down(Schema $schema): void

View File

@ -33,6 +33,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -205,7 +206,7 @@ final class SingleTaskController extends AbstractController
. 'allowed to edit this task'); . 'allowed to edit this task');
$event = (new UIEvent('single-task', $task)) $event = (new UIEvent('single-task', $task))
->setForm($this->setCreateForm($task, new Role(TaskVoter::UPDATE))); ->setForm($this->setCreateForm($task, TaskVoter::UPDATE));
$this->eventDispatcher->dispatch(UIEvent::EDIT_FORM, $event); $this->eventDispatcher->dispatch(UIEvent::EDIT_FORM, $event);
$form = $event->getForm(); $form = $event->getForm();
@ -557,7 +558,7 @@ final class SingleTaskController extends AbstractController
$this->denyAccessUnlessGranted($role, $task, 'You are not ' $this->denyAccessUnlessGranted($role, $task, 'You are not '
. 'allowed to create this task'); . 'allowed to create this task');
$form = $this->setCreateForm($task, new Role($role)); $form = $this->setCreateForm($task, $role);
$form->handleRequest($request); $form->handleRequest($request);
@ -650,7 +651,7 @@ final class SingleTaskController extends AbstractController
/** /**
* @return \Symfony\Component\Form\FormInterface * @return \Symfony\Component\Form\FormInterface
*/ */
protected function setCreateForm(SingleTask $task, Role $role) protected function setCreateForm(SingleTask $task, string $role)
{ {
$form = $this->createForm(SingleTaskType::class, $task, [ $form = $this->createForm(SingleTaskType::class, $task, [
'role' => $role, 'role' => $role,
@ -684,12 +685,9 @@ final class SingleTaskController extends AbstractController
/** /**
* Creates a form to delete a Task entity by id. * Creates a form to delete a Task entity by id.
* * @param mixed $id
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/ */
private function createDeleteForm($id) private function createDeleteForm($id): FormInterface
{ {
return $this->createFormBuilder() return $this->createFormBuilder()
->setAction($this->generateUrl( ->setAction($this->generateUrl(

View File

@ -81,7 +81,7 @@ class SingleTaskType extends AbstractType
->add('circle', ScopePickerType::class, [ ->add('circle', ScopePickerType::class, [
'center' => $center, 'center' => $center,
'role' => $options['role'], 'role' => $options['role'],
'required' => false, 'required' => true,
]); ]);
} }
} }