roles
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* POST /v1/role/:identifier/permissions */
|
||||
export default createRoute(
|
||||
"post",
|
||||
["/:identifier/permissions"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z.object({
|
||||
permissions: z
|
||||
.array(z.string().min(1, "Permission node cannot be empty"))
|
||||
.min(1, "At least one permission is required"),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
await role.addPermissions(...data.permissions);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Permissions Added Successfully!",
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.update"] }),
|
||||
);
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* POST /v1/role */
|
||||
export default createRoute(
|
||||
"post",
|
||||
["/"],
|
||||
|
||||
async (c) => {
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
moniker: z.string().min(1, "Moniker is required"),
|
||||
permissions: z
|
||||
.array(z.string().min(1, "Permission node cannot be empty"))
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const role = await roles.create(data);
|
||||
|
||||
const response = apiResponse.created(
|
||||
"Role Created Successfully!",
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.create"] }),
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* DELETE /v1/role/:identifier */
|
||||
export default createRoute(
|
||||
"delete",
|
||||
["/:identifier"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
await role.delete();
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Role Deleted Successfully!",
|
||||
role.toJson(),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.delete"] }),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* GET /v1/role/:identifier */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/:identifier"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Role Fetched Successfully!",
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.read"] }),
|
||||
);
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* GET /v1/role */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/"],
|
||||
|
||||
async (c) => {
|
||||
const allRoles = await roles.fetchAllRoles();
|
||||
|
||||
const rolesArray = allRoles.map((role) =>
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Roles Fetched Successfully!",
|
||||
rolesArray,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.read", "role.list"] }),
|
||||
);
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* GET /v1/role/:identifier/users */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/:identifier/users"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
const users = role.getUsers();
|
||||
|
||||
const usersArray = users.map((user) => user.toJson());
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Users Fetched Successfully!",
|
||||
usersArray,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.read", "user.read"] }),
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
export { default as create } from "./create";
|
||||
export { default as fetch } from "./fetch";
|
||||
export { default as fetchAll } from "./fetchAll";
|
||||
export { default as update } from "./update";
|
||||
export { default as deleteRole } from "./delete";
|
||||
export { default as addPermissions } from "./addPermissions";
|
||||
export { default as removePermissions } from "./removePermissions";
|
||||
export { default as getUsers } from "./getUsers";
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* DELETE /v1/role/:identifier/permissions */
|
||||
export default createRoute(
|
||||
"delete",
|
||||
["/:identifier/permissions"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z.object({
|
||||
permissions: z
|
||||
.array(z.string().min(1, "Permission node cannot be empty"))
|
||||
.min(1, "At least one permission is required"),
|
||||
});
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
await role.removePermissions(...data.permissions);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Permissions Removed Successfully!",
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.update"] }),
|
||||
);
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { roles } from "../../managers/roles";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
import { z } from "zod";
|
||||
|
||||
/* PATCH /v1/role/:identifier */
|
||||
export default createRoute(
|
||||
"patch",
|
||||
["/:identifier"],
|
||||
|
||||
async (c) => {
|
||||
const identifier = c.req.param("identifier");
|
||||
const body = await c.req.json();
|
||||
|
||||
const schema = z
|
||||
.object({
|
||||
title: z.string().min(1, "Title cannot be empty"),
|
||||
moniker: z.string().min(1, "Moniker cannot be empty"),
|
||||
permissions: z.array(
|
||||
z.string().min(1, "Permission node cannot be empty"),
|
||||
),
|
||||
})
|
||||
.partial()
|
||||
.strict();
|
||||
|
||||
const data = schema.parse(body);
|
||||
|
||||
const role = await roles.fetch(identifier);
|
||||
await role.update(data);
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Role Updated Successfully!",
|
||||
role.toJson({ viewPermissions: true }),
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["role.update"] }),
|
||||
);
|
||||
Reference in New Issue
Block a user