|
| 1 | +import { ForbiddenException, Inject, Injectable, Logger, Scope } from '@nestjs/common'; |
| 2 | +import AbstractUseCase from '../../../common/abstract-use.case.js'; |
| 3 | +import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; |
| 4 | +import { BaseType } from '../../../common/data-injection.tokens.js'; |
| 5 | +import { CedarAction } from '../../../entities/cedar-authorization/cedar-action-map.js'; |
| 6 | +import { CedarAuthorizationService } from '../../../entities/cedar-authorization/cedar-authorization.service.js'; |
| 7 | +import { IPublicTablePermission } from '../../../entities/cedar-authorization/cedar-policy-generator.js'; |
| 8 | +import { Messages } from '../../../exceptions/text/messages.js'; |
| 9 | +import { SetPublicPermissionsDs } from '../data-structures/agents.ds.js'; |
| 10 | +import { PublicPermissionsRO } from '../data-structures/agents-responses.ds.js'; |
| 11 | +import { ISetPublicPermissions } from './agents-use-cases.interface.js'; |
| 12 | + |
| 13 | +// Grants public (anonymous) read access on connection tables on behalf of the website-generation |
| 14 | +// agent (agents-core `set_public_read_permissions`, ADR sitenova/03 §6.4). agents-core has already |
| 15 | +// collected the explicit user approval; this side re-checks that the approving user actually holds |
| 16 | +// Cedar connection:edit, then MERGES the requested tables into the existing public policy (merge by |
| 17 | +// default — the agent can only ever ADD public tables, never remove an admin's; `replace` exists |
| 18 | +// for explicit resets). savePublicPermissions re-validates the generated policy and invalidates |
| 19 | +// the policy cache, so the grant is effective immediately. |
| 20 | + |
| 21 | +// Union two public-table sets: an all-columns grant (absent/empty readableColumns) on either side |
| 22 | +// wins for that table; otherwise the column whitelists are unioned. |
| 23 | +export function mergePublicTables( |
| 24 | + existing: Array<IPublicTablePermission>, |
| 25 | + requested: Array<IPublicTablePermission>, |
| 26 | +): Array<IPublicTablePermission> { |
| 27 | + const merged = new Map<string, IPublicTablePermission>(); |
| 28 | + for (const table of [...existing, ...requested]) { |
| 29 | + if (!table.tableName) continue; |
| 30 | + const previous = merged.get(table.tableName); |
| 31 | + if (!previous) { |
| 32 | + merged.set(table.tableName, { |
| 33 | + tableName: table.tableName, |
| 34 | + ...(table.readableColumns?.length ? { readableColumns: [...table.readableColumns] } : {}), |
| 35 | + }); |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (!previous.readableColumns?.length || !table.readableColumns?.length) { |
| 39 | + merged.set(table.tableName, { tableName: table.tableName }); |
| 40 | + continue; |
| 41 | + } |
| 42 | + merged.set(table.tableName, { |
| 43 | + tableName: table.tableName, |
| 44 | + readableColumns: [...new Set([...previous.readableColumns, ...table.readableColumns])], |
| 45 | + }); |
| 46 | + } |
| 47 | + return [...merged.values()]; |
| 48 | +} |
| 49 | + |
| 50 | +@Injectable({ scope: Scope.REQUEST }) |
| 51 | +export class SetPublicPermissionsUseCase |
| 52 | + extends AbstractUseCase<SetPublicPermissionsDs, PublicPermissionsRO> |
| 53 | + implements ISetPublicPermissions |
| 54 | +{ |
| 55 | + private readonly logger = new Logger(SetPublicPermissionsUseCase.name); |
| 56 | + |
| 57 | + constructor( |
| 58 | + @Inject(BaseType.GLOBAL_DB_CONTEXT) |
| 59 | + protected _dbContext: IGlobalDatabaseContext, |
| 60 | + private readonly cedarAuthService: CedarAuthorizationService, |
| 61 | + ) { |
| 62 | + super(); |
| 63 | + } |
| 64 | + |
| 65 | + protected async implementation(inputData: SetPublicPermissionsDs): Promise<PublicPermissionsRO> { |
| 66 | + const { userId, connectionId, tables, mode } = inputData; |
| 67 | + const requestedNames = tables.map((t) => t.tableName).join(', '); |
| 68 | + |
| 69 | + const allowed = await this.cedarAuthService.validate({ |
| 70 | + userId, |
| 71 | + action: CedarAction.ConnectionEdit, |
| 72 | + connectionId, |
| 73 | + }); |
| 74 | + if (!allowed) { |
| 75 | + // Audit trail: this is the most security-relevant refusal on the endpoint — the approving |
| 76 | + // user does not hold connection:edit, so the agent-collected approval is not honored. |
| 77 | + this.logger.warn( |
| 78 | + `Public-read grant REFUSED (no connection:edit): connection=${connectionId} user=${userId} requested=[${requestedNames}]`, |
| 79 | + ); |
| 80 | + throw new ForbiddenException(Messages.DONT_HAVE_PERMISSIONS); |
| 81 | + } |
| 82 | + |
| 83 | + let effective: Array<IPublicTablePermission> = tables; |
| 84 | + if (mode !== 'replace') { |
| 85 | + const existing = await this.cedarAuthService.getPublicPermissions(connectionId); |
| 86 | + effective = mergePublicTables(existing.tables, tables); |
| 87 | + } |
| 88 | + |
| 89 | + const saved = await this.cedarAuthService.savePublicPermissions(connectionId, effective); |
| 90 | + // Audit trail for every grant (this endpoint changes what ANONYMOUS visitors can read). |
| 91 | + this.logger.log( |
| 92 | + `Public-read grant: connection=${connectionId} user=${userId} mode=${mode} requested=[${requestedNames}] ` + |
| 93 | + `-> enabled=${saved.enabled} tables=[${saved.tables.map((t) => t.tableName).join(', ')}]`, |
| 94 | + ); |
| 95 | + return { enabled: saved.enabled, tables: saved.tables }; |
| 96 | + } |
| 97 | +} |
0 commit comments