Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 40x 36x 36x 36x 36x 36x 40x 19x 19x 40x 20x 20x 20x |
import { doPermApplyToCtx } from '../../security/doPermApplyToCtx'
import { DaoGenericMethods, DaoHookSharedParsed } from '../../types/core.types.js'
type PublicAndSystemApplyToAllValues = Parameters<typeof doPermApplyToCtx>[2]
export async function appliableHooksForUser<Hook extends Partial<DaoHookSharedParsed>[]>(
ctx: Ctx,
hooks: Hook,
method: DaoGenericMethods,
/** Define which value should be returned when ctx.isSystem.
* * => 'matchStrict' will search for an exact matching perm in forPerms
* * => 'matchAll' will search for a matching perm in forPerms, including 'ALL' perm
*/
systemPermApplyWhenAll: PublicAndSystemApplyToAllValues,
/** Define which value should be returned when ctx.isPublic.
* * => 'matchStrict' will search for an exact matching perm in forPerms
* * => 'matchAll' will search for a matching perm in forPerms, including 'ALL' perm
*/
returnValueIfPublic: PublicAndSystemApplyToAllValues | ((hook: Hook[number]) => PublicAndSystemApplyToAllValues),
/** If forPerm includes public, what shall be returned for a user with a perm 'user' for example? */
returnValueIfForPermContainsPublic: 'alwaysReturnTrue' | 'alwaysReturnFalse' | 'match' = 'match'
): Promise<Hook> {
const appliableHooks = [] as any as Hook
for (const hookObj of hooks) {
if ((hookObj.on || []).includes(method) && await doPermApplyToCtx(
ctx,
hookObj.for || [],
systemPermApplyWhenAll,
typeof returnValueIfPublic === 'function' ? returnValueIfPublic(hookObj) : returnValueIfPublic,
returnValueIfForPermContainsPublic,
)) {
appliableHooks.push(hookObj)
}
}
return appliableHooks
} |