Make new tables require ADMIN permissions to read and write.

This commit is contained in:
Sam Rose 2024-10-04 16:12:01 +01:00 committed by Adria Navarro
parent 3814eeb475
commit 3405e6d6b7
3 changed files with 48 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { context, docIds, events } from "@budibase/backend-core" import { context, docIds, events, roles } from "@budibase/backend-core"
import { import {
PROTECTED_EXTERNAL_COLUMNS, PROTECTED_EXTERNAL_COLUMNS,
PROTECTED_INTERNAL_COLUMNS, PROTECTED_INTERNAL_COLUMNS,
@ -189,6 +189,13 @@ describe.each([
) )
} }
) )
it("should create tables with ADMIN read and write permissions", async () => {
const table = await config.api.table.save(tableForDatasource(datasource))
const { permissions } = await config.api.permission.get(table._id!)
expect(permissions.read.role).toEqual(roles.BUILTIN_ROLE_IDS.ADMIN)
expect(permissions.write.role).toEqual(roles.BUILTIN_ROLE_IDS.ADMIN)
})
}) })
describe("update", () => { describe("update", () => {

View File

@ -1,5 +1,6 @@
import { permissions, roles } from "@budibase/backend-core" import { permissions, roles } from "@budibase/backend-core"
import { DocumentType, VirtualDocumentType } from "../db/utils" import { DocumentType, VirtualDocumentType } from "../db/utils"
import { getDocumentType, getVirtualDocumentType } from "@budibase/types"
export const CURRENTLY_SUPPORTED_LEVELS: string[] = [ export const CURRENTLY_SUPPORTED_LEVELS: string[] = [
permissions.PermissionLevel.WRITE, permissions.PermissionLevel.WRITE,
@ -8,14 +9,16 @@ export const CURRENTLY_SUPPORTED_LEVELS: string[] = [
] ]
export function getPermissionType(resourceId: string) { export function getPermissionType(resourceId: string) {
const docType = Object.values(DocumentType).filter(docType => const virtualDocType = getVirtualDocumentType(resourceId)
resourceId.startsWith(docType) switch (virtualDocType) {
)[0]
switch (docType as DocumentType | VirtualDocumentType) {
case DocumentType.TABLE:
case DocumentType.ROW:
case VirtualDocumentType.VIEW: case VirtualDocumentType.VIEW:
return permissions.PermissionType.TABLE return permissions.PermissionType.TABLE
}
const docType = getDocumentType(resourceId)
switch (docType) {
case DocumentType.TABLE:
case DocumentType.ROW:
case DocumentType.AUTOMATION: case DocumentType.AUTOMATION:
return permissions.PermissionType.AUTOMATION return permissions.PermissionType.AUTOMATION
case DocumentType.WEBHOOK: case DocumentType.WEBHOOK:
@ -39,15 +42,18 @@ export function getBasePermissions(resourceId: string) {
if (!role.permissionId) { if (!role.permissionId) {
continue continue
} }
const perms = permissions.getBuiltinPermissionByID(role.permissionId) const perms = permissions.getBuiltinPermissionByID(role.permissionId)
if (!perms) { if (!perms) {
continue continue
} }
const typedPermission = perms.permissions.find(perm => perm.type === type) const typedPermission = perms.permissions.find(perm => perm.type === type)
if ( if (!typedPermission) {
typedPermission && continue
CURRENTLY_SUPPORTED_LEVELS.indexOf(typedPermission.level) !== -1 }
) {
if (CURRENTLY_SUPPORTED_LEVELS.includes(typedPermission.level)) {
const level = typedPermission.level const level = typedPermission.level
basePermissions[level] = roles.lowerBuiltinRoleID( basePermissions[level] = roles.lowerBuiltinRoleID(
basePermissions[level], basePermissions[level],

View File

@ -42,6 +42,17 @@ export enum DocumentType {
ROW_ACTIONS = "ra", ROW_ACTIONS = "ra",
} }
// Because DocumentTypes can overlap, we need to make sure that we search
// longest first to ensure we get the correct type.
const sortedDocumentTypes = Object.values(DocumentType).sort(
(a, b) => b.length - a.length // descending
)
export function getDocumentType(id: string): DocumentType | undefined {
return sortedDocumentTypes.find(docType =>
id.startsWith(`${docType}${SEPARATOR}`)
)
}
// these are the core documents that make up the data, design // these are the core documents that make up the data, design
// and automation sections of an app. This excludes any internal // and automation sections of an app. This excludes any internal
// rows as we shouldn't import data. // rows as we shouldn't import data.
@ -72,6 +83,19 @@ export enum VirtualDocumentType {
ROW_ACTION = "row_action", ROW_ACTION = "row_action",
} }
// Because VirtualDocumentTypes can overlap, we need to make sure that we search
// longest first to ensure we get the correct type.
const sortedVirtualDocumentTypes = Object.values(VirtualDocumentType).sort(
(a, b) => b.length - a.length // descending
)
export function getVirtualDocumentType(
id: string
): VirtualDocumentType | undefined {
return sortedVirtualDocumentTypes.find(docType =>
id.startsWith(`${docType}${SEPARATOR}`)
)
}
export interface Document { export interface Document {
_id?: string _id?: string
_rev?: string _rev?: string