Skip to content

Commit

Permalink
prog: added new endpoints for handling role and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Oct 16, 2023
1 parent cc81759 commit b773e22
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getPermissionsForRole as getPermissions } from "../../../../userroles";

import { APIInterface, APIOptions } from "../../../types";

import STError from "../../../../../error";

const getPermissionsForRole = async ( _: APIInterface, ___: string, options: APIOptions, __: any)=>{
const role = options.req.getKeyValueFromQuery("role")

if (role === undefined) {
throw new STError({
message: "Required parameter 'role' is missing or has an invalid type",
type: STError.BAD_INPUT_ERROR,
});
}

const response = await getPermissions(role);

if(response.status === "OK"){
response.permissions
}
return []
}
29 changes: 29 additions & 0 deletions lib/ts/recipe/dashboard/api/userroles/roles/createRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { APIInterface, APIOptions } from "../../../types";

import { createNewRoleOrAddPermissions } from "../../../../userroles";
import STError from "../../../../../error";

const createRole = async (_: APIInterface, __: string, options: APIOptions, ___: any) => {
const requestBody = await options.req.getJSONBody();
const permissions = requestBody.permissions;
const role = requestBody.role;

if (permissions === undefined) {
throw new STError({
message: "Required parameter 'permissions' is missing or has an invalid type",
type: STError.BAD_INPUT_ERROR,
});
}

if (role === undefined) {
throw new STError({
message: "Required parameter 'role' is missing or has an invalid type",
type: STError.BAD_INPUT_ERROR,
});
}

const response = await createNewRoleOrAddPermissions(role, permissions);
return response;
};

export default createRole;
21 changes: 21 additions & 0 deletions lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { deleteRole as roleDelete } from "../../../../userroles";

import { APIInterface, APIOptions } from "../../../types";

import STError from "../../../../../error";

const deleteRole = async (_: APIInterface, ___: string, options: APIOptions, __: any) => {
const role = options.req.getKeyValueFromQuery("userId");

if (role === undefined) {
throw new STError({
message: "Required parameter 'role' is missing or has an invalid type",
type: STError.BAD_INPUT_ERROR,
});
}

const response = await roleDelete(role);
return response;
};

export default deleteRole;
2 changes: 2 additions & 0 deletions lib/ts/recipe/dashboard/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ export const DASHBOARD_ANALYTICS_API = "/api/analytics";
export const TENANTS_LIST_API = "/api/tenants/list";

export const USERROLES_LIST_API = "/api/userroles/roles";
export const USERROLES_CREATE_ROLE_API = "/api/userroles/role";
export const USERROLES_DELETE_ROLE_API = "/api/userroles/role";

export const UNLINK_USER = "/api/user/unlink";
22 changes: 21 additions & 1 deletion lib/ts/recipe/dashboard/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
VALIDATE_KEY_API,
UNLINK_USER,
USERROLES_LIST_API,
USERROLES_CREATE_ROLE_API,
USERROLES_DELETE_ROLE_API,
} from "./constants";
import NormalisedURLPath from "../../normalisedURLPath";
import type { BaseRequest, BaseResponse } from "../../framework";
Expand All @@ -64,7 +66,9 @@ import { getSearchTags } from "./api/search/tagsGet";
import analyticsPost from "./api/analytics";
import listTenants from "./api/listTenants";
import { userUnlink } from "./api/userdetails/userUnlinkGet";
import { getAllRoles } from "../userroles";
import getAllRoles from "./api/userroles/roles/getAllRoles";
import createRole from "./api/userroles/roles/createRole";
import deleteRole from "./api/userroles/roles/deleteRole";

export default class Recipe extends RecipeModule {
private static instance: Recipe | undefined = undefined;
Expand Down Expand Up @@ -261,6 +265,18 @@ export default class Recipe extends RecipeModule {
disabled: false,
method: "get",
},
{
id: USERROLES_CREATE_ROLE_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USERROLES_CREATE_ROLE_API)),
disabled: false,
method: "post",
},
{
id: USERROLES_DELETE_ROLE_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USERROLES_DELETE_ROLE_API)),
disabled: false,
method: "delete",
},
];
};

Expand Down Expand Up @@ -355,6 +371,10 @@ export default class Recipe extends RecipeModule {
apiFunction = userUnlink;
} else if (id === USERROLES_LIST_API) {
apiFunction = getAllRoles;
} else if (id === USERROLES_CREATE_ROLE_API) {
apiFunction = createRole;
} else if (id === USERROLES_DELETE_ROLE_API) {
apiFunction = deleteRole;
}

// If the id doesnt match any APIs return false
Expand Down

0 comments on commit b773e22

Please sign in to comment.