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); + } }