summaryrefslogtreecommitdiffhomepage
path: root/packages/core/src/permission/evaluate.ts
blob: 7a0d235c30dff7b06cf23989e6e485209e470668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import type { PermissionRule, Ruleset } from "./index.js";
import { Wildcard } from "./wildcard.js";

export function evaluate(
	permission: string,
	pattern: string,
	...rulesets: Ruleset[]
): PermissionRule {
	const flat = ([] as PermissionRule[]).concat(...rulesets);
	const match = flat.findLast(
		(rule) => Wildcard.match(rule.permission, permission) && Wildcard.match(rule.pattern, pattern),
	);
	if (match) return match;
	return { action: "ask", permission, pattern };
}