76 lines
3.5 KiB
JavaScript
76 lines
3.5 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Idempotent sync of permission catalog and system role defaults into PostgreSQL.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.syncPermissionCatalog = syncPermissionCatalog;
|
|
const connection_1 = require("../db/connection");
|
|
const permissionCatalog_1 = require("./permissionCatalog");
|
|
async function syncPermissionCatalog() {
|
|
// Schema extensions (idempotent)
|
|
await (0, connection_1.query)(`
|
|
ALTER TABLE roles ADD COLUMN IF NOT EXISTS is_system BOOLEAN NOT NULL DEFAULT FALSE;
|
|
ALTER TABLE roles ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW();
|
|
ALTER TABLE roles ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT NOW();
|
|
`);
|
|
await (0, connection_1.query)(`
|
|
ALTER TABLE permissions ADD COLUMN IF NOT EXISTS category VARCHAR(64);
|
|
ALTER TABLE permissions ADD COLUMN IF NOT EXISTS resource VARCHAR(64);
|
|
ALTER TABLE permissions ADD COLUMN IF NOT EXISTS action VARCHAR(32);
|
|
ALTER TABLE permissions ADD COLUMN IF NOT EXISTS is_system BOOLEAN NOT NULL DEFAULT TRUE;
|
|
`);
|
|
await (0, connection_1.query)(`
|
|
CREATE TABLE IF NOT EXISTS user_permission_overrides (
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
permission_id INTEGER NOT NULL REFERENCES permissions(id) ON DELETE CASCADE,
|
|
effect VARCHAR(8) NOT NULL CHECK (effect IN ('allow', 'deny')),
|
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
|
updated_by UUID REFERENCES users(id),
|
|
PRIMARY KEY (user_id, permission_id)
|
|
)
|
|
`);
|
|
await (0, connection_1.query)(`
|
|
CREATE INDEX IF NOT EXISTS idx_user_permission_overrides_permission
|
|
ON user_permission_overrides(permission_id)
|
|
`);
|
|
await (0, connection_1.query)(`
|
|
CREATE INDEX IF NOT EXISTS idx_user_permission_overrides_effect
|
|
ON user_permission_overrides(effect)
|
|
`);
|
|
// Mark system roles
|
|
for (const name of permissionCatalog_1.SYSTEM_ROLE_NAMES) {
|
|
await (0, connection_1.query)(`UPDATE roles SET is_system = TRUE WHERE name = $1`, [name]);
|
|
}
|
|
// Upsert permissions from catalog
|
|
for (const def of permissionCatalog_1.PERMISSION_CATALOG) {
|
|
await (0, connection_1.query)(`INSERT INTO permissions (key, description, category, resource, action, is_system)
|
|
VALUES ($1, $2, $3, $4, $5, TRUE)
|
|
ON CONFLICT (key) DO UPDATE SET
|
|
description = EXCLUDED.description,
|
|
category = EXCLUDED.category,
|
|
resource = EXCLUDED.resource,
|
|
action = EXCLUDED.action`, [def.key, def.description, def.category, def.resource, def.action]);
|
|
}
|
|
// super_admin: all permissions
|
|
await (0, connection_1.query)(`
|
|
INSERT INTO role_permissions (role_id, permission_id)
|
|
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p
|
|
WHERE r.name = 'super_admin'
|
|
ON CONFLICT DO NOTHING
|
|
`);
|
|
await assignRolePermissions('admin', permissionCatalog_1.DEFAULT_ADMIN_PERMISSION_KEYS);
|
|
await assignRolePermissions('moderator', permissionCatalog_1.DEFAULT_MODERATOR_PERMISSION_KEYS);
|
|
}
|
|
async function assignRolePermissions(roleName, keys) {
|
|
const roleRows = await (0, connection_1.query)('SELECT id FROM roles WHERE name = $1', [roleName]);
|
|
const roleId = roleRows[0]?.id;
|
|
if (!roleId)
|
|
return;
|
|
for (const key of keys) {
|
|
await (0, connection_1.query)(`INSERT INTO role_permissions (role_id, permission_id)
|
|
SELECT $1, p.id FROM permissions p WHERE p.key = $2
|
|
ON CONFLICT DO NOTHING`, [roleId, key]);
|
|
}
|
|
}
|
|
//# sourceMappingURL=syncPermissions.js.map
|