aaronsaray/laravel-legacy-passwords

该包已被放弃,不再维护。没有建议的替代包。

工具集,用于将旧密码迁移到您的当前Laravel项目

3.0.1 2019-09-01 00:38 UTC

This package is auto-updated.

Last update: 2020-11-05 22:36:55 UTC


README

ARCHIVED: This project is archived and no longer maintained.

这是一个工具,可以将您的应用程序中的旧密码迁移到标准Laravel安装中。当您将项目重写为Laravel时,如果您不希望用户重置密码,这尤其有用。

安装

此项目需要Laravel 5.8及以上。

composer require package-for-laravel/legacy-passwords

然后,运行您的迁移(此包注册了一些迁移)

artisan migrate

接下来,找到您的用户模型(此包配置为使用默认的users表设置 - 但您的用户模型的位置或名称无关紧要)并使用PackageForLaravel\LegacyPasswords\HasLegacyPassword特性。还需要实现PackageForLaravel\LegacyPasswords\HasLegacyPasswordContract

use PackageForLaravel\LegacyPasswords\HasLegacyPasswordContract;
use PackageForLaravel\LegacyPasswords\HasLegacyPassword;

class User extends Authenticatable implements HasLegacyPasswordContract
{
  use HasLegacyPassword;

创建一个旧密码认证策略,实现PackageForLaravel\LegacyPasswords\LegacyPasswordAuthenticationStrategyContract

以下是一个示例;假设我们的旧系统是明文的md5。

use PackageForLaravel\LegacyPasswords\LegacyPasswordAuthenticationStrategyContract;

class MyLegacyPasswordAuthenticationStrategy implements LegacyPasswordAuthenticationStrategyContract
{
    public function validateCredentials(Authenticatable $user, array $credentials): bool
    {
        $password = $credentials['password'];
        $hashed = md5($password);

        return $user->legacyPassword->data['md5'] === $hashed;
    }
}

然后,将其绑定到Laravel中。例如,您可以在AuthServiceProvider中这样做

$this->app->bind(LegacyPasswordAuthenticationStrategyContract::class, function() {
    return new MyLegacyPasswordAuthenticationStrategy();
});

记住,如果需要,您可以在策略中注入需求。

最后,修改您的config/auth.php中的providers.users.driver键,将其更改为laravel-legacy-passwords,以便我们可以注入此认证系统而不是标准系统。

如何创建旧密码?很简单。如下所示

$user = User::create(); // you created this with your legacy data
$user->legacyPassword()->create([
    'data' => [
        'md5' => $oldUser['md5']
    ]
]);

您可以在data键中包含您策略所需的任何内容。

致谢

此包由Aaron Saray创建和维护