-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from roadiz/feature/passwordless-public-user
- Loading branch information
roadiz-ci
committed
Sep 18, 2024
1 parent
44384f8
commit c0bf179
Showing
6 changed files
with
132 additions
and
64 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
41 changes: 41 additions & 0 deletions
41
src/EventSubscriber/JwtAuthenticationSuccessEventSubscriber.php
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\EventSubscriber; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent; | ||
use RZ\Roadiz\CoreBundle\Entity\User; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final readonly class JwtAuthenticationSuccessEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private ManagerRegistry $managerRegistry, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'lexik_jwt_authentication.on_authentication_success' => 'onAuthenticationSuccess', | ||
AuthenticationSuccessEvent::class => 'onAuthenticationSuccess', | ||
]; | ||
} | ||
|
||
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event): void | ||
{ | ||
$user = $event->getUser(); | ||
|
||
if (!($user instanceof User)) { | ||
return; | ||
} | ||
|
||
$user->setLastLogin(new \DateTime()); | ||
$this->managerRegistry->getManager()->flush(); | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/Security/Authentication/JwtAuthenticationSuccessHandler.php
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Security\LoginLink; | ||
|
||
use RZ\Roadiz\CoreBundle\Bag\Settings; | ||
use RZ\Roadiz\CoreBundle\Entity\User; | ||
use RZ\Roadiz\CoreBundle\Mailer\EmailManagerFactory; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\LoginLink\LoginLinkDetails; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
final readonly class EmailLoginLinkSender implements LoginLinkSenderInterface | ||
{ | ||
public function __construct( | ||
private Settings $settingsBag, | ||
private EmailManagerFactory $emailManagerFactory, | ||
private TranslatorInterface $translator, | ||
private string $htmlTemplate = '@RoadizCore/email/users/login_link_email.html.twig', | ||
private string $txtTemplate = '@RoadizCore/email/users/login_link_email.txt.twig' | ||
) { | ||
} | ||
|
||
public function sendLoginLink(UserInterface $user, LoginLinkDetails $loginLinkDetails,): void | ||
{ | ||
if ($user instanceof User && !$user->isEnabled()) { | ||
throw new \InvalidArgumentException('User must be enabled to send a login link.'); | ||
} | ||
|
||
if (!\method_exists($user, 'getEmail')) { | ||
throw new \InvalidArgumentException('User implementation must have getEmail method.'); | ||
} | ||
|
||
if (null === $user->getEmail()) { | ||
throw new \InvalidArgumentException('User must have an email to send a login link.'); | ||
} | ||
|
||
$emailManager = $this->emailManagerFactory->create(); | ||
$emailContact = $this->settingsBag->get('email_sender', null); | ||
if (!\is_string($emailContact)) { | ||
throw new \InvalidArgumentException('Email sender must be a string.'); | ||
} | ||
$siteName = $this->settingsBag->get('site_name', null); | ||
if (!\is_string($siteName)) { | ||
throw new \InvalidArgumentException('Site name must be a string.'); | ||
} | ||
|
||
$emailManager->setAssignation([ | ||
'loginLink' => $loginLinkDetails->getUrl(), | ||
'expiresAt' => $loginLinkDetails->getExpiresAt(), | ||
'user' => $user, | ||
'site' => $siteName, | ||
'mailContact' => $emailContact, | ||
]); | ||
$emailManager->setEmailTemplate($this->htmlTemplate); | ||
$emailManager->setEmailPlainTextTemplate($this->txtTemplate); | ||
$emailManager->setSubject($this->translator->trans( | ||
'login_link.request' | ||
)); | ||
|
||
$emailManager->setReceiver($user->getEmail()); | ||
$emailManager->setSender([$emailContact => $siteName]); | ||
|
||
// Send the message | ||
$emailManager->send(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Security\LoginLink; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\LoginLink\LoginLinkDetails; | ||
|
||
interface LoginLinkSenderInterface | ||
{ | ||
public function sendLoginLink( | ||
UserInterface $user, | ||
LoginLinkDetails $loginLinkDetails, | ||
): void; | ||
} |
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