first commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
|
||||
|
||||
class SysAccessToken extends SanctumPersonalAccessToken
|
||||
{
|
||||
protected $table = 'sys_access_token';
|
||||
|
||||
public function can($ability): bool
|
||||
{
|
||||
if (
|
||||
$this->tokenable_type == SysUserModel::class
|
||||
&& $this->tokenable_id == 1
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return in_array('*', $this->abilities) ||
|
||||
array_key_exists($ability, array_flip($this->abilities));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* 系统用户部门模型
|
||||
*/
|
||||
class SysDeptModel extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
protected $table = 'sys_dept';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'name',
|
||||
'code',
|
||||
'type',
|
||||
'sort',
|
||||
'phone',
|
||||
'email',
|
||||
'status',
|
||||
'address',
|
||||
'remark'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer',
|
||||
];
|
||||
|
||||
protected $hidden = [ 'deleted_at' ];
|
||||
|
||||
/**
|
||||
* 定义与用户的关联关系(一个部门有多个用户)
|
||||
*/
|
||||
public function users(): HasMany
|
||||
{
|
||||
return $this->hasMany(SysUserModel::class, 'dept_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义父级部门关联
|
||||
*/
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SysDeptModel::class, 'parent_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义子部门关联
|
||||
*/
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(SysDeptModel::class, 'parent_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* 系统用户登录日志模型
|
||||
*/
|
||||
class SysLoginRecordModel extends Model
|
||||
{
|
||||
protected $table = 'sys_login_record';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'username',
|
||||
'ipaddr',
|
||||
'login_location',
|
||||
'browser',
|
||||
'os',
|
||||
'status',
|
||||
'msg',
|
||||
'login_time'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'login_time' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义与用户的关联关系
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SysUserModel::class, 'user_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* 系统角色模型
|
||||
*/
|
||||
class SysRoleModel extends Model
|
||||
{
|
||||
protected $table = 'sys_role';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'sort',
|
||||
'rules',
|
||||
'description',
|
||||
'status'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer'
|
||||
];
|
||||
|
||||
protected $appends = ['countUser', 'ruleIds'];
|
||||
|
||||
/**
|
||||
* 角色用户关联
|
||||
*/
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(SysUserModel::class, 'sys_user_role', 'role_id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色权限关联中间表
|
||||
*/
|
||||
public function rules(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(SysRuleModel::class, 'sys_role_rule', 'role_id', 'rule_id');
|
||||
}
|
||||
|
||||
/** 用户总数 */
|
||||
public function getCountUserAttribute(): int
|
||||
{
|
||||
return $this->users()->count();
|
||||
}
|
||||
|
||||
/** 拥有的权限ID */
|
||||
public function getRuleIdsAttribute(): array
|
||||
{
|
||||
if(!empty($this->id) && $this->id == 1) {
|
||||
return SysRuleModel::query()->pluck('id')->toArray();
|
||||
}
|
||||
return $this->rules()->pluck('id')->toArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* 系统规则模型
|
||||
*/
|
||||
class SysRuleModel extends Model
|
||||
{
|
||||
protected $table = 'sys_rule';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'type',
|
||||
'order',
|
||||
'name',
|
||||
'key',
|
||||
'path',
|
||||
'icon',
|
||||
'local',
|
||||
'link',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer',
|
||||
'show' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义子权限关联
|
||||
*/
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(SysRuleModel::class, 'parent_id', 'id')
|
||||
->orderBy('sort');
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义父权限关联
|
||||
*/
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SysRuleModel::class, 'parent_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色权限关联中间表
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(SysRoleModel::class, 'sys_role_rule', 'rule_id', 'role_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemUser\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Ai\Concerns\HasConversations;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Modules\SystemTool\Models\SysFileModel;
|
||||
|
||||
/**
|
||||
* 系统用户模型
|
||||
*/
|
||||
class SysUserModel extends User
|
||||
{
|
||||
use SoftDeletes, HasConversations, HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
protected $table = 'sys_user';
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'username',
|
||||
'password',
|
||||
'nickname',
|
||||
'avatar_id',
|
||||
'bio',
|
||||
'sex',
|
||||
'mobile',
|
||||
'email',
|
||||
'email_verified_at',
|
||||
'dept_id',
|
||||
'login_ip',
|
||||
'login_time',
|
||||
'status'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
'login_time' => 'datetime',
|
||||
'status' => 'integer',
|
||||
'sex' => 'integer',
|
||||
'avatar_id' => 'integer',
|
||||
'dept_id' => 'integer'
|
||||
];
|
||||
|
||||
protected $appends = ['role_id', 'dept_name', 'avatar_url'];
|
||||
|
||||
protected $with = ['dept', 'avatar'];
|
||||
|
||||
protected $hidden = [
|
||||
'dept',
|
||||
'avatar',
|
||||
'password',
|
||||
'remember_token',
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义与部门的归属关系
|
||||
*/
|
||||
public function dept(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SysDeptModel::class, 'dept_id', 'id');
|
||||
}
|
||||
|
||||
/** 部门名称 */
|
||||
public function getDeptNameAttribute(): string
|
||||
{
|
||||
return $this->dept->name ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义与角色的关联
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(SysRoleModel::class, 'sys_user_role', 'user_id', 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户角色列表
|
||||
*/
|
||||
public function getRoleIdAttribute()
|
||||
{
|
||||
return $this->roles()
|
||||
->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义与登录日志的关联
|
||||
*/
|
||||
public function loginRecords(): HasMany
|
||||
{
|
||||
return $this->hasMany(SysLoginRecordModel::class, 'user_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联用户头像
|
||||
* @return HasOne
|
||||
*/
|
||||
public function avatar(): HasOne
|
||||
{
|
||||
return $this->hasOne(SysFileModel::class, 'id', 'avatar_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户角色列表
|
||||
*/
|
||||
public function getAvatarUrlAttribute()
|
||||
{
|
||||
if($this->avatar) {
|
||||
return $this->avatar->preview_url;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户所有权限
|
||||
* @return array
|
||||
*/
|
||||
public function access(): array
|
||||
{
|
||||
if($this->id == 1) {
|
||||
return SysRuleModel::query()
|
||||
->where('status', 1)
|
||||
->pluck('key')
|
||||
->toArray();
|
||||
}
|
||||
$roles = SysUserModel::with(['roles.rules' => function ($query) {
|
||||
$query->where('status', 1);
|
||||
}])->find($this->id)->roles->toArray();
|
||||
|
||||
return collect($roles)
|
||||
->map(fn ($item) => $item['rules'] )
|
||||
->collapse()
|
||||
->map(fn ($item) => $item['key'] )
|
||||
->unique()
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user