mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 21:37:50 +08:00
feat : admin log
This commit is contained in:
132
app/Service/Admin/AdminUser/PermissionService.php
Normal file
132
app/Service/Admin/AdminUser/PermissionService.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\AdminUser;
|
||||
|
||||
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
|
||||
use App\Constants\Model\AdminUser\AdminRoleStatusCode;
|
||||
use App\Constants\ResultCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Repository\AdminMenuRepository;
|
||||
use App\Repository\AdminRoleRepository;
|
||||
use App\Service\Admin\BaseAdminService;
|
||||
use App\Trait\AdminUserTrait;
|
||||
use Hyperf\Collection\Arr;
|
||||
|
||||
class PermissionService extends BaseAdminService
|
||||
{
|
||||
use AdminUserTrait;
|
||||
|
||||
/**
|
||||
* @var AdminRoleRepository
|
||||
*/
|
||||
protected AdminRoleRepository $adminRoleRepository;
|
||||
|
||||
/**
|
||||
* @var AdminMenuRepository
|
||||
*/
|
||||
protected AdminMenuRepository $adminMenuRepository;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return $this->adminReturn->success(
|
||||
'success',
|
||||
$this->getAdminUserInfo($this->adminId)->isSuperAdmin() ?
|
||||
$this->getAdminMenuBySuperAdmin() :
|
||||
$this->getAdminMenuByAdminId()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getAdminMenuByAdminId(): array
|
||||
{
|
||||
$permissions = $this->getAdminUserInfo($this->adminId)->getPermissions()->pluck('name')->unique();
|
||||
|
||||
$menuList = $permissions->isEmpty() ?
|
||||
[] :
|
||||
$this->adminMenuRepository->list([
|
||||
'status' => AdminMenuStatusCode::Normal,
|
||||
'name' => $permissions->toArray()
|
||||
])->toArray();
|
||||
|
||||
$tree = [];
|
||||
$map = [];
|
||||
|
||||
foreach ($menuList as &$menu) {
|
||||
$menu['children'] = [];
|
||||
$map[$menu['id']] = &$menu;
|
||||
}
|
||||
unset($menu);
|
||||
|
||||
foreach ($menuList as &$menu) {
|
||||
$pid = $menu['parent_id'];
|
||||
if ($pid === 0 || !isset($map[$pid])) {
|
||||
$tree[] = &$menu;
|
||||
} else {
|
||||
$map[$pid]['children'][] = &$menu;
|
||||
}
|
||||
}
|
||||
unset($menu);
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getAdminMenuBySuperAdmin(): array
|
||||
{
|
||||
return $this->adminMenuRepository->list([
|
||||
'status' => AdminMenuStatusCode::Normal,
|
||||
'children' => true,
|
||||
'parent_id' => 0,
|
||||
])?->toArray() ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getRoleByAdminUser(): array
|
||||
{
|
||||
return $this->adminReturn->success(
|
||||
'success',
|
||||
$this->getAdminUserInfo($this->adminId)->isSuperAdmin() ?
|
||||
$this->adminRoleRepository->list(['status' => AdminRoleStatusCode::Normal])->toArray() :
|
||||
$this->getAdminUserInfo($this->adminId)->getRoles(['name', 'code', 'remark'])->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function update(): array
|
||||
{
|
||||
$userInfo = $this->getAdminUserInfo($this->adminId);
|
||||
if (!$userInfo) throw new ErrException('用户不存在');
|
||||
|
||||
$data = $this->getRequestData();
|
||||
|
||||
if (Arr::exists($data, 'new_password')) {
|
||||
if (!$userInfo->verifyPassword(Arr::get($data,'old_password')))
|
||||
throw new ErrException('旧密码错误',ResultCode::OLD_PASSWORD_ERROR);
|
||||
|
||||
$data['password'] = $data['new_password'];
|
||||
}
|
||||
|
||||
if (!$this->adminUserRepository->updateById($userInfo->id, $data)) throw new ErrException('更新失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user