feat: role based permission endpoints
This commit is contained in:
parent
52bf59cc64
commit
2e5fba0b07
1 changed files with 166 additions and 0 deletions
|
|
@ -83,6 +83,82 @@ export class MenuController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RoleMenuPermissionCreate = {
|
||||||
|
userRole: string;
|
||||||
|
permission: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type RoleMenuPermissionEdit = {
|
||||||
|
userRole?: string;
|
||||||
|
permission?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Route("v1/permission/menu/{menuId}/role")
|
||||||
|
@Tags("Permission")
|
||||||
|
@Security("keycloak")
|
||||||
|
export class RoleMenuController extends Controller {
|
||||||
|
@Get()
|
||||||
|
async listRoleMenu(@Path() menuId: string) {
|
||||||
|
const record = await prisma.roleMenuPermission.findMany({
|
||||||
|
where: { menuId },
|
||||||
|
orderBy: [{ userRole: "asc" }, { createdAt: "asc" }],
|
||||||
|
});
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
async createRoleMenu(@Path() menuId: string, @Body() body: RoleMenuPermissionCreate) {
|
||||||
|
const menu = await prisma.menu.findFirst({ where: { id: menuId } });
|
||||||
|
|
||||||
|
if (!menu) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Menu not found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const record = await prisma.roleMenuPermission.create({
|
||||||
|
data: Object.assign(body, { menuId }),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put("{roleMenuId}")
|
||||||
|
async editRoleMenu(
|
||||||
|
@Path("roleMenuId") id: string,
|
||||||
|
@Path() menuId: string,
|
||||||
|
@Body() body: RoleMenuPermissionEdit,
|
||||||
|
) {
|
||||||
|
const record = await prisma.roleMenuPermission
|
||||||
|
.update({
|
||||||
|
where: { id, menuId },
|
||||||
|
data: body,
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Role menu cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
throw new Error(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{roleMenuId}")
|
||||||
|
async deleteRoleMenu(@Path("roleMenuId") id: string, @Path() menuId: string) {
|
||||||
|
const record = await prisma.roleMenuPermission.deleteMany({
|
||||||
|
where: { id, menuId },
|
||||||
|
});
|
||||||
|
if (record.count <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Role menu cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type MenuComponentCreate = {
|
type MenuComponentCreate = {
|
||||||
componentId: string;
|
componentId: string;
|
||||||
componentTag: string;
|
componentTag: string;
|
||||||
|
|
@ -176,3 +252,93 @@ export class MenuComponentController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RoleMenuComponentPermissionCreate = {
|
||||||
|
userRole: string;
|
||||||
|
permission: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type RoleMenuComponentPermissionEdit = {
|
||||||
|
userRole?: string;
|
||||||
|
permission?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Route("v1/permission/menu-component/{menuComponentId}/role")
|
||||||
|
@Tags("Permission")
|
||||||
|
@Security("keycloak")
|
||||||
|
export class RoleMenuComponentController extends Controller {
|
||||||
|
@Get()
|
||||||
|
async listRoleMenuComponent(@Path() menuComponentId: string) {
|
||||||
|
const record = await prisma.roleMenuComponentPermission.findMany({
|
||||||
|
where: { menuComponentId },
|
||||||
|
orderBy: [{ userRole: "asc" }, { createdAt: "asc" }],
|
||||||
|
});
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
async createRoleMenuComponent(
|
||||||
|
@Path() menuComponentId: string,
|
||||||
|
@Body() body: RoleMenuComponentPermissionCreate,
|
||||||
|
) {
|
||||||
|
const menu = await prisma.menuComponent.findFirst({ where: { id: menuComponentId } });
|
||||||
|
|
||||||
|
if (!menu) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Menu not found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const record = await prisma.roleMenuComponentPermission.create({
|
||||||
|
data: Object.assign(body, { menuComponentId }),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put("{roleMenuComponentId}")
|
||||||
|
async editRoleMenuComponent(
|
||||||
|
@Path("roleMenuComponentId") id: string,
|
||||||
|
@Path() menuComponentId: string,
|
||||||
|
@Body() body: RoleMenuComponentPermissionEdit,
|
||||||
|
) {
|
||||||
|
const record = await prisma.roleMenuComponentPermission
|
||||||
|
.update({
|
||||||
|
where: { id, menuComponentId },
|
||||||
|
data: body,
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
"Role menu component cannot be found.",
|
||||||
|
"data_not_found",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new Error(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{roleMenuComponentId}")
|
||||||
|
async deleteRoleMenuComponent(
|
||||||
|
@Path("roleMenuComponentId") id: string,
|
||||||
|
@Path() menuComponentId: string,
|
||||||
|
) {
|
||||||
|
const record = await prisma.roleMenuComponentPermission.deleteMany({
|
||||||
|
where: { id, menuComponentId },
|
||||||
|
});
|
||||||
|
if (record.count <= 0) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
"Role menu component cannot be found.",
|
||||||
|
"data_not_found",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue