This commit is contained in:
Adria Navarro 2023-09-01 09:50:55 +02:00
parent 774ff745b6
commit 5d870fb41a
3 changed files with 26 additions and 14 deletions

View File

@ -156,6 +156,8 @@ export async function getResourcePerms(
) {
const resourceId = ctx.params.resourceId
const resourcePermissions = await sdk.permissions.getResourcePerms(resourceId)
const inheritablePermissions =
await sdk.permissions.getInheritablePermissions(resourceId)
ctx.body = {
permissions: Object.entries(resourcePermissions).reduce(
@ -172,6 +174,12 @@ export async function getResourcePerms(
},
{} as Record<string, string>
),
inheritablePermissions:
inheritablePermissions &&
Object.entries(inheritablePermissions).reduce((p, [level, role]) => {
p[level] = role.role
return p
}, {} as Record<string, string>),
}
}

View File

@ -46,17 +46,25 @@ export async function resourceActionAllowed({
}
}
enum PermissionType {
EXPLICIT = "explicit",
INHERITED = "inherited",
BASE = "base",
enum PermissionSource {
EXPLICIT = "EXPLICIT",
INHERITED = "INHERITED",
BASE = "BASE",
}
type ResourcePermissions = Record<
string,
{ role: string; type: PermissionType }
{ role: string; type: PermissionSource }
>
export async function getInheritablePermissions(
resourceId: string
): Promise<ResourcePermissions | undefined> {
if (isViewID(resourceId) && (await features.isViewPermissionEnabled())) {
return await getResourcePerms(extractViewInfoFromID(resourceId).tableId)
}
}
export async function getResourcePerms(
resourceId: string
): Promise<ResourcePermissions> {
@ -69,12 +77,7 @@ export async function getResourcePerms(
const rolesList = body.rows.map<Role>(row => row.doc)
let permissions: ResourcePermissions = {}
let permsToInherit: ResourcePermissions | undefined
if (isViewID(resourceId) && (await features.isViewPermissionEnabled())) {
permsToInherit = await getResourcePerms(
extractViewInfoFromID(resourceId).tableId
)
}
const permsToInherit = await getInheritablePermissions(resourceId)
for (let level of CURRENTLY_SUPPORTED_LEVELS) {
// update the various roleIds in the resource permissions
@ -86,12 +89,12 @@ export async function getResourcePerms(
if (rolePerms[resourceId]?.indexOf(level) > -1) {
permissions[level] = {
role: roles.getExternalRoleID(role._id!, role.version),
type: PermissionType.EXPLICIT,
type: PermissionSource.EXPLICIT,
}
} else if (permsToInherit && permsToInherit[level]) {
permissions[level] = {
role: permsToInherit[level].role,
type: PermissionType.INHERITED,
type: PermissionSource.INHERITED,
}
}
}
@ -100,7 +103,7 @@ export async function getResourcePerms(
const basePermissions = Object.entries(
getBasePermissions(resourceId)
).reduce<ResourcePermissions>((p, [level, role]) => {
p[level] = { role, type: PermissionType.BASE }
p[level] = { role, type: PermissionSource.BASE }
return p
}, {})
const result = Object.assign(basePermissions, permissions)

View File

@ -1,4 +1,5 @@
export interface GetResourcePermsResponse {
permissions: Record<string, string>
permissionType: Record<string, string>
inheritablePermissions?: Record<string, string>
}