diff --git a/lib/build/recipe/dashboard/api/userroles/addRoleToUser.d.ts b/lib/build/recipe/dashboard/api/userroles/addRoleToUser.d.ts index 0f030f7d2..8485ed162 100644 --- a/lib/build/recipe/dashboard/api/userroles/addRoleToUser.d.ts +++ b/lib/build/recipe/dashboard/api/userroles/addRoleToUser.d.ts @@ -5,7 +5,13 @@ declare const addRoleToUser: ( tenantId: string, options: APIOptions, __: any -) => Promise<{ - status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; -}>; +) => Promise< + | { + status: "OK"; + didUserAlreadyHaveRole: boolean; + } + | { + status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; + } +>; export default addRoleToUser; diff --git a/lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts b/lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts index 83d89cbc8..bdf782e49 100644 --- a/lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts +++ b/lib/build/recipe/dashboard/api/userroles/removeUserRole.d.ts @@ -5,7 +5,13 @@ declare const removeUserRole: ( tenantId: string, options: APIOptions, __: any -) => Promise<{ - status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; -}>; +) => Promise< + | { + status: "OK"; + didUserHaveRole: boolean; + } + | { + status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; + } +>; export default removeUserRole; diff --git a/lib/build/recipe/dashboard/api/userroles/roles/createRole.d.ts b/lib/build/recipe/dashboard/api/userroles/roles/createRole.d.ts deleted file mode 100644 index 953788ae0..000000000 --- a/lib/build/recipe/dashboard/api/userroles/roles/createRole.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -// @ts-nocheck -import { APIInterface, APIOptions } from "../../../types"; -declare const createRole: ( - _: APIInterface, - __: string, - options: APIOptions, - ___: any -) => Promise<{ - status: "OK" | "ROLE_ALREADY_EXITS" | "FEATURE_NOT_ENABLED_ERROR"; -}>; -export default createRole; diff --git a/lib/build/recipe/dashboard/api/userroles/roles/createRole.js b/lib/build/recipe/dashboard/api/userroles/roles/createRole.js deleted file mode 100644 index 75e8cab0b..000000000 --- a/lib/build/recipe/dashboard/api/userroles/roles/createRole.js +++ /dev/null @@ -1,37 +0,0 @@ -"use strict"; -var __importDefault = - (this && this.__importDefault) || - function (mod) { - return mod && mod.__esModule ? mod : { default: mod }; - }; -Object.defineProperty(exports, "__esModule", { value: true }); -const recipe_1 = __importDefault(require("../../../../userroles/recipe")); -const userroles_1 = __importDefault(require("../../../../userroles")); -const error_1 = __importDefault(require("../../../../../error")); -const createRole = async (_, __, options, ___) => { - try { - recipe_1.default.getInstanceOrThrowError(); - } catch (_) { - return { - status: "FEATURE_NOT_ENABLED_ERROR", - }; - } - const requestBody = await options.req.getJSONBody(); - const permissions = requestBody.permissions; - const role = requestBody.role; - if (role === undefined || typeof role !== "string") { - throw new error_1.default({ - message: "Required parameter 'role' is missing or has an invalid type", - type: error_1.default.BAD_INPUT_ERROR, - }); - } - if (permissions === undefined || Array.isArray(permissions) === false) { - throw new error_1.default({ - message: "Required parameter 'permissions' is missing or has an invalid type", - type: error_1.default.BAD_INPUT_ERROR, - }); - } - const response = await userroles_1.default.createNewRoleOrAddPermissions(role, permissions); - return response; -}; -exports.default = createRole; diff --git a/lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.d.ts b/lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.d.ts similarity index 70% rename from lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.d.ts rename to lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.d.ts index f7b1cfba0..75663bcb0 100644 --- a/lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.d.ts +++ b/lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.d.ts @@ -1,10 +1,10 @@ // @ts-nocheck import { APIInterface, APIOptions } from "../../../types"; -declare const addPermissions: ( +declare const createRoleOrAddPermissions: ( _: APIInterface, - ___: string, + __: string, options: APIOptions, - __: any + ___: any ) => Promise< | { status: "OK"; @@ -14,4 +14,4 @@ declare const addPermissions: ( status: "FEATURE_NOT_ENABLED_ERROR"; } >; -export default addPermissions; +export default createRoleOrAddPermissions; diff --git a/lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.js b/lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.js similarity index 92% rename from lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.js rename to lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.js index 4f7e877a6..1540fe51d 100644 --- a/lib/build/recipe/dashboard/api/userroles/permissions/addPermissions.js +++ b/lib/build/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.js @@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const recipe_1 = __importDefault(require("../../../../userroles/recipe")); const userroles_1 = __importDefault(require("../../../../userroles")); const error_1 = __importDefault(require("../../../../../error")); -const addPermissions = async (_, ___, options, __) => { +const createRoleOrAddPermissions = async (_, __, options, ___) => { try { recipe_1.default.getInstanceOrThrowError(); } catch (_) { @@ -17,8 +17,8 @@ const addPermissions = async (_, ___, options, __) => { }; } const requestBody = await options.req.getJSONBody(); - const role = requestBody.role; const permissions = requestBody.permissions; + const role = requestBody.role; if (role === undefined || typeof role !== "string") { throw new error_1.default({ message: "Required parameter 'role' is missing or has an invalid type", @@ -34,4 +34,4 @@ const addPermissions = async (_, ___, options, __) => { const response = await userroles_1.default.createNewRoleOrAddPermissions(role, permissions); return response; }; -exports.default = addPermissions; +exports.default = createRoleOrAddPermissions; diff --git a/lib/build/recipe/dashboard/api/userroles/roles/deleteRole.d.ts b/lib/build/recipe/dashboard/api/userroles/roles/deleteRole.d.ts index aed08db7c..14e5cf5b7 100644 --- a/lib/build/recipe/dashboard/api/userroles/roles/deleteRole.d.ts +++ b/lib/build/recipe/dashboard/api/userroles/roles/deleteRole.d.ts @@ -5,7 +5,13 @@ declare const deleteRole: ( ___: string, options: APIOptions, __: any -) => Promise<{ - status: "OK" | "ROLE_DO_NOT_EXISTS" | "FEATURE_NOT_ENABLED_ERROR"; -}>; +) => Promise< + | { + status: "OK"; + didRoleExist: boolean; + } + | { + status: "FEATURE_NOT_ENABLED_ERROR"; + } +>; export default deleteRole; diff --git a/lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js b/lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js index f6c49e2a9..e02939ae7 100644 --- a/lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js +++ b/lib/build/recipe/dashboard/api/userroles/roles/getAllRoles.js @@ -6,8 +6,9 @@ var __importDefault = }; Object.defineProperty(exports, "__esModule", { value: true }); const userroles_1 = __importDefault(require("../../../../userroles")); +const error_1 = __importDefault(require("../../../../../error")); const recipe_1 = __importDefault(require("../../../../userroles/recipe")); -const getAllRoles = async (_, __, ___, ____) => { +const getAllRoles = async (_, __, options, ____) => { try { recipe_1.default.getInstanceOrThrowError(); } catch (_) { @@ -15,10 +16,38 @@ const getAllRoles = async (_, __, ___, ____) => { status: "FEATURE_NOT_ENABLED_ERROR", }; } + const limit = Number(options.req.getKeyValueFromQuery("limit")); + let page = Number(options.req.getKeyValueFromQuery("page")); + if (limit === undefined || isNaN(limit) === false) { + throw new error_1.default({ + message: "Missing required parameter 'limit'", + type: error_1.default.BAD_INPUT_ERROR, + }); + } + if (page === undefined || isNaN(page) === false) { + throw new error_1.default({ + message: "Missing required parameter 'page'", + type: error_1.default.BAD_INPUT_ERROR, + }); + } + //set default page number to 1 if the page number is negitive or zero + if (page <= 0) { + page = 1; + } + const skip = limit * (page - 1); const response = await userroles_1.default.getAllRoles(); + const totalPages = Math.ceil(response.roles.length / limit); + if (page > totalPages) { + return { + roles: [], + totalPages, + status: "OK", + }; + } + const paginatedRoles = response.roles.slice(skip, skip + limit); let roles = []; - for (let i = 0; i < response.roles.length; i++) { - const role = response.roles[i]; + for (let i = 0; i < paginatedRoles.length; i++) { + const role = paginatedRoles[i]; try { const res = await userroles_1.default.getPermissionsForRole(role); if (res.status === "OK") { @@ -26,11 +55,17 @@ const getAllRoles = async (_, __, ___, ____) => { role, permissions: res.permissions, }); + } else { + roles.push({ + role, + permissions: [], + }); } } catch (_) {} } return { roles, + totalPages, status: "OK", }; }; diff --git a/lib/build/recipe/dashboard/recipe.js b/lib/build/recipe/dashboard/recipe.js index e2693e629..237d76887 100644 --- a/lib/build/recipe/dashboard/recipe.js +++ b/lib/build/recipe/dashboard/recipe.js @@ -50,14 +50,13 @@ const analytics_1 = __importDefault(require("./api/analytics")); const listTenants_1 = __importDefault(require("./api/listTenants")); const userUnlinkGet_1 = require("./api/userdetails/userUnlinkGet"); const getAllRoles_1 = __importDefault(require("./api/userroles/roles/getAllRoles")); -const createRole_1 = __importDefault(require("./api/userroles/roles/createRole")); const deleteRole_1 = __importDefault(require("./api/userroles/roles/deleteRole")); -const addPermissions_1 = __importDefault(require("./api/userroles/permissions/addPermissions")); const removePermissions_1 = __importDefault(require("./api/userroles/permissions/removePermissions")); const getPermissionsForRole_1 = __importDefault(require("./api/userroles/permissions/getPermissionsForRole")); const addRoleToUser_1 = __importDefault(require("./api/userroles/addRoleToUser")); const getRolesForUser_1 = __importDefault(require("./api/userroles/getRolesForUser")); const removeUserRole_1 = __importDefault(require("./api/userroles/removeUserRole")); +const createRoleOrAddPermissions_1 = __importDefault(require("./api/userroles/roles/createRoleOrAddPermissions")); class Recipe extends recipeModule_1.default { constructor(recipeId, appInfo, isInServerlessEnv, config) { super(recipeId, appInfo); @@ -394,8 +393,8 @@ class Recipe extends recipeModule_1.default { } else if (id === constants_1.USERROLES_LIST_API) { apiFunction = getAllRoles_1.default; } else if (id === constants_1.USERROLES_ROLE_API) { - if (req.getMethod() === "post") { - apiFunction = createRole_1.default; + if (req.getMethod() === "put") { + apiFunction = createRoleOrAddPermissions_1.default; } if (req.getMethod() === "delete") { apiFunction = deleteRole_1.default; @@ -404,9 +403,6 @@ class Recipe extends recipeModule_1.default { if (req.getMethod() === "get") { apiFunction = getPermissionsForRole_1.default; } - if (req.getMethod() === "put") { - apiFunction = addPermissions_1.default; - } } else if (id === constants_1.USERROLES_REMOVE_PERMISSIONS_API) { apiFunction = removePermissions_1.default; } else if (id === constants_1.USERROLES_USER_API) { diff --git a/lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts b/lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts index 6311ce5be..5c732e95f 100644 --- a/lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts +++ b/lib/ts/recipe/dashboard/api/userroles/addRoleToUser.ts @@ -9,9 +9,15 @@ const addRoleToUser = async ( tenantId: string, options: APIOptions, __: any -): Promise<{ - status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; -}> => { +): Promise< + | { + status: "OK"; + didUserAlreadyHaveRole: boolean; + } + | { + status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; + } +> => { try { UserRolesRecipe.getInstanceOrThrowError(); } catch (_) { diff --git a/lib/ts/recipe/dashboard/api/userroles/permissions/addPermissions.ts b/lib/ts/recipe/dashboard/api/userroles/permissions/addPermissions.ts deleted file mode 100644 index 860d7eb5b..000000000 --- a/lib/ts/recipe/dashboard/api/userroles/permissions/addPermissions.ts +++ /dev/null @@ -1,50 +0,0 @@ -import UserRolesRecipe from "../../../../userroles/recipe"; -import UserRoles from "../../../../userroles"; -import { APIInterface, APIOptions } from "../../../types"; - -import STError from "../../../../../error"; - -const addPermissions = async ( - _: APIInterface, - ___: string, - options: APIOptions, - __: any -): Promise< - | { - status: "OK"; - createdNewRole: boolean; - } - | { status: "FEATURE_NOT_ENABLED_ERROR" } -> => { - try { - UserRolesRecipe.getInstanceOrThrowError(); - } catch (_) { - return { - status: "FEATURE_NOT_ENABLED_ERROR", - }; - } - - const requestBody = await options.req.getJSONBody(); - const role = requestBody.role; - const permissions = requestBody.permissions; - - if (role === undefined || typeof role !== "string") { - throw new STError({ - message: "Required parameter 'role' is missing or has an invalid type", - type: STError.BAD_INPUT_ERROR, - }); - } - - if (permissions === undefined || Array.isArray(permissions) === false) { - throw new STError({ - message: "Required parameter 'permissions' is missing or has an invalid type", - type: STError.BAD_INPUT_ERROR, - }); - } - - const response = await UserRoles.createNewRoleOrAddPermissions(role, permissions); - - return response; -}; - -export default addPermissions; diff --git a/lib/ts/recipe/dashboard/api/userroles/removeUserRole.ts b/lib/ts/recipe/dashboard/api/userroles/removeUserRole.ts index 98c6f8737..5015b7acf 100644 --- a/lib/ts/recipe/dashboard/api/userroles/removeUserRole.ts +++ b/lib/ts/recipe/dashboard/api/userroles/removeUserRole.ts @@ -9,9 +9,15 @@ const removeUserRole = async ( tenantId: string, options: APIOptions, __: any -): Promise<{ - status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; -}> => { +): Promise< + | { + status: "OK"; + didUserHaveRole: boolean; + } + | { + status: "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR"; + } +> => { try { UserRolesRecipe.getInstanceOrThrowError(); } catch (_) { diff --git a/lib/ts/recipe/dashboard/api/userroles/roles/createRole.ts b/lib/ts/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.ts similarity index 86% rename from lib/ts/recipe/dashboard/api/userroles/roles/createRole.ts rename to lib/ts/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.ts index b4212f27f..7e7f8cdce 100644 --- a/lib/ts/recipe/dashboard/api/userroles/roles/createRole.ts +++ b/lib/ts/recipe/dashboard/api/userroles/roles/createRoleOrAddPermissions.ts @@ -4,14 +4,12 @@ import UserRoles from "../../../../userroles"; import STError from "../../../../../error"; -const createRole = async ( +const createRoleOrAddPermissions = async ( _: APIInterface, __: string, options: APIOptions, ___: any -): Promise<{ - status: "OK" | "ROLE_ALREADY_EXITS" | "FEATURE_NOT_ENABLED_ERROR"; -}> => { +): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" }> => { try { UserRolesRecipe.getInstanceOrThrowError(); } catch (_) { @@ -43,4 +41,4 @@ const createRole = async ( return response; }; -export default createRole; +export default createRoleOrAddPermissions; diff --git a/lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts b/lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts index c54190f1e..28c06d073 100644 --- a/lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts +++ b/lib/ts/recipe/dashboard/api/userroles/roles/deleteRole.ts @@ -9,9 +9,15 @@ const deleteRole = async ( ___: string, options: APIOptions, __: any -): Promise<{ - status: "OK" | "ROLE_DO_NOT_EXISTS" | "FEATURE_NOT_ENABLED_ERROR"; -}> => { +): Promise< + | { + status: "OK"; + didRoleExist: boolean; + } + | { + status: "FEATURE_NOT_ENABLED_ERROR"; + } +> => { try { UserRolesRecipe.getInstanceOrThrowError(); } catch (_) { diff --git a/lib/ts/recipe/dashboard/api/userroles/roles/getAllRoles.ts b/lib/ts/recipe/dashboard/api/userroles/roles/getAllRoles.ts index 16145555d..a3f6ba021 100644 --- a/lib/ts/recipe/dashboard/api/userroles/roles/getAllRoles.ts +++ b/lib/ts/recipe/dashboard/api/userroles/roles/getAllRoles.ts @@ -1,4 +1,5 @@ import UserRoles from "../../../../userroles"; +import STError from "../../../../../error"; import UserRolesRecipe from "../../../../userroles/recipe"; import { APIFunction, APIInterface, APIOptions } from "../../../types"; @@ -8,13 +9,19 @@ type Roles = { role: string; permissions: string[] }[]; type Response = | { status: "OK"; + totalPages: number; roles: Roles; } | { status: "FEATURE_NOT_ENABLED_ERROR"; }; -const getAllRoles: APIFunction = async (_: APIInterface, __: string, ___: APIOptions, ____: any): Promise => { +const getAllRoles: APIFunction = async ( + _: APIInterface, + __: string, + options: APIOptions, + ____: any +): Promise => { try { UserRolesRecipe.getInstanceOrThrowError(); } catch (_) { @@ -22,13 +29,47 @@ const getAllRoles: APIFunction = async (_: APIInterface, __: string, ___: APIOpt status: "FEATURE_NOT_ENABLED_ERROR", }; } + const limit = Number(options.req.getKeyValueFromQuery("limit")); + let page = Number(options.req.getKeyValueFromQuery("page")); + + if (limit === undefined || isNaN(limit) === false) { + throw new STError({ + message: "Missing required parameter 'limit'", + type: STError.BAD_INPUT_ERROR, + }); + } + + if (page === undefined || isNaN(page) === false) { + throw new STError({ + message: "Missing required parameter 'page'", + type: STError.BAD_INPUT_ERROR, + }); + } + + //set default page number to 1 if the page number is negitive or zero + if (page <= 0) { + page = 1; + } + + const skip = limit * (page - 1); const response = await UserRoles.getAllRoles(); + const totalPages = Math.ceil(response.roles.length / limit); + + if (page > totalPages) { + return { + roles: [], + totalPages, + status: "OK", + }; + } + const paginatedRoles = response.roles.slice(skip, skip + limit); + let roles: Roles = []; - for (let i = 0; i < response.roles.length; i++) { - const role = response.roles[i]; + for (let i = 0; i < paginatedRoles.length; i++) { + const role = paginatedRoles[i]; try { const res = await UserRoles.getPermissionsForRole(role); @@ -37,12 +78,18 @@ const getAllRoles: APIFunction = async (_: APIInterface, __: string, ___: APIOpt role, permissions: res.permissions, }); + } else { + roles.push({ + role, + permissions: [], + }); } } catch (_) {} } return { roles, + totalPages, status: "OK", }; }; diff --git a/lib/ts/recipe/dashboard/recipe.ts b/lib/ts/recipe/dashboard/recipe.ts index b0f5467d7..d1d67c1d5 100644 --- a/lib/ts/recipe/dashboard/recipe.ts +++ b/lib/ts/recipe/dashboard/recipe.ts @@ -69,14 +69,13 @@ import analyticsPost from "./api/analytics"; import listTenants from "./api/listTenants"; import { userUnlink } from "./api/userdetails/userUnlinkGet"; import getAllRoles from "./api/userroles/roles/getAllRoles"; -import createRole from "./api/userroles/roles/createRole"; import deleteRole from "./api/userroles/roles/deleteRole"; -import addPermissions from "./api/userroles/permissions/addPermissions"; import removePermissionsFromRole from "./api/userroles/permissions/removePermissions"; import getPermissionsForRole from "./api/userroles/permissions/getPermissionsForRole"; import addRoleToUser from "./api/userroles/addRoleToUser"; import getRolesForUser from "./api/userroles/getRolesForUser"; import removeUserRole from "./api/userroles/removeUserRole"; +import createRoleOrAddPermissions from "./api/userroles/roles/createRoleOrAddPermissions"; export default class Recipe extends RecipeModule { private static instance: Recipe | undefined = undefined; @@ -424,8 +423,8 @@ export default class Recipe extends RecipeModule { } else if (id === USERROLES_LIST_API) { apiFunction = getAllRoles; } else if (id === USERROLES_ROLE_API) { - if (req.getMethod() === "post") { - apiFunction = createRole; + if (req.getMethod() === "put") { + apiFunction = createRoleOrAddPermissions; } if (req.getMethod() === "delete") { @@ -435,10 +434,6 @@ export default class Recipe extends RecipeModule { if (req.getMethod() === "get") { apiFunction = getPermissionsForRole; } - - if (req.getMethod() === "put") { - apiFunction = addPermissions; - } } else if (id === USERROLES_REMOVE_PERMISSIONS_API) { apiFunction = removePermissionsFromRole; } else if (id === USERROLES_USER_API) {