@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),
});
whereis{}when every row passes andFALSEwhen none does, so a query that ignoreshasAccessstill 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
LIKEwith%,_and\escaped; paths become~regexes.
Columns
| Key | Holds | Needed for |
|---|---|---|
id | The row's public id | Always |
parentId | The parent's public id | Listing across parents |
path | The stored path (/a/b.txt) | A type with a path alias |
tags | The row's JSONB tag bag | A tagged type |
parentTags | The parent's JSONB tag bag | Listing 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
| Export | Returns |
|---|---|
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 |