This commit is contained in:
Adria Navarro 2024-10-08 17:19:18 +02:00
parent 56459b2736
commit ebd762cdb6
3 changed files with 9 additions and 7 deletions

View File

@ -42,7 +42,7 @@ export class Role implements RoleDoc {
_id: string _id: string
_rev?: string _rev?: string
name: string name: string
permissionId: string permissionId: BuiltinPermissionID
inherits?: string inherits?: string
version?: string version?: string
permissions: Record<string, PermissionLevel[]> = {} permissions: Record<string, PermissionLevel[]> = {}
@ -51,7 +51,7 @@ export class Role implements RoleDoc {
constructor( constructor(
id: string, id: string,
name: string, name: string,
permissionId: string, permissionId: BuiltinPermissionID,
uiMetadata?: RoleUIMetadata uiMetadata?: RoleUIMetadata
) { ) {
this._id = id this._id = id

View File

@ -33,7 +33,7 @@ export async function fetch(
) { ) {
const db = context.getAppDB() const db = context.getAppDB()
const dbRoles = await sdk.permissions.getAllDBRoles(db) const dbRoles = await sdk.permissions.getAllDBRoles(db)
let permissions: any = {} let permissions: Record<string, Record<string, string>> = {}
// create an object with structure role ID -> resource ID -> level // create an object with structure role ID -> resource ID -> level
for (let role of dbRoles) { for (let role of dbRoles) {
if (!role.permissions) { if (!role.permissions) {
@ -45,7 +45,7 @@ export async function fetch(
} }
for (let [resource, levelArr] of Object.entries(role.permissions)) { for (let [resource, levelArr] of Object.entries(role.permissions)) {
const levels: string[] = Array.isArray(levelArr) ? levelArr : [levelArr] const levels: string[] = Array.isArray(levelArr) ? levelArr : [levelArr]
const perms: Record<string, string> = {} const perms: Record<string, string> = permissions[resource] || {}
levels.forEach(level => (perms[level] = roleId!)) levels.forEach(level => (perms[level] = roleId!))
permissions[resource] = perms permissions[resource] = perms
} }

View File

@ -39,6 +39,8 @@ describe("/permission", () => {
}) })
describe("table permissions", () => { describe("table permissions", () => {
const DEFAULT_TABLE_ROLE_ID = BUILTIN_ROLE_IDS.ADMIN
let tableId: string let tableId: string
let row: Row let row: Row
let view: ViewV2 let view: ViewV2
@ -67,11 +69,11 @@ describe("/permission", () => {
expect(permissions).toEqual({ expect(permissions).toEqual({
read: { read: {
permissionType: "BASE", permissionType: "BASE",
role: "BASIC", role: DEFAULT_TABLE_ROLE_ID,
}, },
write: { write: {
permissionType: "BASE", permissionType: "BASE",
role: "BASIC", role: DEFAULT_TABLE_ROLE_ID,
}, },
}) })
}) })
@ -91,7 +93,7 @@ describe("/permission", () => {
expect(res.body).toEqual({ expect(res.body).toEqual({
permissions: { permissions: {
read: { permissionType: "EXPLICIT", role: STD_ROLE_ID }, read: { permissionType: "EXPLICIT", role: STD_ROLE_ID },
write: { permissionType: "BASE", role: HIGHER_ROLE_ID }, write: { permissionType: "BASE", role: DEFAULT_TABLE_ROLE_ID },
}, },
}) })
}) })