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
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz Source: /opt/alga-psa on psa.joliet.tech
103 lines
2.9 KiB
JavaScript
103 lines
2.9 KiB
JavaScript
const RESOURCE = 'billing.recurring_service_periods';
|
|
const ACTIONS = ['view', 'manage_future', 'regenerate', 'correct_history'];
|
|
const TARGET_ROLE_NAMES = ['admin', 'manager'];
|
|
|
|
async function assignPermissionsToRole(knex, tenant, roleId, permissionIds) {
|
|
if (!roleId || permissionIds.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const existing = await knex('role_permissions')
|
|
.where({ tenant, role_id: roleId })
|
|
.whereIn('permission_id', permissionIds)
|
|
.select('permission_id');
|
|
|
|
const existingIds = new Set(existing.map((row) => row.permission_id));
|
|
const inserts = permissionIds
|
|
.filter((permissionId) => !existingIds.has(permissionId))
|
|
.map((permissionId) => ({
|
|
tenant,
|
|
role_id: roleId,
|
|
permission_id: permissionId,
|
|
}));
|
|
|
|
if (inserts.length > 0) {
|
|
await knex('role_permissions').insert(inserts);
|
|
}
|
|
}
|
|
|
|
exports.up = async function up(knex) {
|
|
const tenants = await knex('tenants').select('tenant');
|
|
if (!tenants.length) {
|
|
return;
|
|
}
|
|
|
|
for (const { tenant } of tenants) {
|
|
const existingPerms = await knex('permissions')
|
|
.where({ tenant, resource: RESOURCE })
|
|
.select('permission_id', 'action');
|
|
|
|
const existingActions = new Set(existingPerms.map((row) => row.action));
|
|
const permissionsToAdd = ACTIONS
|
|
.filter((action) => !existingActions.has(action))
|
|
.map((action) => ({
|
|
tenant,
|
|
permission_id: knex.raw('gen_random_uuid()'),
|
|
resource: RESOURCE,
|
|
action,
|
|
created_at: new Date(),
|
|
}));
|
|
|
|
if (permissionsToAdd.length > 0) {
|
|
await knex('permissions').insert(permissionsToAdd);
|
|
}
|
|
|
|
const permissionRows = await knex('permissions')
|
|
.where({ tenant, resource: RESOURCE })
|
|
.whereIn('action', ACTIONS)
|
|
.select('permission_id');
|
|
const permissionIds = permissionRows.map((row) => row.permission_id);
|
|
|
|
const roles = await knex('roles')
|
|
.where({ tenant })
|
|
.whereIn(
|
|
knex.raw('LOWER(role_name)'),
|
|
TARGET_ROLE_NAMES,
|
|
)
|
|
.select('role_id', 'role_name');
|
|
|
|
for (const role of roles) {
|
|
await assignPermissionsToRole(knex, tenant, role.role_id, permissionIds);
|
|
}
|
|
}
|
|
};
|
|
|
|
exports.down = async function down(knex) {
|
|
const tenants = await knex('tenants').select('tenant');
|
|
if (!tenants.length) {
|
|
return;
|
|
}
|
|
|
|
for (const { tenant } of tenants) {
|
|
const permissionRows = await knex('permissions')
|
|
.where({ tenant, resource: RESOURCE })
|
|
.whereIn('action', ACTIONS)
|
|
.select('permission_id');
|
|
const permissionIds = permissionRows.map((row) => row.permission_id);
|
|
|
|
if (permissionIds.length === 0) {
|
|
continue;
|
|
}
|
|
|
|
await knex('role_permissions')
|
|
.where({ tenant })
|
|
.whereIn('permission_id', permissionIds)
|
|
.delete();
|
|
|
|
await knex('permissions')
|
|
.where({ tenant, resource: RESOURCE })
|
|
.whereIn('action', ACTIONS)
|
|
.delete();
|
|
}
|
|
};
|