stratio84/laravel-password-history

一个用于记录用户所有密码更改历史的包

0.0.2 2023-02-09 11:48 UTC

This package is not auto-updated.

Last update: 2024-09-20 17:30:07 UTC


README

为了防止用户重复使用相同的密码,可以像谷歌那样记录用户的密码历史以提高安全性。

特别感谢imanghafoori,这个库是从他的库分叉的。所有的荣誉都归他所有!

安装

composer require stratio84/laravel-password-history

要发布配置文件并迁移数据库

php artisan vendor:publish
php artisan migrate

访问 config/password_history.php 文件以查看所有可能配置。

用法

该包将监听模型的 saved 事件(这些模型在配置文件中列出)并自动记录密码散列。

<?php
// When inserting, it will also log the password hash in the "password_histories" table
 User::create($data);

// Sample for changing the password
$user = User::find($id);
$passHash = Hash::make(request('new_password'));

$user->password = $passHash;
$user->save(); // after saving the model, the password change  will be recorded, automatically

我们建议使用 saveOrFail 在事务中进行所有查询

$user->saveOrFail();

请注意,如下修改模型不会触发任何模型事件,因此密码更改将不会在幕后记录。

<?php
// Here we do NOT get the model from db and only send  an update query
// So laravel does NOT fire model events
User::where('id', $id)->update($data);

验证规则

还有一个验证规则,用于在 Laravel 验证规则中检查整个密码历史与新密码。

<?php
use Imanghafoori\PasswordHistory\Rules\NotBeInPasswordHistory;
//...

$rules = [
    // ... 
    'password' => [
       'required',
       'confirmed',
       NotBeInPasswordHistory::ofUser($this->user),
    ]
    // ... 
];

$this->validate(...);

再次提醒,您可以快速查看源代码以了解其中的情况。

质量保证

  • 我有一个 users 表和一个 admins 表(用户模型和管理员模型),我也可以跟踪管理员密码更改吗?
Yeah, the package supports it, visit the config file.

🙋 贡献

如果您发现任何问题或有一个更好的方法来做某事,请随时提出问题或拉取请求。如果您在开源项目中使用 laravel-widgetize,请创建一个拉取请求,以提供其 URL 作为 README.md 文件中的示例应用程序。

❗ 安全性

如果您发现任何安全相关问题,请使用 security tab 而不是使用问题跟踪器。

⭐ 您的星标让我们做得更多 ⭐

一如既往,如果您发现这个包很有用,并且希望鼓励我们维护和改进它,只需按星标按钮即可声明您的意愿。