refactor(system/role): 重构角色管理

This commit is contained in:
2025-02-06 20:36:18 +08:00
parent 4caada8c64
commit f6535ef7a3
20 changed files with 455 additions and 77 deletions

View File

@@ -27,13 +27,18 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.system.model.query.RoleQuery;
import top.continew.admin.system.model.query.RoleUserQuery;
import top.continew.admin.system.model.req.RoleReq;
import top.continew.admin.system.model.resp.RoleDetailResp;
import top.continew.admin.system.model.resp.RoleResp;
import top.continew.admin.system.model.req.RoleUpdatePermissionReq;
import top.continew.admin.system.model.resp.role.RoleDetailResp;
import top.continew.admin.system.model.resp.role.RoleResp;
import top.continew.admin.system.model.resp.role.RoleUserResp;
import top.continew.admin.system.service.RoleService;
import top.continew.admin.system.service.UserRoleService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import java.util.List;
@@ -47,12 +52,30 @@ import java.util.List;
@Validated
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/system/role", api = {Api.PAGE, Api.DETAIL, Api.ADD, Api.UPDATE, Api.DELETE})
@CrudRequestMapping(value = "/system/role", api = {Api.LIST, Api.DETAIL, Api.ADD, Api.UPDATE, Api.DELETE})
public class RoleController extends BaseController<RoleService, RoleResp, RoleDetailResp, RoleQuery, RoleReq> {
private final UserRoleService userRoleService;
@Operation(summary = "分配角色给用户", description = "批量分配角色给用户")
@Operation(summary = "修改权限", description = "修改角色的功能权限")
@SaCheckPermission("system:role:updatePermission")
@PutMapping("/{id}/permission")
public void updatePermission(@PathVariable("id") Long id, @Validated @RequestBody RoleUpdatePermissionReq req) {
baseService.updatePermission(id, req);
}
@Operation(summary = "分页查询关联用户", description = "分页查询角色关联的用户列表")
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
@SaCheckPermission("system:role:list")
@GetMapping("/{id}/user")
public PageResp<RoleUserResp> pageUser(@PathVariable("id") Long id,
@Validated RoleUserQuery query,
@Validated PageQuery pageQuery) {
query.setRoleId(id);
return userRoleService.pageUser(query, pageQuery);
}
@Operation(summary = "分配用户", description = "批量分配角色给用户")
@SaCheckPermission("system:role:assign")
@PostMapping("/{id}/user")
public void assignToUsers(@PathVariable("id") Long id,
@@ -60,10 +83,18 @@ public class RoleController extends BaseController<RoleService, RoleResp, RoleDe
baseService.assignToUsers(id, userIds);
}
@Operation(summary = "查询角色关联用户", description = "查询角色关联的用户ID列表")
@Operation(summary = "取消分配用户", description = "批量取消分配角色给用户")
@SaCheckPermission("system:role:unassign")
@DeleteMapping("/user")
public void unassignFromUsers(@Validated @NotEmpty(message = "用户列表不能为空") @RequestBody List<Long> userRoleIds) {
userRoleService.deleteByIds(userRoleIds);
}
@Operation(summary = "查询关联用户ID", description = "查询角色关联的用户ID列表")
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
@GetMapping("/{id}/user")
public List<Long> listUser(@PathVariable("id") Long id) {
@SaCheckPermission("system:role:list")
@GetMapping("/{id}/user/id")
public List<Long> listUserId(@PathVariable("id") Long id) {
return userRoleService.listUserIdByRoleId(id);
}
}

View File

@@ -24,7 +24,9 @@ VALUES
(1033, '新增', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:add', 3, 1, 1, NOW()),
(1034, '修改', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:update', 4, 1, 1, NOW()),
(1035, '删除', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:delete', 5, 1, 1, NOW()),
(1036, '分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:assign', 6, 1, 1, NOW()),
(1036, '修改权限', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:updatePermission', 6, 1, 1, NOW()),
(1037, '分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:assign', 7, 1, 1, NOW()),
(1038, '取消分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:unassign', 8, 1, 1, NOW()),
(1050, '菜单管理', 1000, 2, '/system/menu', 'SystemMenu', 'system/menu/index', NULL, 'menu', b'0', b'0', b'0', NULL, 3, 1, 1, NOW()),
(1051, '列表', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:list', 1, 1, 1, NOW()),
@@ -203,19 +205,19 @@ VALUES
-- 初始化默认用户和角色关联数据
INSERT INTO `sys_user_role`
(`user_id`, `role_id`)
(`id`, `user_id`, `role_id`)
VALUES
(1, 1),
(547889293968801822, 547888897925840927),
(547889293968801823, 547888897925840928),
(547889293968801824, 547888897925840928),
(547889293968801825, 547888897925840928),
(547889293968801826, 547888897925840928),
(547889293968801827, 547888897925840928),
(547889293968801828, 547888897925840928),
(547889293968801829, 547888897925840928),
(547889293968801830, 547888897925840928),
(547889293968801831, 547888897925840928);
(1, 1, 1),
(2, 547889293968801822, 547888897925840927),
(3, 547889293968801823, 547888897925840928),
(4, 547889293968801824, 547888897925840928),
(5, 547889293968801825, 547888897925840928),
(6, 547889293968801826, 547888897925840928),
(7, 547889293968801827, 547888897925840928),
(8, 547889293968801828, 547888897925840928),
(9, 547889293968801829, 547888897925840928),
(10, 547889293968801830, 547888897925840928),
(11, 547889293968801831, 547888897925840928);
-- 初始化默认角色和菜单关联数据
INSERT INTO `sys_role_menu`

View File

@@ -119,9 +119,11 @@ CREATE TABLE IF NOT EXISTS `sys_user_social` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户社会化关联表';
CREATE TABLE IF NOT EXISTS `sys_user_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`role_id` bigint(20) NOT NULL COMMENT '角色ID',
PRIMARY KEY (`user_id`, `role_id`)
PRIMARY KEY (`id`),
UNIQUE INDEX `uk_user_id_role_id`(`user_id`, `role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户和角色关联表';
CREATE TABLE IF NOT EXISTS `sys_role_menu` (

View File

@@ -24,7 +24,9 @@ VALUES
(1033, '新增', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:add', 3, 1, 1, NOW()),
(1034, '修改', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:update', 4, 1, 1, NOW()),
(1035, '删除', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:delete', 5, 1, 1, NOW()),
(1036, '分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:assign', 6, 1, 1, NOW()),
(1036, '修改权限', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:updatePermission', 6, 1, 1, NOW()),
(1037, '分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:assign', 7, 1, 1, NOW()),
(1038, '取消分配', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:unassign', 8, 1, 1, NOW()),
(1050, '菜单管理', 1000, 2, '/system/menu', 'SystemMenu', 'system/menu/index', NULL, 'menu', false, false, false, NULL, 3, 1, 1, NOW()),
(1051, '列表', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:list', 1, 1, 1, NOW()),
@@ -203,19 +205,19 @@ VALUES
-- 初始化默认用户和角色关联数据
INSERT INTO "sys_user_role"
("user_id", "role_id")
("id", "user_id", "role_id")
VALUES
(1, 1),
(547889293968801822, 547888897925840927),
(547889293968801823, 547888897925840928),
(547889293968801824, 547888897925840928),
(547889293968801825, 547888897925840928),
(547889293968801826, 547888897925840928),
(547889293968801827, 547888897925840928),
(547889293968801828, 547888897925840928),
(547889293968801829, 547888897925840928),
(547889293968801830, 547888897925840928),
(547889293968801831, 547888897925840928);
(1, 1, 1),
(2, 547889293968801822, 547888897925840927),
(3, 547889293968801823, 547888897925840928),
(4, 547889293968801824, 547888897925840928),
(5, 547889293968801825, 547888897925840928),
(6, 547889293968801826, 547888897925840928),
(7, 547889293968801827, 547888897925840928),
(8, 547889293968801828, 547888897925840928),
(9, 547889293968801829, 547888897925840928),
(10, 547889293968801830, 547888897925840928),
(11, 547889293968801831, 547888897925840928);
-- 初始化默认角色和菜单关联数据
INSERT INTO "sys_role_menu"

View File

@@ -197,10 +197,13 @@ COMMENT ON COLUMN "sys_user_social"."create_time" IS '创建时间';
COMMENT ON TABLE "sys_user_social" IS '用户社会化关联表';
CREATE TABLE IF NOT EXISTS "sys_user_role" (
"id" int8 NOT NULL,
"user_id" int8 NOT NULL,
"role_id" int8 NOT NULL,
PRIMARY KEY ("user_id", "role_id")
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "uk_user_id_role_id" ON "sys_user_role" ("user_id", "role_id");
COMMENT ON COLUMN "sys_user_role"."id" IS 'ID';
COMMENT ON COLUMN "sys_user_role"."user_id" IS '用户ID';
COMMENT ON COLUMN "sys_user_role"."role_id" IS '角色ID';
COMMENT ON TABLE "sys_user_role" IS '用户和角色关联表';