import type { Knex } from "knex"; import { WebinyError } from "@webiny/error"; import type { AcoFolderLevelPermissionsStorageOperations, FolderLevelPermission, StorageOperationsBatchUpdateFlpParams, StorageOperationsCreateFlpParams, StorageOperationsDeleteFlpParams, StorageOperationsGetFlpParams, StorageOperationsListFlpsParams, StorageOperationsUpdateFlpParams } from "@webiny/api-aco/types.js"; interface FlpRow { id: string; tenant: string; type: string; slug: string; path: string; parentId: string; permissions: string; } const BASE_TABLE_NAME = "webiny_aco_flp"; export interface StorageOperationsConfig { knex: Knex; tableNamePrefix?: string; /** * The schema was set up before the API took traffic (see `SqlSchemaConfig` in * `@webiny/api-core-sql`): the table is never checked for or created at runtime. */ schemaManaged?: boolean; } export class FolderLevelPermissionsStorageOperations implements AcoFolderLevelPermissionsStorageOperations { private readonly knex: Knex; private readonly tableName: string; private readonly schemaManaged: boolean; constructor({ knex, tableNamePrefix, schemaManaged }: StorageOperationsConfig) { this.knex = knex; this.schemaManaged = schemaManaged === true; this.tableName = tableNamePrefix ? `${tableNamePrefix}_${BASE_TABLE_NAME}` : BASE_TABLE_NAME; } public async list({ where: { tenant, type, path_startsWith, parentId } }: StorageOperationsListFlpsParams): Promise { try { await this.ensureTable(); if (parentId) { const rows = await this.knex(this.tableName) .where("tenant", tenant) .andWhere("parentId", parentId); return rows.map(row => this.fromRow(row)); } if (path_startsWith) { const rows = await this.knex(this.tableName) .where("tenant", tenant) .andWhere("type", type) .andWhere("path", "like", path_startsWith + "%"); return rows.map(row => this.fromRow(row)); } throw new WebinyError("Missing required parameters.", "LIST_FLP_MISSING_PARAMETERS", { tenant, type, path_startsWith, parentId }); } catch (err) { throw WebinyError.from(err, { message: "Could not list folder level permissions.", code: "LIST_FLP_ERROR" }); } } public async get({ tenant, id }: StorageOperationsGetFlpParams): Promise { try { await this.ensureTable(); const row = await this.knex(this.tableName) .where("tenant", tenant) .andWhere("id", id) .first(); if (!row) { return null; } return this.fromRow(row); } catch (err) { throw WebinyError.from(err, { message: "Could not load folder level permission.", code: "GET_FLP_ERROR", data: { tenant, id } }); } } public async create({ data }: StorageOperationsCreateFlpParams): Promise { try { await this.ensureTable(); await this.knex(this.tableName).insert(this.toRow(data)); return data; } catch (err) { throw WebinyError.from(err, { message: "Could not create folder level permission.", code: "CREATE_FLP_ERROR", data: { data } }); } } public async update({ data: inputData, original }: StorageOperationsUpdateFlpParams): Promise { try { await this.ensureTable(); const data = { ...original, ...inputData }; await this.knex(this.tableName) .where("tenant", data.tenant) .andWhere("id", data.id) .update(this.toRow(data)); return data; } catch (err) { throw WebinyError.from(err, { message: "Could not update folder level permission.", code: "UPDATE_FLP_ERROR", data: { inputData, original } }); } } public async delete({ flp }: StorageOperationsDeleteFlpParams): Promise { try { await this.ensureTable(); await this.knex(this.tableName) .where("tenant", flp.tenant) .andWhere("id", flp.id) .delete(); } catch (err) { throw WebinyError.from(err, { message: "Could not delete folder level permission.", code: "DELETE_FLP_ERROR", data: { flp } }); } } public async batchUpdate({ items }: StorageOperationsBatchUpdateFlpParams): Promise { try { await this.ensureTable(); // Built inside the callback: a transaction that loses its connection runs it again. return await this.knex.transaction(async trx => { const updatedItems: FolderLevelPermission[] = []; for (const { original, data: inputData } of items) { const data = { ...original, ...inputData }; await trx(this.tableName) .where("tenant", data.tenant) .andWhere("id", data.id) .update(this.toRow(data)); updatedItems.push(data); } return updatedItems; }); } catch (err) { throw WebinyError.from(err, { message: "Could not batch update folder level permissions.", code: "BATCH_UPDATE_FLP_ERROR", data: { items } }); } } private async ensureTable(): Promise { // Set up before the API took traffic, so don't check for it on every call. if (this.schemaManaged) { return; } await this.setupSchema(); } /* Creates the table if missing, whatever the mode (deploy-time schema setup). */ public async setupSchema(): Promise { const exists = await this.knex.schema.hasTable(this.tableName); if (exists) { return; } await this.knex.schema.createTable(this.tableName, table => { table.text("id").notNullable(); table.text("tenant").notNullable(); table.text("type").notNullable(); table.text("slug").notNullable(); table.text("path").notNullable(); table.text("parentId").notNullable(); table.text("permissions").notNullable(); table.primary(["tenant", "id"]); }); } private toRow(data: FolderLevelPermission & { tenant: string }): FlpRow { return { id: data.id, tenant: data.tenant, type: data.type, slug: data.slug, path: data.path, parentId: data.parentId, permissions: JSON.stringify(data.permissions) }; } private fromRow(row: FlpRow): FolderLevelPermission { return { id: row.id, parentId: row.parentId, slug: row.slug, path: row.path, permissions: JSON.parse(row.permissions), type: row.type }; } }