feat: permission hooks, isAllowed and permittedOrgs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Pool } from 'pg';
|
||||
import { isAllowed } from '../permissions.js';
|
||||
|
||||
/**
|
||||
* Whether the caller may hand out this role at all: assigning it is exercising
|
||||
@@ -7,12 +8,6 @@ import type { Pool } from 'pg';
|
||||
export async function callerControlsRoleOrg(
|
||||
db: Pool, callerId: string, roleId: string, activity: string,
|
||||
): Promise<boolean> {
|
||||
const { rows } = await db.query<{ ok: boolean }>(
|
||||
`SELECT EXISTS (
|
||||
SELECT 1 FROM morbac.roles r
|
||||
WHERE r.id = $2::UUID AND morbac.is_allowed($1::UUID, r.org_id, $3, 'user_roles')
|
||||
) AS ok`,
|
||||
[callerId, roleId, activity],
|
||||
);
|
||||
return rows[0]?.ok ?? false;
|
||||
const { rows } = await db.query<{ org_id: string | null }>('SELECT org_id FROM morbac.roles WHERE id = $1::UUID', [roleId]);
|
||||
return rows.length > 0 && isAllowed(db, callerId, rows[0].org_id, activity, 'user_roles');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user