242 lines
5.7 KiB
PHP
242 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Models\BaseModel;
|
||
use App\Models\System\SystemRole;
|
||
use App\Models\System\SystemUserRole;
|
||
use App\Models\System\SystemUserSchoolCampus;
|
||
use App\Models\Teachers\TeacherClass;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||
use Illuminate\Notifications\Notifiable;
|
||
use Laravel\Sanctum\HasApiTokens;
|
||
|
||
/**
|
||
* 用户信息模型
|
||
* @package App\Models
|
||
* @property int $id 用户ID
|
||
* @property string $username 用户账号
|
||
* @property string $password 密码
|
||
* @property string $nickname 用户昵称
|
||
* @property string $remark 备注
|
||
* @property int $dept_id 部门ID
|
||
* @property string $post_ids 岗位编号数组
|
||
* @property string $email 用户邮箱
|
||
* @property string $mobile 手机号码
|
||
* @property int $sex 用户性别
|
||
* @property string $avatar 头像地址
|
||
* @property int $status 帐号状态(0正常 1停用)
|
||
* @property string $login_ip 最后登录IP
|
||
* @property string $login_date 最后登录时间
|
||
* @property string $creator 创建者
|
||
* @property string $create_time 创建时间
|
||
* @property string $updater 更新者
|
||
* @property string $update_time 更新时间
|
||
* @property string $deleted 是否删除
|
||
* @property string $tenant_id 租户编号
|
||
*/
|
||
class User extends Authenticatable
|
||
{
|
||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||
use HasFactory, Notifiable, HasApiTokens;
|
||
|
||
protected $table = "system_users";
|
||
|
||
/**
|
||
* 可批量赋值的属性
|
||
*/
|
||
protected $fillable = [
|
||
'username',
|
||
'password',
|
||
'nickname',
|
||
'remark',
|
||
'dept_id',
|
||
'post_ids',
|
||
'email',
|
||
'mobile',
|
||
'sex',
|
||
'avatar',
|
||
'status',
|
||
'login_ip',
|
||
'login_date',
|
||
'creator',
|
||
'create_time',
|
||
'updater',
|
||
'update_time',
|
||
'deleted',
|
||
'tenant_id',
|
||
];
|
||
|
||
/**
|
||
* 隐藏属性
|
||
*/
|
||
protected $hidden = [
|
||
'password',
|
||
];
|
||
|
||
/**
|
||
* 自定义时间戳字段名
|
||
*/
|
||
const CREATED_AT = 'create_time';
|
||
const UPDATED_AT = 'update_time';
|
||
|
||
/**
|
||
* 类型转换
|
||
*/
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'login_date' => 'datetime',
|
||
'create_time' => 'datetime',
|
||
'update_time' => 'datetime',
|
||
'password' => 'hashed',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 用户角色关联
|
||
*/
|
||
public function userRoles()
|
||
{
|
||
return $this->hasMany(SystemUserRole::class, 'user_id');
|
||
}
|
||
|
||
/**
|
||
* 用户的角色(多对多)
|
||
*/
|
||
public function roles()
|
||
{
|
||
return $this->belongsToMany(SystemRole::class, 'system_user_role', 'user_id', 'role_id');
|
||
}
|
||
|
||
/**
|
||
* 获取用户的角色ID列表
|
||
*/
|
||
public function getRoleIds(): array
|
||
{
|
||
return $this->userRoles()->pluck('role_id')->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取用户的角色编码列表
|
||
*/
|
||
public function getRoleCodes(): array
|
||
{
|
||
return $this->roles()->pluck('code')->toArray();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否拥有指定角色
|
||
*/
|
||
public function hasRole(int $roleId): bool
|
||
{
|
||
return $this->userRoles()->where('role_id', $roleId)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否拥有指定角色编码
|
||
*/
|
||
public function hasRoleCode(string $roleCode): bool
|
||
{
|
||
return $this->roles()->where('code', $roleCode)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否拥有任一角色
|
||
*/
|
||
public function hasAnyRole(array $roleIds): bool
|
||
{
|
||
return $this->userRoles()->whereIn('role_id', $roleIds)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否拥有任一角色编码
|
||
*/
|
||
public function hasAnyRoleCode(array $roleCodes): bool
|
||
{
|
||
return $this->roles()->whereIn('code', $roleCodes)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否拥有所有角色
|
||
*/
|
||
public function hasAllRoles(array $roleIds): bool
|
||
{
|
||
$userRoleIds = $this->getRoleIds();
|
||
return empty(array_diff($roleIds, $userRoleIds));
|
||
}
|
||
|
||
/**
|
||
* 获取用户权限范围内的租户ID
|
||
*/
|
||
public function getCurrentTenantId(): ?int
|
||
{
|
||
return $this->tenant_id;
|
||
}
|
||
|
||
/**
|
||
* 用户的学校校区关联
|
||
*/
|
||
public function schoolCampuses()
|
||
{
|
||
return $this->hasMany(SystemUserSchoolCampus::class, 'userid');
|
||
}
|
||
|
||
/**
|
||
* 用户的班级关联(作为老师)
|
||
*/
|
||
public function teacherClasses()
|
||
{
|
||
return $this->hasMany(TeacherClass::class, 'teacher_id');
|
||
}
|
||
|
||
/**
|
||
* 获取用户管理的学校ID列表
|
||
*/
|
||
public function getSchoolIds(): array
|
||
{
|
||
return $this->schoolCampuses()->pluck('schoolid')->unique()->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取用户管理的校区ID列表
|
||
*/
|
||
public function getCampusIds(): array
|
||
{
|
||
return $this->schoolCampuses()->pluck('campusid')->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取用户管理的班级ID列表
|
||
*/
|
||
public function getClassIds(): array
|
||
{
|
||
return $this->teacherClasses()->pluck('class_id')->toArray();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否管理指定学校
|
||
*/
|
||
public function managesSchool(int $schoolId): bool
|
||
{
|
||
return $this->schoolCampuses()->where('schoolid', $schoolId)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否管理指定校区
|
||
*/
|
||
public function managesCampus(int $campusId): bool
|
||
{
|
||
return $this->schoolCampuses()->where('campusid', $campusId)->exists();
|
||
}
|
||
|
||
/**
|
||
* 检查用户是否管理指定班级
|
||
*/
|
||
public function managesClass(int $classId): bool
|
||
{
|
||
return $this->teacherClasses()->where('class_id', $classId)->exists();
|
||
}
|
||
}
|