Allow view permission type

This commit is contained in:
Adria Navarro 2023-08-24 10:22:08 +02:00
parent 972cc9916b
commit bfa2b491f3
3 changed files with 32 additions and 16 deletions

View File

@ -5,7 +5,7 @@ import { paramResource, paramSubResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
import { internalSearchValidator } from "./utils/validators" import { internalSearchValidator } from "./utils/validators"
import trimViewRowInfo from "../../middleware/trimViewRowInfo" import trimViewRowInfo from "../../middleware/trimViewRowInfo"
import { extractViewInfoFromID } from "../../db/utils"
const { PermissionType, PermissionLevel } = permissions const { PermissionType, PermissionLevel } = permissions
const router: Router = new Router() const router: Router = new Router()
@ -270,12 +270,7 @@ router
router.post( router.post(
"/api/v2/views/:viewId/search", "/api/v2/views/:viewId/search",
authorizedResource( authorizedResource(PermissionType.VIEW, PermissionLevel.READ, "viewId"),
PermissionType.TABLE,
PermissionLevel.READ,
"viewId",
val => extractViewInfoFromID(val).tableId
),
rowController.views.searchView rowController.views.searchView
) )

View File

@ -6,9 +6,11 @@ import {
users, users,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import { PermissionLevel, PermissionType, Role, UserCtx } from "@budibase/types" import { PermissionLevel, PermissionType, Role, UserCtx } from "@budibase/types"
import { features } from "@budibase/pro"
import builderMiddleware from "./builder" import builderMiddleware from "./builder"
import { isWebhookEndpoint } from "./utils" import { isWebhookEndpoint } from "./utils"
import { paramResource } from "./resourceId" import { paramResource } from "./resourceId"
import { extractViewInfoFromID, isViewID } from "../db/utils"
function hasResource(ctx: any) { function hasResource(ctx: any) {
return ctx.resourceId != null return ctx.resourceId != null
@ -75,12 +77,31 @@ const checkAuthorizedResource = async (
} }
} }
const resourceIdTranformers: Partial<
Record<PermissionType, (ctx: UserCtx) => Promise<void>>
> = {
[PermissionType.VIEW]: async ctx => {
const { resourceId } = ctx
if (!isViewID(resourceId)) {
ctx.throw(400, `"${resourceId}" is not a valid viewId`)
}
if (await features.isViewPermissionEnabled()) {
ctx.subResourceId = ctx.resourceId
ctx.resourceId = extractViewInfoFromID(resourceId).tableId
} else {
ctx.resourceId = extractViewInfoFromID(resourceId).tableId
delete ctx.subResourceId
}
},
}
const authorized = const authorized =
( (
permType: PermissionType, permType: PermissionType,
permLevel?: PermissionLevel, permLevel?: PermissionLevel,
opts = { schema: false }, opts = { schema: false },
resourceId?: { path: string; transformer?: (val: string) => string } resourcePath?: string
) => ) =>
async (ctx: any, next: any) => { async (ctx: any, next: any) => {
// webhooks don't need authentication, each webhook unique // webhooks don't need authentication, each webhook unique
@ -102,15 +123,15 @@ const authorized =
: PermissionLevel.READ : PermissionLevel.READ
const appId = context.getAppId() const appId = context.getAppId()
if (resourceId?.path) { if (resourcePath) {
// Reusing the existing middleware to extract the value // Reusing the existing middleware to extract the value
paramResource(resourceId.path)(ctx, () => {}) paramResource(resourcePath)(ctx, () => {})
if (resourceId.transformer) {
ctx.resourceId = resourceId.transformer(ctx.resourceId)
}
} }
if (appId && hasResource(ctx)) { if (appId && hasResource(ctx)) {
if (resourceIdTranformers[permType]) {
await resourceIdTranformers[permType]!(ctx)
}
resourceRoles = await roles.getRequiredResourceRole(permLevel!, ctx) resourceRoles = await roles.getRequiredResourceRole(permLevel!, ctx)
if (opts && opts.schema) { if (opts && opts.schema) {
otherLevelRoles = await roles.getRequiredResourceRole(otherLevel, ctx) otherLevelRoles = await roles.getRequiredResourceRole(otherLevel, ctx)
@ -165,8 +186,7 @@ export default (
export const authorizedResource = ( export const authorizedResource = (
permType: PermissionType, permType: PermissionType,
permLevel: PermissionLevel, permLevel: PermissionLevel,
path: string, resourcePath: string
transformer?: (val: string) => string
) => { ) => {
return authorized(permType, permLevel, undefined, { path, transformer }) return authorized(permType, permLevel, undefined, resourcePath)
} }

View File

@ -15,4 +15,5 @@ export enum PermissionType {
BUILDER = "builder", BUILDER = "builder",
GLOBAL_BUILDER = "globalBuilder", GLOBAL_BUILDER = "globalBuilder",
QUERY = "query", QUERY = "query",
VIEW = "view",
} }