diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index 78a940c18f..579f7e5f78 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -1,6 +1,7 @@ import { CreateRowActionRequest, Ctx, + RowActionPermissions, RowActionResponse, RowActionsResponse, UpdateRowActionRequest, @@ -18,25 +19,26 @@ async function getTable(ctx: Ctx) { export async function find(ctx: Ctx) { const table = await getTable(ctx) + const tableId = table._id! - if (!(await sdk.rowActions.docExists(table._id!))) { + if (!(await sdk.rowActions.docExists(tableId))) { ctx.body = { actions: {}, } return } - const { actions } = await sdk.rowActions.getAll(table._id!) + const { actions } = await sdk.rowActions.getAll(tableId) const result: RowActionsResponse = { actions: Object.entries(actions).reduce>( (acc, [key, action]) => ({ ...acc, [key]: { id: key, - tableId: table._id!, + tableId, name: action.name, automationId: action.automationId, - allowedSources: flattenAllowedViews(action.permissions.views), + allowedSources: flattenAllowedSources(tableId, action.permissions), }, }), {} @@ -49,13 +51,14 @@ export async function create( ctx: Ctx ) { const table = await getTable(ctx) + const tableId = table._id! - const createdAction = await sdk.rowActions.create(table._id!, { + const createdAction = await sdk.rowActions.create(tableId, { name: ctx.request.body.name, }) ctx.body = { - tableId: table._id!, + tableId, id: createdAction.id, name: createdAction.name, automationId: createdAction.automationId, @@ -68,14 +71,15 @@ export async function update( ctx: Ctx ) { const table = await getTable(ctx) + const tableId = table._id! const { actionId } = ctx.params - const action = await sdk.rowActions.update(table._id!, actionId, { + const action = await sdk.rowActions.update(tableId, actionId, { name: ctx.request.body.name, }) ctx.body = { - tableId: table._id!, + tableId, id: action.id, name: action.name, automationId: action.automationId, @@ -93,47 +97,56 @@ export async function remove(ctx: Ctx) { export async function setViewPermission(ctx: Ctx) { const table = await getTable(ctx) + const tableId = table._id! const { actionId, viewId } = ctx.params const action = await sdk.rowActions.setViewPermission( - table._id!, + tableId, actionId, viewId ) ctx.body = { - tableId: table._id!, + tableId, id: action.id, name: action.name, automationId: action.automationId, - allowedSources: flattenAllowedViews(action.permissions.views), + allowedSources: flattenAllowedSources(tableId, action.permissions), } } export async function unsetViewPermission(ctx: Ctx) { const table = await getTable(ctx) + const tableId = table._id! const { actionId, viewId } = ctx.params const action = await sdk.rowActions.unsetViewPermission( - table._id!, + tableId, actionId, viewId ) ctx.body = { - tableId: table._id!, + tableId, id: action.id, name: action.name, automationId: action.automationId, - allowedSources: flattenAllowedViews(action.permissions.views), + allowedSources: flattenAllowedSources(tableId, action.permissions), } } -function flattenAllowedViews( - permissions: Record +function flattenAllowedSources( + tableId: string, + permissions: RowActionPermissions ) { - const allowedPermissions = Object.entries(permissions || {}) - .filter(([_, p]) => p.runAllowed) - .map(([viewId]) => viewId) + const allowedPermissions = [] + if (permissions.table.runAllowed) { + allowedPermissions.push(tableId) + } + allowedPermissions.push( + ...Object.keys(permissions.views || {}).filter( + viewId => permissions.views[viewId].runAllowed + ) + ) if (!allowedPermissions.length) { return undefined } diff --git a/packages/types/src/documents/app/rowAction.ts b/packages/types/src/documents/app/rowAction.ts index fc9a25c2e2..071b480b1c 100644 --- a/packages/types/src/documents/app/rowAction.ts +++ b/packages/types/src/documents/app/rowAction.ts @@ -8,8 +8,10 @@ export interface TableRowActions extends Document { export interface RowActionData { name: string automationId: string - permissions: { - table: { runAllowed: boolean } - views: Record - } + permissions: RowActionPermissions +} + +export interface RowActionPermissions { + table: { runAllowed: boolean } + views: Record }