BastionSSO/app/Models/User.php
Boen_Shi 777c682a4e feat(用户安全): 支持要求更改密码并强制登录后改密
- 新增 users.force_password_change 字段与迁移

- 用户新增/编辑/批量导入支持要求更改密码

- 登录后未改密用户仅允许访问改密相关接口
2026-04-30 14:41:11 +08:00

70 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use HasFactory;
use HasRoles;
use Notifiable;
protected string $guard_name = 'api';
protected $fillable = [
'nickname',
'email',
'phone',
'password',
'force_password_change',
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier(): mixed
{
return $this->getKey();
}
public function getJWTCustomClaims(): array
{
return [];
}
public function serverResources(): BelongsToMany
{
return $this->belongsToMany(ServerResource::class, 'user_server_permissions')
->withPivot(['can_ssh', 'can_sftp', 'can_rdp'])
->withTimestamps();
}
public function opsSoftwarePreferences(): HasMany
{
return $this->hasMany(UserOpsSoftwarePreference::class);
}
public function isAdmin(): bool
{
return $this->hasRole('admin', 'api');
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'force_password_change' => 'boolean',
];
}
}