mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 14:25:40 +08:00
190 lines
4.2 KiB
PHP
190 lines
4.2 KiB
PHP
<?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();
|
|
}
|
|
}
|