-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'wip' into feat/1419-user-notifier
- Loading branch information
Showing
5 changed files
with
45 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,11 @@ | |
namespace App\Security; | ||
|
||
use App\Entity\User; | ||
use Psr\Log\LoggerInterface; | ||
use Scheb\TwoFactorBundle\Model\BackupCodeInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\Backup\BackupCodeManagerInterface; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Twig\Environment; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
|
||
/** | ||
* @author Colin O'Dell <[email protected]> | ||
|
@@ -32,12 +26,8 @@ class TwoFactorAuthManager implements BackupCodeManagerInterface | |
{ | ||
public function __construct( | ||
private ManagerRegistry $doctrine, | ||
private MailerInterface $mailer, | ||
private Environment $twig, | ||
private LoggerInterface $logger, | ||
private RequestStack $requestStack, | ||
/** @var array{from: string, fromName: string} */ | ||
private array $options | ||
private UserNotifier $userNotifier, | ||
) { | ||
} | ||
|
||
|
@@ -49,22 +39,7 @@ public function enableTwoFactorAuth(User $user, string $secret): void | |
$user->setTotpSecret($secret); | ||
$this->doctrine->getManager()->flush(); | ||
|
||
$body = $this->twig->render('email/two_factor_enabled.txt.twig', [ | ||
'username' => $user->getUsername(), | ||
]); | ||
|
||
$message = (new Email()) | ||
->subject('[Packagist] Two-factor authentication enabled') | ||
->from(new Address($this->options['from'], $this->options['fromName'])) | ||
->to($user->getEmail()) | ||
->text($body) | ||
; | ||
|
||
try { | ||
$this->mailer->send($message); | ||
} catch (TransportExceptionInterface $e) { | ||
$this->logger->error('['.get_class($e).'] '.$e->getMessage()); | ||
} | ||
$this->userNotifier->notifyTwoFactorAuth($user->getEmail(), ['username' => $user->getUsername()], true); | ||
} | ||
|
||
/** | ||
|
@@ -76,23 +51,7 @@ public function disableTwoFactorAuth(User $user, string $reason): void | |
$user->invalidateAllBackupCodes(); | ||
$this->doctrine->getManager()->flush(); | ||
|
||
$body = $this->twig->render('email/two_factor_disabled.txt.twig', [ | ||
'username' => $user->getUsername(), | ||
'reason' => $reason, | ||
]); | ||
|
||
$message = (new Email()) | ||
->subject('[Packagist] Two-factor authentication disabled') | ||
->from(new Address($this->options['from'], $this->options['fromName'])) | ||
->to($user->getEmail()) | ||
->text($body) | ||
; | ||
|
||
try { | ||
$this->mailer->send($message); | ||
} catch (TransportExceptionInterface $e) { | ||
$this->logger->error('['.get_class($e).'] '.$e->getMessage()); | ||
} | ||
$this->userNotifier->notifyTwoFactorAuth($user->getEmail(), ['username' => $user->getUsername(), 'reason' => $reason,], false); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @author Pierre Ambroise <[email protected]> | ||
|
@@ -14,25 +16,50 @@ class UserNotifier | |
private MailerInterface $mailer; | ||
private string $mailFromEmail; | ||
private string $mailFromName; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct(string $mailFromEmail, string $mailFromName, MailerInterface $mailer) | ||
public function __construct(string $mailFromEmail, string $mailFromName, MailerInterface $mailer, LoggerInterface $logger) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->mailFromEmail = $mailFromEmail; | ||
$this->mailFromName = $mailFromName; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function notifyChange(string $email, string $reason): void | ||
{ | ||
$email = (new TemplatedEmail()) | ||
$this->notify($email, 'A change has been made to your account', 'email/alert_change.txt.twig', ['change' => $reason]); | ||
} | ||
|
||
/** | ||
* @param string[] $context | ||
*/ | ||
public function notifyTwoFactorAuth(string $email, array $context, bool $enabled): void | ||
{ | ||
$this->notify( | ||
$email, | ||
$enabled ? '[Packagist] Two-factor authentication enabled' : '[Packagist] Two-factor authentication disabled', | ||
$enabled ? 'email/two_factor_enabled.txt.twig' : 'email/two_factor_disabled.txt.twig', | ||
$context | ||
); | ||
} | ||
|
||
/** | ||
* @param string[] $context | ||
*/ | ||
public function notify(string $email, string $subject, string $template, array $context = []): void | ||
{ | ||
$mail = (new TemplatedEmail()) | ||
->from(new Address($this->mailFromEmail, $this->mailFromName)) | ||
->to($email) | ||
->subject('A change has been made to your account') | ||
->textTemplate('email/alert_change.txt.twig') | ||
->context([ | ||
'reason' => $reason, | ||
]); | ||
->subject($subject) | ||
->textTemplate($template) | ||
->context($context); | ||
|
||
$this->mailer->send($email); | ||
try { | ||
$this->mailer->send($mail); | ||
} catch (TransportExceptionInterface $e) { | ||
$this->logger->error('[' . get_class($e) . '] ' . $e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters