M1: 数据模型 + sablog:import 脚本(MySQL 8 验证通过,不迁移 trackback 等过时功能)

This commit is contained in:
ak
2026-08-11 17:40:13 +08:00
parent 8d526a8cfd
commit f16bb75538
17 changed files with 1010 additions and 13 deletions
+50 -2
View File
@@ -2,16 +2,18 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
use HasFactory, Notifiable, HasRoles;
/**
* The attributes that are mass assignable.
@@ -22,6 +24,13 @@ class User extends Authenticatable
'name',
'email',
'password',
'legacy_md5',
'url',
'logincount',
'loginip',
'regip',
'logintime',
'lastpost_at',
];
/**
@@ -32,6 +41,7 @@ class User extends Authenticatable
protected $hidden = [
'password',
'remember_token',
'legacy_md5',
];
/**
@@ -44,6 +54,44 @@ class User extends Authenticatable
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'logintime' => 'datetime',
'lastpost_at' => 'datetime',
];
}
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
/**
* 验证登录:优先新密码哈希,兼容 sablog MD5 密码。
*/
public function verifyPassword(string $plain): bool
{
if ($this->password && Hash::check($plain, $this->password)) {
return true;
}
if ($this->legacy_md5 && md5($plain) === strtolower($this->legacy_md5)) {
// 登录成功后自动升级为现代哈希
$this->password = Hash::make($plain);
$this->legacy_md5 = null;
$this->save();
return true;
}
return false;
}
public function isAdmin(): bool
{
return $this->hasRole('admin');
}
}