mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 17:07:49 +08:00
feat : admin role finish
This commit is contained in:
@@ -11,8 +11,11 @@ declare(strict_types=1);
|
||||
namespace App\Service\Admin\AdminUser;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\AdminMenu;
|
||||
use App\Repository\AdminMenuRepository;
|
||||
use App\Repository\AdminRoleRepository;
|
||||
use App\Service\Admin\BaseAdminService;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class RoleService extends BaseAdminService
|
||||
@@ -23,6 +26,12 @@ class RoleService extends BaseAdminService
|
||||
#[Inject]
|
||||
protected AdminRoleRepository $adminRoleRepository;
|
||||
|
||||
/**
|
||||
* @var AdminMenuRepository
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminMenuRepository $adminMenuRepository;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -53,26 +62,77 @@ class RoleService extends BaseAdminService
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
public function update(int $id)
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(int $id): array
|
||||
{
|
||||
if (!$this->adminRoleRepository->updateById(
|
||||
$id,
|
||||
array_merge()
|
||||
));
|
||||
array_merge(
|
||||
$this->getRequestData(),
|
||||
['updated_by' => $this->adminId]
|
||||
)
|
||||
)) throw new ErrException('更新失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
public function delete()
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function delete(): array
|
||||
{
|
||||
if (!$this->adminRoleRepository->deleteById($this->getRequestData())) throw new ErrException('删除失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
public function getRole()
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function getRole(int $id): array
|
||||
{
|
||||
|
||||
return $this->adminReturn->success(
|
||||
$this->adminRoleRepository
|
||||
->findById($id)
|
||||
->adminMenus()
|
||||
->get()
|
||||
->map(static fn (AdminMenu $adminMenu) => $adminMenu->only([
|
||||
'id' , 'name'
|
||||
]))->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function setRole()
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function setRole(int $id): array
|
||||
{
|
||||
if (!$this->adminRoleRepository->existsById($id)) throw new ErrException('角色不存在');
|
||||
|
||||
$permissionsCode = Arr::get($this->getRequestData(), 'permissions', []);
|
||||
|
||||
if (count($permissionsCode) === 0) {
|
||||
$this->adminRoleRepository->findById($id)->adminMenus()->detach();
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
if (!$this->adminRoleRepository
|
||||
->findById($id)
|
||||
->adminMenus()
|
||||
->sync(
|
||||
$this->adminMenuRepository
|
||||
->list([
|
||||
'code' => $permissionsCode
|
||||
])
|
||||
->map(static fn ($item) => $item->id)
|
||||
->toArray()
|
||||
)) throw new ErrException('更新失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user