diff --git a/app/Domains/Auth/Models/ResetPasswordAttempt.php b/app/Domains/Auth/Models/ResetPasswordAttempt.php index 39dc274..03b27e4 100644 --- a/app/Domains/Auth/Models/ResetPasswordAttempt.php +++ b/app/Domains/Auth/Models/ResetPasswordAttempt.php @@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Attributes\Hidden; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -#[Fillable(['user_id', 'codigo', 'status'])] +#[Fillable(['user_id', 'codigo', 'reason', 'status'])] #[Hidden(['codigo'])] class ResetPasswordAttempt extends Model { diff --git a/app/Domains/Auth/Services/PasswordLoginService.php b/app/Domains/Auth/Services/PasswordLoginService.php index 3cc42a1..79d637b 100644 --- a/app/Domains/Auth/Services/PasswordLoginService.php +++ b/app/Domains/Auth/Services/PasswordLoginService.php @@ -8,10 +8,15 @@ use App\Domains\Auth\Models\User; use Carbon\CarbonImmutable; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Log; use Illuminate\Validation\ValidationException; class PasswordLoginService { + public function __construct( + private readonly ResetPasswordAttemptService $resetPasswordAttemptService, + ) {} + /** * @throws AccountLockedException * @throws ValidationException @@ -67,7 +72,7 @@ class PasswordLoginService if ($user === null || ! Hash::check($password, $user->password)) { if ($user !== null) { - $this->registerFailure($user, $now); + $this->registerFailure($user, $now, $tenantCode); } $outcome = $user?->locked_until?->isFuture() @@ -126,7 +131,7 @@ class PasswordLoginService return $result['user']; } - private function registerFailure(User $user, CarbonImmutable $now): void + private function registerFailure(User $user, CarbonImmutable $now, string $tenantCode): void { $windowMinutes = max(1, (int) config('login-security.attempt_window_minutes')); $maxAttempts = max(1, (int) config('login-security.max_attempts')); @@ -138,6 +143,8 @@ class PasswordLoginService ? $user->failed_login_attempts + 1 : 1; + $previousAttempts = $user->failed_login_attempts; + $user->forceFill([ 'failed_login_attempts' => $attempts, 'last_failed_login_at' => $now, @@ -145,6 +152,17 @@ class PasswordLoginService ? $now->addMinutes($lockMinutes) : null, ])->save(); + + if ($attempts >= $maxAttempts && $previousAttempts < $maxAttempts) { + try { + $this->resetPasswordAttemptService->createForEmail($user->email, $tenantCode, 'account_locked'); + } catch (\Throwable $e) { + Log::error('Failed to trigger reset password on account lock', [ + 'user_id' => $user->id, + 'exception' => $e + ]); + } + } } private function recordAttempt( diff --git a/app/Domains/Auth/Services/ResetPasswordAttemptService.php b/app/Domains/Auth/Services/ResetPasswordAttemptService.php index 201c1b4..9721277 100644 --- a/app/Domains/Auth/Services/ResetPasswordAttemptService.php +++ b/app/Domains/Auth/Services/ResetPasswordAttemptService.php @@ -11,12 +11,12 @@ use Throwable; class ResetPasswordAttemptService { - public function createForEmail(string $email, string $tenantCode): void + public function createForEmail(string $email, string $tenantCode, string $reason = 'manual'): void { $emailFingerprint = $this->emailFingerprint($email); try { - $attemptId = DB::transaction(function () use ($email, $emailFingerprint): ?int { + $attemptId = DB::transaction(function () use ($email, $emailFingerprint, $reason): ?int { $user = User::query() ->where('email', $email) ->lockForUpdate() @@ -39,6 +39,7 @@ class ResetPasswordAttemptService $attempt = $user->resetPasswordAttempts()->create([ 'codigo' => $this->generateCode(), + 'reason' => $reason, 'status' => ResetPasswordAttempt::STATUS_PENDING, ]); diff --git a/database/migrations/2026_08_05_125427_add_reason_to_reset_password_attempts_table.php b/database/migrations/2026_08_05_125427_add_reason_to_reset_password_attempts_table.php new file mode 100644 index 0000000..b770ad8 --- /dev/null +++ b/database/migrations/2026_08_05_125427_add_reason_to_reset_password_attempts_table.php @@ -0,0 +1,28 @@ +string('reason')->default('manual')->after('codigo'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('reset_password_attempts', function (Blueprint $table): void { + $table->dropColumn('reason'); + }); + } +}; diff --git a/resources/views/mail/notifications/password-reset.blade.php b/resources/views/mail/notifications/password-reset.blade.php index 422b214..0b9a955 100644 --- a/resources/views/mail/notifications/password-reset.blade.php +++ b/resources/views/mail/notifications/password-reset.blade.php @@ -2,10 +2,16 @@ Recuperá tu contraseña +@if($attempt->reason === 'account_locked') +

+ Hola {{ $attempt->user->nombre_apellido }}, registramos varios intentos fallidos de inicio de sesión en tu cuenta. Por seguridad, hemos bloqueado el acceso temporalmente. Puedes utilizar este código para cambiar tu contraseña y desbloquearla inmediatamente. +

+@else

Hola {{ $attempt->user->nombre_apellido }}, recibimos una solicitud para restablecer la contraseña de tu cuenta.

+@endif

Ingresá este código en {{ $tenant->nombre }}:

@@ -15,6 +21,21 @@ +@php + $recoveryUrl = 'https://' . $tenant->dominio . '/recuperar-contrasena/codigo?email=' . urlencode($attempt->user->email); +@endphp + +
+ + Ingresar código ahora + +
+

+@if($attempt->reason === 'account_locked') + Si no fuiste vos, por favor desestimá y borrá este correo. Tu cuenta seguirá protegida. +@else Si no solicitaste recuperar tu contraseña, podés ignorar este mensaje. +@endif