PSA/server/migrations/20260102213000_add_service_catalog_permissions.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

102 lines
3.7 KiB
JavaScript

/**
* Backfill RBAC permissions for the service catalog.
*
* Products are implemented as a subset of the service catalog (`service_catalog.item_kind = 'product'`),
* so we reuse the `service:*` RBAC resource for both services and products.
*/
exports.up = async function up(knex) {
const tenants = await knex('tenants').select('tenant');
const permissionDefs = [
{ resource: 'service', action: 'create', msp: true, client: false, description: 'Create services/products in the service catalog' },
{ resource: 'service', action: 'read', msp: true, client: false, description: 'View services/products in the service catalog' },
{ resource: 'service', action: 'update', msp: true, client: false, description: 'Update services/products in the service catalog' },
{ resource: 'service', action: 'delete', msp: true, client: false, description: 'Archive/delete services/products in the service catalog' },
];
for (const { tenant } of tenants) {
// Insert any missing permissions for this tenant
for (const def of permissionDefs) {
const existing = await knex('permissions')
.where({ tenant, resource: def.resource, action: def.action })
.first(['permission_id', 'msp', 'client', 'description']);
if (!existing) {
await knex('permissions').insert({
tenant,
resource: def.resource,
action: def.action,
msp: def.msp,
client: def.client,
description: def.description,
});
} else {
// Keep existing permissions, but ensure MSP flag/description are set.
const nextMsp = Boolean(existing.msp) || def.msp;
const nextClient = Boolean(existing.client) || def.client;
const nextDescription = existing.description || def.description;
if (nextMsp !== existing.msp || nextClient !== existing.client || nextDescription !== existing.description) {
await knex('permissions')
.where({ tenant, permission_id: existing.permission_id })
.update({
msp: nextMsp,
client: nextClient,
description: nextDescription,
updated_at: knex.fn.now(),
});
}
}
}
// Ensure MSP Admin role gets these permissions (Admin is defined as "all MSP permissions")
// but role_permissions may already be missing them on existing tenants.
const adminRole = await knex('roles')
.where({ tenant, role_name: 'Admin', msp: true })
.first(['role_id']);
if (!adminRole) continue;
const perms = await knex('permissions')
.where({ tenant, resource: 'service' })
.whereIn('action', permissionDefs.map((d) => d.action))
.select(['permission_id']);
for (const { permission_id } of perms) {
const existingRp = await knex('role_permissions')
.where({ tenant, role_id: adminRole.role_id, permission_id })
.first('tenant');
if (existingRp) continue;
await knex('role_permissions').insert({
tenant,
role_id: adminRole.role_id,
permission_id,
});
}
}
};
exports.down = async function down(knex) {
const tenants = await knex('tenants').select('tenant');
const actions = ['create', 'read', 'update', 'delete'];
for (const { tenant } of tenants) {
const permissionIds = await knex('permissions')
.where({ tenant, resource: 'service' })
.whereIn('action', actions)
.pluck('permission_id');
if (permissionIds.length > 0) {
await knex('role_permissions')
.where({ tenant })
.whereIn('permission_id', permissionIds)
.del();
await knex('permissions')
.where({ tenant, resource: 'service' })
.whereIn('action', actions)
.del();
}
}
};