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

View File

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