From 14f2139e922a5ab7708ba482f53c3d6a3cb847ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Mon, 22 Nov 2021 13:31:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/Models/User.php b/app/Models/User.php index d2c2cabe..6579e755 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\Access\Authorizable; +use Illuminate\Support\Facades\Hash; class User extends Model implements AuthorizableContract, AuthenticatableContract { @@ -52,4 +53,32 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac 'last_login_at' => 'datetime', 'status' => 'int', ]; + + /** + * 设置此用户的密码 + * + * @param string $value + * @return void + */ + public function setPasswordAttribute($value): void + { + if ((string) $value === '') { + $value = null; + } elseif (Hash::needsRehash($value)) { + $value = Hash::make($value); + } + + $this->attributes['password'] = $value; + } + + /** + * 确认给定的密码是否正确 + * + * @param string $password + * @return bool + */ + public function verifyPassword(string $password): bool + { + return $this->password && Hash::check($password, $this->password); + } }