ddtech/laravel-password-policy

将密码策略与Eloquent模型关联

dev-main 2021-10-20 20:20 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:12 UTC


README

此包确保不输入最后使用过的(n)个密码。

需求

Laravel Fortify或Jetstream必须安装。

安装

composer require ddtech/laravel-password-policy
php artisan vendor:publish --tag=migrations
php artisan migrate

用法

将PasswordRecords Traits添加到User模型

use PasswordRecords;

需要更改Fortify, ResetUserPassword的reset函数

Validator::make($input, [
  'password' => $this->passwordRules(),
])->validate();
Validator::make($input, [
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    $user->oldPasswordRules($validator, $user, $input, 3);
})->validate();

$user->triggerPasswordRecord($user->password);

需要更改Fortify, UpdateUserPassword的update函数

Validator::make($input, [
    'current_password' => ['required', 'string'],
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    if (! isset($input['current_password']) || ! Hash::check($input['current_password'], $user->password)) {
        $validator->errors()->add('current_password', __('The provided password does not match your current password.'));
    }
})->validateWithBag('updatePassword');
Validator::make($input, [
    'current_password' => ['required', 'string'],
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    if (! isset($input['current_password']) || ! Hash::check($input['current_password'], $user->password)) {
        $validator->errors()->add('current_password', __('The provided password does not match your current password.'));
    }
    $user->oldPasswordRules($validator, $user, $input, 3);
})->validateWithBag('updatePassword');

$user->triggerPasswordRecord($user->password);