mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 17:07:49 +08:00
first commit
This commit is contained in:
113
app/Model/AdminMenu.php
Normal file
113
app/Model/AdminMenu.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
|
||||
use App\Model\Meta\AdminUserMeta;
|
||||
use App\Model\Meta\MetaCast;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Database\Model\Events\Deleting;
|
||||
use Hyperf\Database\Model\Relations\BelongsToMany;
|
||||
use Hyperf\Database\Model\Relations\HasMany;
|
||||
use Hyperf\Database\Model\Collection;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $parent_id
|
||||
* @property string $name
|
||||
* @property AdminUserMeta $meta
|
||||
* @property string $path
|
||||
* @property string $component
|
||||
* @property string $redirect
|
||||
* @property int $status
|
||||
* @property int $sort
|
||||
* @property int $created_by
|
||||
* @property int $updated_by
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property string $remark
|
||||
* @property Collection|AdminRole[] $roles
|
||||
* @property Collection|AdminMenu[] $children
|
||||
*/
|
||||
class AdminMenu extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_menu';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'id',
|
||||
'parent_id',
|
||||
'name',
|
||||
'component',
|
||||
'redirect',
|
||||
'status',
|
||||
'sort',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'remark',
|
||||
'meta',
|
||||
'path',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'status' => AdminMenuStatusCode::class,
|
||||
'sort' => 'integer',
|
||||
'created_by' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'meta' => MetaCast::class,
|
||||
'path' => 'string',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 通过中间表获取角色.
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
AdminRole::class,
|
||||
'admin_role_belongs_menu',
|
||||
'menu_id',
|
||||
'role_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany
|
||||
*/
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this
|
||||
->hasMany(self::class, 'parent_id', 'id')
|
||||
->where('status', AdminMenuStatusCode::Normal)
|
||||
->orderBy('sort')
|
||||
->with('children');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Deleting $event
|
||||
* @return void
|
||||
*/
|
||||
public function deleting(Deleting $event): void
|
||||
{
|
||||
$this->roles()->detach();
|
||||
}
|
||||
}
|
||||
96
app/Model/AdminRole.php
Normal file
96
app/Model/AdminRole.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
use Hyperf\Database\Model\Events\Deleting;
|
||||
use Hyperf\Database\Model\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $code
|
||||
* @property int $status
|
||||
* @property int $sort
|
||||
* @property int $created_by
|
||||
* @property int $updated_by
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property string $remark
|
||||
*/
|
||||
class AdminRole extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_role';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'id',
|
||||
'name',
|
||||
'code',
|
||||
'status',
|
||||
'sort',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'remark'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'status' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'created_by' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @return BelongsToMany
|
||||
*/
|
||||
public function adminMenus(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
AdminMenu::class,
|
||||
'role_belongs_menu',
|
||||
'role_id',
|
||||
'menu_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany
|
||||
*/
|
||||
public function adminUsers(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
AdminUser::class,
|
||||
'admin_user_belongs_role',
|
||||
'role_id',
|
||||
'user_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Deleting $event
|
||||
* @return void
|
||||
*/
|
||||
public function deleting(Deleting $event): void
|
||||
{
|
||||
$this->adminUsers()->detach();
|
||||
$this->adminMenus()->detach();
|
||||
}
|
||||
}
|
||||
32
app/Model/AdminRoleBelongsMenu.php
Normal file
32
app/Model/AdminRoleBelongsMenu.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $admin_role_id
|
||||
* @property int $admin_menu_id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*/
|
||||
class AdminRoleBelongsMenu extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_role_belongs_menu';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'admin_role_id' => 'integer', 'admin_menu_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
|
||||
}
|
||||
189
app/Model/AdminUser.php
Normal file
189
app/Model/AdminUser.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
use App\Constants\Model\AdminUser\AdminUserStatusCode;
|
||||
use App\Constants\Model\AdminUser\AdminUserTypeCode;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Collection\Enumerable;
|
||||
use Hyperf\Database\Model\Collection;
|
||||
use Hyperf\Database\Model\Events\Creating;
|
||||
use Hyperf\Database\Model\Events\Deleted;
|
||||
use Hyperf\Database\Model\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $username
|
||||
* @property string $password
|
||||
* @property string $user_type
|
||||
* @property string $nickname
|
||||
* @property string $phone
|
||||
* @property string $email
|
||||
* @property string $avatar
|
||||
* @property string $signed
|
||||
* @property AdminUserStatusCode $status
|
||||
* @property string $login_ip
|
||||
* @property string $login_time
|
||||
* @property string $backend_setting
|
||||
* @property int $created_by
|
||||
* @property int $updated_by
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property string $remark
|
||||
*/
|
||||
class AdminUser extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_user';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'id',
|
||||
'username',
|
||||
'password',
|
||||
'user_type',
|
||||
'nickname',
|
||||
'phone',
|
||||
'email',
|
||||
'avatar',
|
||||
'signed',
|
||||
'status',
|
||||
'login_ip',
|
||||
'login_time',
|
||||
'backend_setting',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'remark'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'status' => AdminUserStatusCode::class,
|
||||
'user_type' => AdminUserTypeCode::class,
|
||||
'created_by' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'backend_setting' => 'json',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 隐藏的字段列表.
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $hidden = ['password'];
|
||||
|
||||
|
||||
/**
|
||||
* @return BelongsToMany
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
AdminRole::class,
|
||||
'admin_user_belongs_role',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Creating $event
|
||||
* @return void
|
||||
*/
|
||||
public function creating(Creating $event): void
|
||||
{
|
||||
if (!$this->isDirty('password')) $this->resetPassword();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Deleted $event
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Deleted $event): void
|
||||
{
|
||||
$this->roles()->detach();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function resetPassword(): void
|
||||
{
|
||||
$this->password = 'admin';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return void
|
||||
*/
|
||||
public function setPasswordAttribute(string $password): void
|
||||
{
|
||||
$this->attributes['password'] = password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
public function verifyPassword(string $password): bool
|
||||
{
|
||||
return password_verify($password, $this->password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuperAdmin(): bool
|
||||
{
|
||||
return $this->roles()->where('code','SuperAdmin')->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $fields
|
||||
* @return Collection
|
||||
*/
|
||||
public function getRoles(array $fields): Collection
|
||||
{
|
||||
return $this->roles()
|
||||
->where('status',AdminUserStatusCode::Normal)
|
||||
->select($fields)
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Hyperf\Collection\Collection|Enumerable|Collection<int, AdminMenu>
|
||||
*/
|
||||
public function getPermissions(): Collection|Enumerable|\Hyperf\Collection\Collection
|
||||
{
|
||||
return $this->roles()
|
||||
->with('adminMenus')
|
||||
->orderBy('sort')
|
||||
->get()
|
||||
->pluck('adminMenus')
|
||||
->flatten();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission(string $permission): bool
|
||||
{
|
||||
return $this->roles()
|
||||
->whereRelation('adminMenus','name',$permission)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
27
app/Model/AdminUserBelongsMenu.php
Normal file
27
app/Model/AdminUserBelongsMenu.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
class AdminUserBelongsMenu extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_user_belongs_menu';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [];
|
||||
}
|
||||
32
app/Model/AdminUserBelongsRole.php
Normal file
32
app/Model/AdminUserBelongsRole.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $admin_user_id
|
||||
* @property int $admin_role_id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*/
|
||||
class AdminUserBelongsRole extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_user_belongs_role';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'admin_user_id' => 'integer', 'admin_role_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
|
||||
}
|
||||
37
app/Model/AdminUserLoginLog.php
Normal file
37
app/Model/AdminUserLoginLog.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $admin_user_id
|
||||
* @property string $username
|
||||
* @property string $ip
|
||||
* @property string $os
|
||||
* @property string $browser
|
||||
* @property int $status
|
||||
* @property string $message
|
||||
* @property string $login_time
|
||||
* @property string $remark
|
||||
*/
|
||||
class AdminUserLoginLog extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_user_login_log';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'admin_user_id' => 'integer', 'status' => 'integer'];
|
||||
}
|
||||
37
app/Model/AdminUserOperationLog.php
Normal file
37
app/Model/AdminUserOperationLog.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $admin_user_id
|
||||
* @property string $username
|
||||
* @property string $method
|
||||
* @property string $router
|
||||
* @property string $service_name
|
||||
* @property string $ip
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property string $remark
|
||||
*/
|
||||
class AdminUserOperationLog extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_user_operation_log';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'admin_user_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
|
||||
}
|
||||
78
app/Model/Meta/AdminUserMeta.php
Normal file
78
app/Model/Meta/AdminUserMeta.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model\Meta;
|
||||
|
||||
use App\Model\Model;
|
||||
|
||||
/**
|
||||
* @property string $title 标题
|
||||
* @property string $i18n 国际化
|
||||
* @property string $badge 徽章
|
||||
* @property string $icon 图标
|
||||
* @property bool $affix 是否固定
|
||||
* @property bool $hidden 是否隐藏
|
||||
* @property string $type 类型
|
||||
* @property bool $cache 是否缓存
|
||||
* @property bool $copyright 是否显示版权
|
||||
* @property string $link 链接
|
||||
* @property string $componentPath 视图文件类型
|
||||
* @property string $componentSuffix 视图前缀路径
|
||||
* @property string $breadcrumbEnable 是否显示面包屑
|
||||
* @property string $activeName 激活高亮的菜单标识
|
||||
* @property string $auth 前端权限判断,允许访问的权限码
|
||||
* @property string $role 前端权限判断,允许访问的角色码
|
||||
* @property string $user 前端权限判断,允许访问的用户名
|
||||
*/
|
||||
final class AdminUserMeta extends Model
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public bool $incrementing = false;
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'title',
|
||||
'i18n',
|
||||
'badge',
|
||||
'icon',
|
||||
'affix',
|
||||
'hidden',
|
||||
'type',
|
||||
'cache',
|
||||
'copyright',
|
||||
'breadcrumbEnable',
|
||||
'componentPath',
|
||||
'componentSuffix',
|
||||
'link',
|
||||
'activeName',
|
||||
'auth',
|
||||
'role',
|
||||
'user',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
protected array $casts = [
|
||||
'affix' => 'boolean',
|
||||
'hidden' => 'boolean',
|
||||
'cache' => 'boolean',
|
||||
'copyright' => 'boolean',
|
||||
'breadcrumbEnable' => 'boolean',
|
||||
'title' => 'string',
|
||||
'componentPath' => 'string',
|
||||
'componentSuffix' => 'string',
|
||||
'i18n' => 'string',
|
||||
'badge' => 'string',
|
||||
'icon' => 'string',
|
||||
'type' => 'string',
|
||||
'link' => 'string',
|
||||
'activeName' => 'string',
|
||||
'auth' => 'array',
|
||||
'role' => 'array',
|
||||
'user' => 'array',
|
||||
];
|
||||
}
|
||||
33
app/Model/Meta/MetaCast.php
Normal file
33
app/Model/Meta/MetaCast.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model\Meta;
|
||||
|
||||
use Hyperf\Codec\Json;
|
||||
use Hyperf\Contract\CastsAttributes;
|
||||
|
||||
class MetaCast implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* @param $model
|
||||
* @param string $key
|
||||
* @param $value
|
||||
* @param array $attributes
|
||||
* @return AdminUserMeta
|
||||
*/
|
||||
public function get($model, string $key, $value, array $attributes): AdminUserMeta
|
||||
{
|
||||
return new AdminUserMeta(empty($value) ? [] : Json::decode($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $model
|
||||
* @param string $key
|
||||
* @param $value
|
||||
* @param array $attributes
|
||||
* @return array|string
|
||||
*/
|
||||
public function set($model, string $key, $value, array $attributes): array|string
|
||||
{
|
||||
return Json::encode($value);
|
||||
}
|
||||
}
|
||||
22
app/Model/Model.php
Normal file
22
app/Model/Model.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Model\Model as BaseModel;
|
||||
use Hyperf\ModelCache\Cacheable;
|
||||
use Hyperf\ModelCache\CacheableInterface;
|
||||
|
||||
abstract class Model extends BaseModel implements CacheableInterface
|
||||
{
|
||||
use Cacheable;
|
||||
}
|
||||
Reference in New Issue
Block a user