Skip to content

Commit

Permalink
[UserBundle] email sent in proper locale
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Dec 18, 2023
1 parent 89199ad commit 36a3a56
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 11 deletions.
31 changes: 31 additions & 0 deletions app/migrations/Version20231218175905.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231218175905 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE draw_acme__user ADD preferred_locale VARCHAR(255) DEFAULT \'en\' NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE draw_acme__user DROP preferred_locale');
}
}
1 change: 1 addition & 0 deletions app/src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function load(ObjectManager $manager): void
$user->setPlainPassword('password');
if (1 === $number) {
$user->setTags([$inactiveTag]);
$user->setPreferredLocale('fr');
}
$manager->persist($user);
}
Expand Down
19 changes: 17 additions & 2 deletions app/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\ConfigurationTrait;
use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\TwoFactorAuthenticationUserInterface;
use Draw\Component\EntityMigrator\MigrationTargetEntityInterface;
use Draw\Component\Mailer\Recipient\LocalizationAwareInterface;
use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface;
use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderTrait;
use Draw\DoctrineExtra\Common\Collections\CollectionUtil;
Expand All @@ -34,7 +35,7 @@
#[ORM\Table(name: 'draw_acme__user')]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ['email'])]
class User implements MessageHolderInterface, SecurityUserInterface, TwoFactorAuthenticationUserInterface, PasswordChangeUserInterface, LockableUserInterface, TwoFactorInterface, ByEmailInterface, ByTimeBaseOneTimePasswordInterface, MigrationTargetEntityInterface
class User implements MessageHolderInterface, SecurityUserInterface, TwoFactorAuthenticationUserInterface, PasswordChangeUserInterface, LockableUserInterface, TwoFactorInterface, ByEmailInterface, ByTimeBaseOneTimePasswordInterface, MigrationTargetEntityInterface, LocalizationAwareInterface
{
use ByEmailTrait;
use ByTimeBaseOneTimePasswordTrait;
Expand Down Expand Up @@ -148,9 +149,11 @@ class User implements MessageHolderInterface, SecurityUserInterface, TwoFactorAu
private Collection $userTags;

#[Assert\NotNull]
#[Serializer\ReadOnlyProperty]
private string $requiredReadOnly = 'value';

#[ORM\Column(type: 'string', nullable: false, options: ['default' => 'en'])]
private string $preferredLocale = 'en';

public function __construct()
{
$this->address = new Address();
Expand All @@ -176,6 +179,18 @@ public function setId(string $id): void
$this->id = $id;
}

public function getPreferredLocale(): string
{
return $this->preferredLocale;
}

public function setPreferredLocale(string $preferredLocale): static
{
$this->preferredLocale = $preferredLocale;

return $this;
}

/**
* @see UserInterface
*/
Expand Down
2 changes: 2 additions & 0 deletions config/serializer/Entity/User.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ App\Entity\User:
exclude: true
emailAuthCodeGeneratedAt:
exclude: true
preferredLocale:
exclude: true
twoFactorAuthenticationEnabledProviders:
exclude: true
4 changes: 3 additions & 1 deletion packages/mailer/Email/CallToActionEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Symfony\Bridge\Twig\Mime\TemplatedEmail;

class CallToActionEmail extends TemplatedEmail
class CallToActionEmail extends TemplatedEmail implements LocalizeEmailInterface
{
use LocalizeEmailTrait;

private ?string $callToActionLink = null;

public array $translationTokens = [];
Expand Down
20 changes: 20 additions & 0 deletions packages/mailer/Email/LocalizeEmailTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Draw\Component\Mailer\Email;

trait LocalizeEmailTrait
{
private ?string $locale = null;

public function setLocale(?string $locale): self
{
$this->locale = $locale;

return $this;
}

public function getLocale(): ?string
{
return $this->locale;
}
}
8 changes: 8 additions & 0 deletions packages/mailer/Recipient/LocalizationAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Draw\Component\Mailer\Recipient;

interface LocalizationAwareInterface
{
public function getPreferredLocale(): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function forgotPasswordAction(
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$mailer->send(new ForgotPasswordEmail($form->get('email')->getData()));
$mailer->send(
(new ForgotPasswordEmail($form->get('email')->getData()))
->setLocale($request->getLocale())
);

return new RedirectResponse($this->generateUrl('admin_check_email'));
}
Expand Down
17 changes: 17 additions & 0 deletions packages/user-bundle/Email/ToUserEmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

namespace Draw\Bundle\UserBundle\Email;

use Draw\Bundle\UserBundle\Entity\SecurityUserInterface;
use Draw\Component\Mailer\Email\LocalizeEmailTrait;
use Draw\Component\Mailer\Recipient\LocalizationAwareInterface;

trait ToUserEmailTrait
{
use LocalizeEmailTrait;

private string|int|null $userId = null;

public function toUser(SecurityUserInterface $user): self
{
$this->userId = $user->getId();

if ($user instanceof LocalizationAwareInterface) {
$this->setLocale($user->getPreferredLocale());
}

return $this;
}

public function setUserId(string|int $userId): self
{
$this->userId = $userId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityRepository;
use Draw\Bundle\UserBundle\Email\UserOnboardingEmail;
use Draw\Bundle\UserBundle\Message\NewUserMessage;
use Draw\Component\Mailer\Recipient\LocalizationAwareInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -29,14 +30,18 @@ public function __invoke(NewUserMessage $message): void
{
$user = $this->drawUserEntityRepository->find($message->getUserId());

if (null === $user) {
return;
}

if (!method_exists($user, 'getEmail') || empty($user->getEmail())) {
return;
}

$this->mailer->send((new UserOnboardingEmail())->setUserId($message->getUserId()));
$this->mailer->send(
(new UserOnboardingEmail())
->setUserId($message->getUserId())
->setLocale(
$user instanceof LocalizationAwareInterface ?
$user->getPreferredLocale() :
null
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Draw\Bundle\UserBundle\Email\PasswordChangeRequestedEmail;
use Draw\Bundle\UserBundle\Entity\PasswordChangeUserInterface;
use Draw\Bundle\UserBundle\Message\PasswordChangeRequestedMessage;
use Draw\Component\Mailer\Recipient\LocalizationAwareInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -38,6 +39,14 @@ public function __invoke(PasswordChangeRequestedMessage $message): void
return;
}

$this->mailer->send((new PasswordChangeRequestedEmail())->setUserId($user->getId()));
$this->mailer->send(
(new PasswordChangeRequestedEmail())
->setUserId($user->getId())
->setLocale(
$user instanceof LocalizationAwareInterface
? $user->getPreferredLocale()
: null
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ email:
subject: 'Welcome'
top_section: |
<p>Your account have been created.<p>
<p>Please click on the button below to finally your account creation.</p>
<p>Please click on the button below to finalise your account creation.</p>
call_to_action_text: 'Finalise my account creation'
bottom_section: |
<p>Follow the instruction on the site to finalise your account creation.</p>
Expand Down
9 changes: 9 additions & 0 deletions packages/user-bundle/Resources/translations/DrawEmail.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ email:
bottom_section: |
<p>Si vous n'êtes pas l'auteur de cette demande, peut-être que quelqu'un à fait une erreur.</p>
<p>Peut-être seriez vous intéressé par notre site web ? Cliquer sur le lien plus haut pour en savoir plus.</p>
user_onboarding:
subject: 'Bienvenue'
top_section: |
<p>Votre compte a été créé.<p>
<p>Veuillez cliquer sur le bouton ci-dessous pour finaliser la création de votre compte.</p>
call_to_action_text: 'Finaliser la création de mon compte'
bottom_section: |
<p>Suivez les instructions sur le site pour finaliser la création de votre compte.</p>

0 comments on commit 36a3a56

Please sign in to comment.