Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
<?php
namespace HiEvents\Services\Application\Handlers\Auth;
use HiEvents\DomainObjects\UserDomainObject;
use HiEvents\Enterprise\Sso\Services\Domain\SsoEnforcementService;
use HiEvents\Mail\User\ForgotPassword;
use HiEvents\Repository\Interfaces\PasswordResetTokenRepositoryInterface;
use HiEvents\Repository\Interfaces\UserRepositoryInterface;
use HiEvents\Services\Domain\Auth\PasswordLoginPolicy;
use HiEvents\Services\Domain\Auth\Sso\UserIdentityService;
use HiEvents\Services\Infrastructure\TokenGenerator\TokenGeneratorService;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Database\DatabaseManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Throwable;
class ForgotPasswordHandler
{
public function __construct(
private readonly UserRepositoryInterface $userRepository,
private readonly Mailer $mailer,
private readonly LoggerInterface $logger,
private readonly PasswordResetTokenRepositoryInterface $passwordResetTokenRepository,
private readonly TokenGeneratorService $tokenGeneratorService,
private readonly DatabaseManager $databaseManager,
private readonly PasswordLoginPolicy $passwordLoginPolicy,
private readonly UserIdentityService $userIdentityService,
private readonly SsoEnforcementService $ssoEnforcementService,
) {}
/**
* @throws Throwable
*/
public function handle(string $email): void
{
$email = strtolower($email);
$this->databaseManager->transaction(function () use ($email) {
$user = $this->findUserByEmail($email);
$token = $this->generateAndSaveResetToken($email);
$this->sendResetPasswordEmail($user, $token);
});
}
private function findUserByEmail(string $email)
{
$user = $this->userRepository->findFirstWhere([
'email' => strtolower($email),
]
);
if ($user === null) {
$this->logUnrecognisedEmail($email);
throw new ResourceNotFoundException;
}
if (! $this->passwordLoginPolicy->isAllowedFor($user->getId()) || ! $this->ssoEnforcementService->isPasswordResetAllowed($user)) {
throw new ResourceNotFoundException;
}
return $user;
}
private function generateAndSaveResetToken(string $email): string
{
$token = $this->tokenGeneratorService->generateToken(prefix: 'rp');
$this->passwordResetTokenRepository->deleteWhere(['email' => $email]);
$this->passwordResetTokenRepository->create([
'email' => $email,
'token' => $token,
]);
return $token;
}
private function sendResetPasswordEmail(UserDomainObject $user, string $token): void
{
$this->logger->info('resetting password for user', [
'user' => $user->getId(),
'email' => $user->getEmail(),
]);
$this->mailer
->to($user->getEmail())
->locale($user->getLocale())
->send(new ForgotPassword(
user: $user,
token: $token,
signInProviders: $this->userIdentityService->providerNamesFor($user->getId()),
));
}
private function logUnrecognisedEmail(string $email): void
{
$this->logger->info('unrecognised email for password reset', [
'email' => $email,
]);
}
}