From 395969e0f06233dc0b9c6c685107c6f2134b20d1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 27 Sep 2023 16:24:12 +0100 Subject: [PATCH] Fixing build issue,. --- packages/backend-core/src/security/roles.ts | 34 ++++++++++--------- packages/server/src/api/controllers/role.ts | 4 +-- .../server/src/api/controllers/routing.ts | 4 +-- packages/server/src/middleware/authorized.ts | 4 +-- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/backend-core/src/security/roles.ts b/packages/backend-core/src/security/roles.ts index 3497ecc972..24279e6b5c 100644 --- a/packages/backend-core/src/security/roles.ts +++ b/packages/backend-core/src/security/roles.ts @@ -215,21 +215,23 @@ async function getAllUserRoles(userRoleId?: string): Promise { return roles } +export async function getUserRoleIdHierarchy( + userRoleId?: string +): Promise { + const roles = await getUserRoleHierarchy(userRoleId) + return roles.map(role => role._id!) +} + /** * Returns an ordered array of the user's inherited role IDs, this can be used * to determine if a user can access something that requires a specific role. * @param {string} userRoleId The user's role ID, this can be found in their access token. - * @param {object} opts Various options, such as whether to only retrieve the IDs (default true). - * @returns {Promise} returns an ordered array of the roles, with the first being their + * @returns {Promise} returns an ordered array of the roles, with the first being their * highest level of access and the last being the lowest level. */ -export async function getUserRoleHierarchy( - userRoleId?: string, - opts = { idOnly: true } -) { +export async function getUserRoleHierarchy(userRoleId?: string) { // special case, if they don't have a role then they are a public user - const roles = await getAllUserRoles(userRoleId) - return opts.idOnly ? roles.map(role => role._id) : roles + return getAllUserRoles(userRoleId) } // this function checks that the provided permissions are in an array format @@ -249,14 +251,16 @@ export function checkForRoleResourceArray( return rolePerms } +export async function getAllRoleIds(appId?: string) { + const roles = await getAllRoles(appId) + return roles.map(role => role._id) +} + /** * Given an app ID this will retrieve all of the roles that are currently within that app. * @return {Promise} An array of the role objects that were found. */ -export async function getAllRoles( - appId?: string, - opts?: { idOnly: boolean } -): Promise { +export async function getAllRoles(appId?: string): Promise { if (appId) { return doWithDB(appId, internal) } else { @@ -311,7 +315,7 @@ export async function getAllRoles( ) } } - return opts?.idOnly ? roles.map(role => role._id) : roles + return roles } } @@ -335,9 +339,7 @@ export class AccessController { } let roleIds = userRoleId ? this.userHierarchies[userRoleId] : null if (!roleIds && userRoleId) { - roleIds = (await getUserRoleHierarchy(userRoleId, { - idOnly: true, - })) as string[] + roleIds = await getUserRoleIdHierarchy(userRoleId) this.userHierarchies[userRoleId] = roleIds } diff --git a/packages/server/src/api/controllers/role.ts b/packages/server/src/api/controllers/role.ts index e72517afc3..ed23009706 100644 --- a/packages/server/src/api/controllers/role.ts +++ b/packages/server/src/api/controllers/role.ts @@ -139,8 +139,8 @@ export async function accessible(ctx: UserCtx) { } if (ctx.user && sharedSdk.users.isAdminOrBuilder(ctx.user)) { const appId = context.getAppId() - ctx.body = await roles.getAllRoles(appId, { idOnly: true }) + ctx.body = await roles.getAllRoleIds(appId) } else { - ctx.body = await roles.getUserRoleHierarchy(roleId!, { idOnly: true }) + ctx.body = await roles.getUserRoleIdHierarchy(roleId!) } } diff --git a/packages/server/src/api/controllers/routing.ts b/packages/server/src/api/controllers/routing.ts index 1bfd289637..f356a1cd59 100644 --- a/packages/server/src/api/controllers/routing.ts +++ b/packages/server/src/api/controllers/routing.ts @@ -63,9 +63,7 @@ export async function fetch(ctx: UserCtx) { export async function clientFetch(ctx: UserCtx) { const routing = await getRoutingStructure() let roleId = ctx.user?.role?._id - const roleIds = (await roles.getUserRoleHierarchy(roleId, { - idOnly: true, - })) as string[] + const roleIds = await roles.getUserRoleIdHierarchy(roleId) for (let topLevel of Object.values(routing.routes) as any) { for (let subpathKey of Object.keys(topLevel.subpaths)) { let found = false diff --git a/packages/server/src/middleware/authorized.ts b/packages/server/src/middleware/authorized.ts index b2ffeacaf8..8d148b04bf 100644 --- a/packages/server/src/middleware/authorized.ts +++ b/packages/server/src/middleware/authorized.ts @@ -55,9 +55,7 @@ const checkAuthorizedResource = async ( ) => { // get the user's roles const roleId = ctx.roleId || roles.BUILTIN_ROLE_IDS.PUBLIC - const userRoles = (await roles.getUserRoleHierarchy(roleId, { - idOnly: false, - })) as Role[] + const userRoles = await roles.getUserRoleIdHierarchy(roleId) const permError = "User does not have permission" // check if the user has the required role if (resourceRoles.length > 0) {