Skip to content

Commit

Permalink
feat(UserViewer): Removed setUser setter for single method usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Mar 29, 2023
1 parent 297f099 commit b7c0f75
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public function prePersist(LifecycleEventArgs $event): void
$user->setPasswordRequestedAt(new \DateTime());
$user->setConfirmationToken($tokenGenerator->generateToken());

$this->userViewer->setUser($user);
$this->userViewer->sendPasswordResetLink(
$user,
'loginResetPage',
'@RoadizCore/email/users/welcome_user_email.html.twig',
'@RoadizCore/email/users/welcome_user_email.txt.twig'
Expand Down
46 changes: 16 additions & 30 deletions lib/RoadizCoreBundle/src/Security/User/UserViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use Psr\Log\LoggerInterface;
use RZ\Roadiz\CoreBundle\Bag\Settings;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\User;
use RZ\Roadiz\CoreBundle\Mailer\EmailManager;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

class UserViewer
{
Expand All @@ -21,7 +24,6 @@ class UserViewer
protected TranslatorInterface $translator;
protected EmailManager $emailManager;
protected LoggerInterface $logger;
protected ?User $user = null;

public function __construct(
Settings $settingsBag,
Expand All @@ -40,29 +42,31 @@ public function __construct(
/**
* Send email to reset user password.
*
* @param string|NodesSources $route
* @param User $user
* @param object|string $route
* @param string $htmlTemplate
* @param string $txtTemplate
*
* @return bool
* @throws \Exception
* @throws TransportExceptionInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function sendPasswordResetLink(
$route = 'loginResetPage',
User $user,
object|string $route = 'loginResetPage',
string $htmlTemplate = '@RoadizCore/email/users/reset_password_email.html.twig',
string $txtTemplate = '@RoadizCore/email/users/reset_password_email.txt.twig'
): bool {
if (null === $this->user) {
throw new \InvalidArgumentException('User should be defined before sending email.');
}
$emailContact = $this->getContactEmail();
$siteName = $this->getSiteName();

if (is_string($route)) {
$resetLink = $this->urlGenerator->generate(
$route,
[
'token' => $this->user->getConfirmationToken(),
'token' => $user->getConfirmationToken(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
Expand All @@ -71,14 +75,14 @@ public function sendPasswordResetLink(
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
[
RouteObjectInterface::ROUTE_OBJECT => $route,
'token' => $this->user->getConfirmationToken(),
'token' => $user->getConfirmationToken(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
$this->emailManager->setAssignation([
'resetLink' => $resetLink,
'user' => $this->user,
'user' => $user,
'site' => $siteName,
'mailContact' => $emailContact,
]);
Expand All @@ -87,7 +91,7 @@ public function sendPasswordResetLink(
$this->emailManager->setSubject($this->translator->trans(
'reset.password.request'
));
$this->emailManager->setReceiver($this->user->getEmail());
$this->emailManager->setReceiver($user->getEmail());
$this->emailManager->setSender([$emailContact => $siteName]);

try {
Expand Down Expand Up @@ -129,22 +133,4 @@ protected function getSiteName(): string

return $siteName;
}

/**
* @return null|User
*/
public function getUser(): ?User
{
return $this->user;
}

/**
* @param null|User $user
* @return UserViewer
*/
public function setUser(?User $user)
{
$this->user = $user;
return $this;
}
}
3 changes: 1 addition & 2 deletions lib/RoadizCoreBundle/src/Traits/LoginRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function sendConfirmationEmail(
$user->setConfirmationToken($tokenGenerator->generateToken());
$entityManager->flush();
$userViewer = $this->getUserViewer();
$userViewer->setUser($user);
$userViewer->sendPasswordResetLink($resetRoute);
$userViewer->sendPasswordResetLink($user, $resetRoute);
return true;
} catch (\Exception $e) {
$user->setPasswordRequestedAt(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function getUserViewer(): UserViewer
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function indexAction(Request $request)
public function indexAction(Request $request): Response
{
$form = $this->createForm(LoginRequestForm::class);
$form->handleRequest($request);
Expand Down Expand Up @@ -71,7 +71,7 @@ public function indexAction(Request $request)
/**
* @return Response
*/
public function confirmAction()
public function confirmAction(): Response
{
return $this->render('@RoadizRozier/login/requestConfirm.html.twig', $this->assignation);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizUserBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
parameters:
env(USER_PASSWORD_RESET_URL): 'loginResetPage'
env(USER_VALIDATION_URL): 'http://example.test/my-account/validate'
env(USER_PASSWORD_RESET_EXPIRES_IN): '600'
env(USER_PASSWORD_RESET_EXPIRES_IN): '900'
env(USER_VALIDATION_EXPIRES_IN): '3600'

services:
Expand Down

0 comments on commit b7c0f75

Please sign in to comment.