From 5d870fb41a2dd1920419d37ea6259451a2cdb655 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 1 Sep 2023 09:50:55 +0200 Subject: [PATCH] Rename --- .../server/src/api/controllers/permission.ts | 8 +++++ .../server/src/sdk/app/permissions/index.ts | 31 ++++++++++--------- packages/types/src/api/web/app/permission.ts | 1 + 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/server/src/api/controllers/permission.ts b/packages/server/src/api/controllers/permission.ts index 8cc5bcec74..6702c24450 100644 --- a/packages/server/src/api/controllers/permission.ts +++ b/packages/server/src/api/controllers/permission.ts @@ -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 ), + inheritablePermissions: + inheritablePermissions && + Object.entries(inheritablePermissions).reduce((p, [level, role]) => { + p[level] = role.role + return p + }, {} as Record), } } diff --git a/packages/server/src/sdk/app/permissions/index.ts b/packages/server/src/sdk/app/permissions/index.ts index b1ebbd2ac8..ce6ac8c98a 100644 --- a/packages/server/src/sdk/app/permissions/index.ts +++ b/packages/server/src/sdk/app/permissions/index.ts @@ -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 { + if (isViewID(resourceId) && (await features.isViewPermissionEnabled())) { + return await getResourcePerms(extractViewInfoFromID(resourceId).tableId) + } +} + export async function getResourcePerms( resourceId: string ): Promise { @@ -69,12 +77,7 @@ export async function getResourcePerms( const rolesList = body.rows.map(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((p, [level, role]) => { - p[level] = { role, type: PermissionType.BASE } + p[level] = { role, type: PermissionSource.BASE } return p }, {}) const result = Object.assign(basePermissions, permissions) diff --git a/packages/types/src/api/web/app/permission.ts b/packages/types/src/api/web/app/permission.ts index 52f6bdcabb..73ad47d83f 100644 --- a/packages/types/src/api/web/app/permission.ts +++ b/packages/types/src/api/web/app/permission.ts @@ -1,4 +1,5 @@ export interface GetResourcePermsResponse { permissions: Record permissionType: Record + inheritablePermissions?: Record }