diff --git a/lib/ts/recipe/dashboard/api/userroles/roles/allRoles.ts b/lib/ts/recipe/dashboard/api/userroles/roles/allRoles.ts new file mode 100644 index 000000000..cded2ba93 --- /dev/null +++ b/lib/ts/recipe/dashboard/api/userroles/roles/allRoles.ts @@ -0,0 +1,40 @@ +import { getAllRoles, getPermissionsForRole } from "../../../../userroles"; + +import { APIFunction, APIInterface, APIOptions } from "../../../types"; + +type Roles = Array<{ role: string; permissions: string[] }>; + +const allRoles: APIFunction = async ( + _: APIInterface, + __: string, + ___: APIOptions, + userContext: any +): Promise<{ + status: "OK"; + roles: Roles; +}> => { + const response = await getAllRoles(userContext); + + let roles: Roles = []; + + for (let i = 0; i < response.roles.length; i++) { + const role = response.roles[i]; + try { + const res = await getPermissionsForRole(role); + + if (res.status === "OK") { + roles.push({ + role, + permissions: res.permissions, + }); + } + } catch (_) {} + } + + return { + roles, + status: "OK", + }; +}; + +export default allRoles; diff --git a/lib/ts/recipe/dashboard/constants.ts b/lib/ts/recipe/dashboard/constants.ts index 49bbabf68..49342ec9b 100644 --- a/lib/ts/recipe/dashboard/constants.ts +++ b/lib/ts/recipe/dashboard/constants.ts @@ -29,4 +29,6 @@ export const SEARCH_TAGS_API = "/api/search/tags"; 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 UNLINK_USER = "/api/user/unlink"; diff --git a/lib/ts/recipe/dashboard/recipe.ts b/lib/ts/recipe/dashboard/recipe.ts index 39323c06c..9a34d58b9 100644 --- a/lib/ts/recipe/dashboard/recipe.ts +++ b/lib/ts/recipe/dashboard/recipe.ts @@ -37,6 +37,7 @@ import { USER_SESSIONS_API, VALIDATE_KEY_API, UNLINK_USER, + USERROLES_LIST_API, } from "./constants"; import NormalisedURLPath from "../../normalisedURLPath"; import type { BaseRequest, BaseResponse } from "../../framework"; @@ -63,6 +64,7 @@ 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"; export default class Recipe extends RecipeModule { private static instance: Recipe | undefined = undefined; @@ -253,6 +255,12 @@ export default class Recipe extends RecipeModule { disabled: false, method: "get", }, + { + id: USERROLES_LIST_API, + pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USERROLES_LIST_API)), + disabled: false, + method: "get", + }, ]; }; @@ -345,6 +353,8 @@ export default class Recipe extends RecipeModule { apiFunction = listTenants; } else if (id === UNLINK_USER) { apiFunction = userUnlink; + } else if (id === USERROLES_LIST_API) { + apiFunction = getAllRoles; } // If the id doesnt match any APIs return false