instances added god may help
This commit is contained in:
@@ -2,13 +2,12 @@
|
||||
|
||||
namespace MintyPHP\Service\Auth;
|
||||
|
||||
use MintyPHP\Repository\Auth\PasswordResetRepository;
|
||||
use MintyPHP\Repository\User\UserRepository;
|
||||
use MintyPHP\I18n;
|
||||
use MintyPHP\Http\Request;
|
||||
use MintyPHP\Service\Auth\RememberMeService;
|
||||
use MintyPHP\I18n;
|
||||
use MintyPHP\Repository\Auth\PasswordResetRepository;
|
||||
use MintyPHP\Repository\User\UserReadRepository;
|
||||
use MintyPHP\Service\Mail\MailService;
|
||||
use MintyPHP\Service\User\UserService;
|
||||
use MintyPHP\Service\User\UserPasswordService;
|
||||
|
||||
class PasswordResetService
|
||||
{
|
||||
@@ -16,26 +15,35 @@ class PasswordResetService
|
||||
private const EXPIRY_MINUTES = 15;
|
||||
private const MAX_ATTEMPTS = 5;
|
||||
|
||||
public static function requestReset(string $email, ?string $locale = null): array
|
||||
public function __construct(
|
||||
private readonly UserReadRepository $userReadRepository,
|
||||
private readonly PasswordResetRepository $passwordResetRepository,
|
||||
private readonly UserPasswordService $userPasswordService,
|
||||
private readonly RememberMeService $rememberMeService,
|
||||
private readonly MailService $mailService
|
||||
) {
|
||||
}
|
||||
|
||||
public function requestReset(string $email, ?string $locale = null): array
|
||||
{
|
||||
$email = trim($email);
|
||||
if ($email === '') {
|
||||
return ['ok' => false, 'error' => 'email_required'];
|
||||
}
|
||||
|
||||
$user = UserRepository::findByEmail($email);
|
||||
$user = $this->userReadRepository->findByEmail($email);
|
||||
if (!$user || !isset($user['id'])) {
|
||||
return ['ok' => true];
|
||||
}
|
||||
|
||||
$userId = (int) $user['id'];
|
||||
PasswordResetRepository::invalidateForUser($userId);
|
||||
$this->passwordResetRepository->invalidateForUser($userId);
|
||||
|
||||
$code = self::generateCode();
|
||||
$code = $this->generateCode();
|
||||
$codeHash = password_hash($code, PASSWORD_DEFAULT);
|
||||
$expiresAt = gmdate('Y-m-d H:i:s', time() + (self::EXPIRY_MINUTES * 60));
|
||||
|
||||
$resetId = PasswordResetRepository::create($userId, $codeHash, $expiresAt);
|
||||
$resetId = $this->passwordResetRepository->create($userId, $codeHash, $expiresAt);
|
||||
if (!$resetId) {
|
||||
return ['ok' => false, 'error' => 'create_failed'];
|
||||
}
|
||||
@@ -65,12 +73,12 @@ class PasswordResetService
|
||||
'verify_url' => $verifyUrl,
|
||||
'greeting' => $greeting,
|
||||
];
|
||||
MailService::sendTemplate('reset_code', $vars, $email, $subject, $locale);
|
||||
$this->mailService->sendTemplate('reset_code', $vars, $email, $subject, $locale);
|
||||
|
||||
return ['ok' => true];
|
||||
}
|
||||
|
||||
public static function verifyCode(string $email, string $code): array
|
||||
public function verifyCode(string $email, string $code): array
|
||||
{
|
||||
$email = trim($email);
|
||||
$code = trim($code);
|
||||
@@ -78,12 +86,12 @@ class PasswordResetService
|
||||
return ['ok' => false, 'error' => 'invalid'];
|
||||
}
|
||||
|
||||
$user = UserRepository::findByEmail($email);
|
||||
$user = $this->userReadRepository->findByEmail($email);
|
||||
if (!$user || !isset($user['id'])) {
|
||||
return ['ok' => false, 'error' => 'invalid'];
|
||||
}
|
||||
|
||||
$reset = PasswordResetRepository::findActiveByUserId((int) $user['id']);
|
||||
$reset = $this->passwordResetRepository->findActiveByUserId((int) $user['id']);
|
||||
if (!$reset || !isset($reset['id'])) {
|
||||
return ['ok' => false, 'error' => 'invalid'];
|
||||
}
|
||||
@@ -95,16 +103,16 @@ class PasswordResetService
|
||||
|
||||
$hash = (string) ($reset['code_hash'] ?? '');
|
||||
if ($hash === '' || !password_verify($code, $hash)) {
|
||||
PasswordResetRepository::incrementAttempts((int) $reset['id']);
|
||||
$this->passwordResetRepository->incrementAttempts((int) $reset['id']);
|
||||
return ['ok' => false, 'error' => 'invalid'];
|
||||
}
|
||||
|
||||
return ['ok' => true, 'reset_id' => (int) $reset['id'], 'user_id' => (int) $user['id']];
|
||||
}
|
||||
|
||||
public static function resetPassword(int $resetId, string $password, string $password2): array
|
||||
public function resetPassword(int $resetId, string $password, string $password2): array
|
||||
{
|
||||
$reset = PasswordResetRepository::findById($resetId);
|
||||
$reset = $this->passwordResetRepository->findById($resetId);
|
||||
if (!$reset || !isset($reset['id'])) {
|
||||
return ['ok' => false, 'errors' => [t('Reset request not found')]];
|
||||
}
|
||||
@@ -130,17 +138,17 @@ class PasswordResetService
|
||||
return ['ok' => false, 'errors' => [t('Reset request not found')]];
|
||||
}
|
||||
|
||||
$result = UserService::resetPassword($userId, $password, $password2);
|
||||
$result = $this->userPasswordService->resetPassword($userId, $password, $password2);
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
PasswordResetRepository::markUsed($resetId);
|
||||
RememberMeService::forgetAllForUser($userId);
|
||||
$this->passwordResetRepository->markUsed($resetId);
|
||||
$this->rememberMeService->forgetAllForUser($userId);
|
||||
return ['ok' => true];
|
||||
}
|
||||
|
||||
private static function generateCode(): string
|
||||
private function generateCode(): string
|
||||
{
|
||||
$max = (10 ** self::CODE_LENGTH) - 1;
|
||||
$code = (string) random_int(0, $max);
|
||||
|
||||
Reference in New Issue
Block a user