Fixing build issue,.

This commit is contained in:
mike12345567 2023-09-27 16:24:12 +01:00
parent 4bfda10fbb
commit 395969e0f0
4 changed files with 22 additions and 24 deletions

View File

@ -215,21 +215,23 @@ async function getAllUserRoles(userRoleId?: string): Promise<RoleDoc[]> {
return roles
}
export async function getUserRoleIdHierarchy(
userRoleId?: string
): Promise<string[]> {
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<string[]|object[]>} returns an ordered array of the roles, with the first being their
* @returns {Promise<object[]>} 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<object[]>} An array of the role objects that were found.
*/
export async function getAllRoles(
appId?: string,
opts?: { idOnly: boolean }
): Promise<RoleDoc[]> {
export async function getAllRoles(appId?: string): Promise<RoleDoc[]> {
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
}

View File

@ -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!)
}
}

View File

@ -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

View File

@ -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) {