'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 */ 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(); } }