Skip to main content

@ttoss/iam-postgresdb

Compiles @ttoss/iam policies into a Sequelize where, so a list query returns exactly the rows the policies allow an action on.

Installation​

pnpm add @ttoss/iam @ttoss/iam-postgresdb sequelize

Usage​

import {
compilePolicyWhere,
referencesAssociation,
} from '@ttoss/iam-postgresdb';

const { where, hasAccess } = compilePolicyWhere({
iam,
policies: [userPolicies],
action: 'memories:GetMemory',
list: {
root: { type: 'project', id: projectId },
resourceType: 'memory',
parent: { type: 'memory_store' },
rowTagged: true,
parentTagged: true,
},
columns: {
id: { column: 'publicId', alias: 'Memory' },
tags: { column: 'tags', alias: 'Memory' },
parentId: { column: 'publicId', alias: 'store' },
parentTags: { column: 'tags', alias: 'store' },
},
});

if (!hasAccess) return [];

await Memory.findAll({
where,
include: [{ model: MemoryStore, as: 'store', attributes: [] }],
subQuery: !referencesAssociation(where),
});
  • where is {} when every row passes and FALSE when none does, so a query that ignores hasAccess still reads nothing.
  • Every leaf is two-valued: a row with no path or tag bag is excluded by the leaf and included by its negation, never dropped as NULL.
  • Ids and tag globs become LIKE with %, _ and \ escaped; paths become ~ regexes.

Columns​

KeyHoldsNeeded for
idThe row's public idAlways
parentIdThe parent's public idListing across parents
pathThe stored path (/a/b.txt)A type with a path alias
tagsThe row's JSONB tag bagA tagged type
parentTagsThe parent's JSONB tag bagListing across tagged parents

A column is { column, alias? }. alias is its table's alias in the query: an association's, or the root model's name. Name the root model when the query joins a table holding a column of the same name. Aliased columns are written as $alias.field$ with the field underscored, which needs subQuery: false. A filter that reads a column the map lacks throws.

API​

ExportReturns
compilePolicyWhere({ iam, policies, action, list, columns }){ where, hasAccess }
toSequelizeWhere({ filter, columns })The where of a RowFilter
hasPolicyConstraints(where)Whether the clause restricts anything
referencesAssociation(where)Whether it names an $alias.field$ column
globToLike(glob)The glob as an escaped LIKE pattern
columnRef(spec)The attribute or $alias.field$ reference