binarycabin/laravel-password

创建/更新时更新用户密码的基本工具

1.0.1 2017-11-17 15:01 UTC

This package is auto-updated.

Last update: 2024-09-06 10:20:13 UTC


README

创建/更新时更新用户密码的基本工具

此包向模型添加一个简单的特质,以自动保存创建此模型的用户ID。

只需将 "\BinaryCabin\LaravelPassword\Traits\HasPassword;" 特质添加到您的模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    use \BinaryCabin\LaravelAuthor\Traits\HasAuthorUser;

}

现在当您创建新用户时

  • 如果没有在 ::create 属性中添加,将生成一个临时密码
  • 传入属性的密码将自动进行散列

并且当调用 $user->update

  • 传入属性的密码将自动进行散列

注意:请小心移除应用程序中存在的任何现有散列,因为这会导致密码被散列两次。例如,Laravel的默认注册控制器包含

return User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
]);

由于 User::create 方法现在将散列包含的密码,您可以将其更新为

return User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => $data['password'],
]);

此外,默认的 ResetPasswordController 也会散列用户的密码。将此添加到控制器中以覆盖此功能

protected function resetPassword($user, $password){
    $user->password = $password;
    $user->setRememberToken(\Illuminate\Support\Str::random(60));
    $user->save();
    event(new \Illuminate\Auth\Events\PasswordReset($user));
    $this->guard()->login($user);
}