mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 15:57:50 +08:00
feat : admin role finish
This commit is contained in:
@@ -37,38 +37,56 @@ class AdminRoleController
|
|||||||
return $this->service->handle();
|
return $this->service->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
#[RequestMapping(path: "", methods: "POST")]
|
#[RequestMapping(path: "", methods: "POST")]
|
||||||
#[Permission(code: 'permission:role:save')]
|
#[Permission(code: 'permission:role:save')]
|
||||||
public function createRole()
|
public function createRole(): array
|
||||||
{
|
{
|
||||||
return $this->service->create();
|
return $this->service->create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
#[RequestMapping(path: "{id}", methods: "PUT")]
|
#[RequestMapping(path: "{id}", methods: "PUT")]
|
||||||
#[Permission(code: 'permission:role:update')]
|
#[Permission(code: 'permission:role:update')]
|
||||||
public function updateRole(int $id)
|
public function updateRole(int $id): array
|
||||||
{
|
{
|
||||||
return $this->service->update($id);
|
return $this->service->update($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
#[RequestMapping(path: "", methods: "DELETE")]
|
#[RequestMapping(path: "", methods: "DELETE")]
|
||||||
#[Permission(code: 'permission:role:delete')]
|
#[Permission(code: 'permission:role:delete')]
|
||||||
public function deleteRole()
|
public function deleteRole(): array
|
||||||
{
|
{
|
||||||
return $this->service->delete();
|
return $this->service->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
#[RequestMapping(path: "{id}/permission", methods: "GET")]
|
#[RequestMapping(path: "{id}/permission", methods: "GET")]
|
||||||
#[Permission(code: 'permission:role:getMenu')]
|
#[Permission(code: 'permission:role:getMenu')]
|
||||||
public function getRolePermission()
|
public function getRolePermission(int $id): array
|
||||||
{
|
{
|
||||||
return $this->service->getRole();
|
return $this->service->getRole($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
#[RequestMapping(path: "{id}/permission", methods: "PUT")]
|
#[RequestMapping(path: "{id}/permission", methods: "PUT")]
|
||||||
#[Permission(code: 'permission:role:setMenu')]
|
#[Permission(code: 'permission:role:setMenu')]
|
||||||
public function batchGrantPermissionByRole()
|
public function batchGrantPermissionByRole(int $id): array
|
||||||
{
|
{
|
||||||
return $this->service->setRole();
|
return $this->service->setRole($id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,11 @@ declare(strict_types=1);
|
|||||||
namespace App\Service\Admin\AdminUser;
|
namespace App\Service\Admin\AdminUser;
|
||||||
|
|
||||||
use App\Exception\ErrException;
|
use App\Exception\ErrException;
|
||||||
|
use App\Model\AdminMenu;
|
||||||
|
use App\Repository\AdminMenuRepository;
|
||||||
use App\Repository\AdminRoleRepository;
|
use App\Repository\AdminRoleRepository;
|
||||||
use App\Service\Admin\BaseAdminService;
|
use App\Service\Admin\BaseAdminService;
|
||||||
|
use Hyperf\Collection\Arr;
|
||||||
use Hyperf\Di\Annotation\Inject;
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
class RoleService extends BaseAdminService
|
class RoleService extends BaseAdminService
|
||||||
@@ -23,6 +26,12 @@ class RoleService extends BaseAdminService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected AdminRoleRepository $adminRoleRepository;
|
protected AdminRoleRepository $adminRoleRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var AdminMenuRepository
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected AdminMenuRepository $adminMenuRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -53,26 +62,77 @@ class RoleService extends BaseAdminService
|
|||||||
return $this->adminReturn->success();
|
return $this->adminReturn->success();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(int $id)
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function update(int $id): array
|
||||||
{
|
{
|
||||||
if (!$this->adminRoleRepository->updateById(
|
if (!$this->adminRoleRepository->updateById(
|
||||||
$id,
|
$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