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 46dac23..462d9f7 100644 --- a/app/Domains/Auth/Services/PasswordLoginService.php +++ b/app/Domains/Auth/Services/PasswordLoginService.php @@ -9,10 +9,15 @@ use App\Domains\Authorization\Enums\RoleCode; 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 @@ -118,7 +123,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() @@ -177,7 +182,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')); @@ -189,6 +194,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, @@ -196,6 +203,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/app/Domains/Catalog/Models/Variant.php b/app/Domains/Catalog/Models/Variant.php index 3fb716c..fe3b13e 100644 --- a/app/Domains/Catalog/Models/Variant.php +++ b/app/Domains/Catalog/Models/Variant.php @@ -95,25 +95,7 @@ class Variant extends Model public function getName(): string { - $name = $this->catalogItem->nombre; - $this->loadMissing(['definitions.itemAttribute.attribute', 'eventDate']); - $definitions = $this->definitions - ->map(function (VariantDefinition $definition): ?string { - $attributeName = $definition->itemAttribute?->attribute?->nombre; - - return $attributeName - ? "{$attributeName}: {$definition->value}" - : $definition->value; - }) - ->filter(); - - if ($this->eventDate !== null) { - $definitions->push('Fecha: '.$this->eventDate->date->format('Y-m-d')); - } - - $description = $definitions->implode(', '); - - return $description === '' ? $name : "{$name} ({$description})"; + return $this->catalogItem->nombre; } public function getMinimumUseDate(): ?CarbonInterface 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. +
+@elseHola {{ $attempt->user->nombre_apellido }}, recibimos una solicitud para restablecer la contraseña de tu cuenta.
+@endifIngresá este código en {{ $tenant->nombre }}:
@@ -15,6 +21,21 @@ +@php + $recoveryUrl = 'https://' . $tenant->dominio . '/recuperar-contrasena/codigo?email=' . urlencode($attempt->user->email); +@endphp + + ++@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
diff --git a/tests/Unit/Catalog/CatalogModelsTest.php b/tests/Unit/Catalog/CatalogModelsTest.php index 1f4abd3..95f8cc7 100644 --- a/tests/Unit/Catalog/CatalogModelsTest.php +++ b/tests/Unit/Catalog/CatalogModelsTest.php @@ -195,7 +195,7 @@ class CatalogModelsTest extends TestCase $variant->setRelation('eventDate', $eventDate); $variant->setRelation('definitions', new EloquentCollection); - $this->assertSame('Entrada General (Fecha: 2026-10-09)', $variant->getName()); + $this->assertSame('Entrada General', $variant->getName()); $this->assertSame('2026-10-09 09:00:00', $variant->getMinimumUseDate()->format('Y-m-d H:i:s')); $this->assertSame('2026-10-09 18:00:00', $variant->getMaximumUseDate()->format('Y-m-d H:i:s')); }