refactor(auth): simplify password reset attempt handling logic and improve code readability
This commit is contained in:
@@ -236,8 +236,7 @@ class PasswordLoginService
|
|||||||
CarbonImmutable $now,
|
CarbonImmutable $now,
|
||||||
string $tenantCode,
|
string $tenantCode,
|
||||||
string $passwordResetChannel,
|
string $passwordResetChannel,
|
||||||
): void
|
): void {
|
||||||
{
|
|
||||||
$windowMinutes = max(1, (int) config('login-security.attempt_window_minutes'));
|
$windowMinutes = max(1, (int) config('login-security.attempt_window_minutes'));
|
||||||
$maxAttempts = max(1, (int) config('login-security.max_attempts'));
|
$maxAttempts = max(1, (int) config('login-security.max_attempts'));
|
||||||
$lockMinutes = max(1, (int) config('login-security.lock_minutes'));
|
$lockMinutes = max(1, (int) config('login-security.lock_minutes'));
|
||||||
@@ -260,23 +259,23 @@ class PasswordLoginService
|
|||||||
|
|
||||||
if ($attempts >= $maxAttempts && $previousAttempts < $maxAttempts) {
|
if ($attempts >= $maxAttempts && $previousAttempts < $maxAttempts) {
|
||||||
try {
|
try {
|
||||||
match ($passwordResetChannel) {
|
if ($passwordResetChannel === PasswordResetRequested::CHANNEL_ADMINAPP) {
|
||||||
PasswordResetRequested::CHANNEL_ADMINAPP =>
|
$this->resetPasswordAttemptService->createForAdminAppEmail(
|
||||||
$this->resetPasswordAttemptService->createForAdminAppEmail(
|
$user->email,
|
||||||
$user->email,
|
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
||||||
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
);
|
||||||
),
|
} elseif ($passwordResetChannel === PasswordResetRequested::CHANNEL_SCANNER) {
|
||||||
PasswordResetRequested::CHANNEL_SCANNER =>
|
$this->resetPasswordAttemptService->createForScannerEmail(
|
||||||
$this->resetPasswordAttemptService->createForScannerEmail(
|
$user->email,
|
||||||
$user->email,
|
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
||||||
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
);
|
||||||
),
|
} else {
|
||||||
default => $this->resetPasswordAttemptService->createForEmail(
|
$this->resetPasswordAttemptService->createForEmail(
|
||||||
$user->email,
|
$user->email,
|
||||||
$tenantCode,
|
$tenantCode,
|
||||||
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
||||||
),
|
);
|
||||||
};
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Log::error('Failed to trigger reset password on account lock', [
|
Log::error('Failed to trigger reset password on account lock', [
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ class ResetPasswordAttemptService
|
|||||||
string $email,
|
string $email,
|
||||||
string $tenantCode,
|
string $tenantCode,
|
||||||
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
||||||
): void
|
): void {
|
||||||
{
|
|
||||||
$emailFingerprint = $this->emailFingerprint($email);
|
$emailFingerprint = $this->emailFingerprint($email);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -54,8 +53,7 @@ class ResetPasswordAttemptService
|
|||||||
public function createForAdminAppEmail(
|
public function createForAdminAppEmail(
|
||||||
string $email,
|
string $email,
|
||||||
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
||||||
): void
|
): void {
|
||||||
{
|
|
||||||
$emailFingerprint = $this->emailFingerprint($email);
|
$emailFingerprint = $this->emailFingerprint($email);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -103,8 +101,7 @@ class ResetPasswordAttemptService
|
|||||||
public function createForScannerEmail(
|
public function createForScannerEmail(
|
||||||
string $email,
|
string $email,
|
||||||
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
string $reason = ResetPasswordAttempt::REASON_MANUAL,
|
||||||
): void
|
): void {
|
||||||
{
|
|
||||||
$emailFingerprint = $this->emailFingerprint($email);
|
$emailFingerprint = $this->emailFingerprint($email);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ namespace App\Domains\Notification\Services;
|
|||||||
|
|
||||||
use App\Domains\Auth\Models\ResetPasswordAttempt;
|
use App\Domains\Auth\Models\ResetPasswordAttempt;
|
||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Notification\Events\PasswordResetRequested;
|
|
||||||
use App\Domains\Integration\Services\MailService;
|
use App\Domains\Integration\Services\MailService;
|
||||||
|
use App\Domains\Notification\Events\PasswordResetRequested;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Ticket\Models\Ticket;
|
use App\Domains\Ticket\Models\Ticket;
|
||||||
@@ -36,8 +36,7 @@ class NotificationMailService
|
|||||||
int $attemptId,
|
int $attemptId,
|
||||||
string $tenantCode,
|
string $tenantCode,
|
||||||
string $channel = PasswordResetRequested::CHANNEL_STOREFRONT,
|
string $channel = PasswordResetRequested::CHANNEL_STOREFRONT,
|
||||||
): void
|
): void {
|
||||||
{
|
|
||||||
$tenant = Tenant::query()
|
$tenant = Tenant::query()
|
||||||
->with('websiteType')
|
->with('websiteType')
|
||||||
->where('codigo', $tenantCode)
|
->where('codigo', $tenantCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user