Files
laravel-gost-template/app/Services/Auth/PasswordManager.php
2026-06-24 17:20:43 +09:00

54 lines
1.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\Auth;
use App\Models\User;
use App\Support\PasswordPolicy;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
/**
* Управление сменой пароля с ведением истории (меры ИАФ.3).
*/
class PasswordManager
{
/**
* Установить новый пароль: сохранить старый в историю, обновить метку
* времени смены и подрезать историю до лимита.
*/
public function change(User $user, string $newPassword): void
{
DB::transaction(function () use ($user, $newPassword): void {
// Сохраняем текущий пароль в историю до перезаписи.
if ($user->password) {
$user->passwordHistories()->create([
'password_hash' => $user->password,
'created_at' => now(),
]);
}
$user->forceFill([
'password' => Hash::make($newPassword),
'password_changed_at' => now(),
])->save();
$this->trimHistory($user);
});
}
private function trimHistory(User $user): void
{
$limit = PasswordPolicy::historyLimit();
$ids = $user->passwordHistories()
->orderByDesc('created_at')
->skip($limit)
->take(PHP_INT_MAX)
->pluck('id');
if ($ids->isNotEmpty()) {
$user->passwordHistories()->whereIn('id', $ids)->delete();
}
}
}