2022-03-11 02:10:57 +00:00
|
|
|
import { ACL } from './acl';
|
|
|
|
|
2022-10-06 09:21:20 +00:00
|
|
|
export type ConditionFunc = (ctx: any) => Promise<boolean> | boolean;
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
export class AllowManager {
|
2022-03-11 02:10:57 +00:00
|
|
|
protected skipActions = new Map<string, Map<string, string | ConditionFunc | true>>();
|
|
|
|
|
|
|
|
protected registeredCondition = new Map<string, ConditionFunc>();
|
|
|
|
|
|
|
|
constructor(public acl: ACL) {
|
2022-04-24 02:14:46 +00:00
|
|
|
this.registerAllowCondition('loggedIn', (ctx) => {
|
2022-03-11 02:10:57 +00:00
|
|
|
return ctx.state.currentUser;
|
|
|
|
});
|
2022-04-24 02:14:46 +00:00
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
this.registerAllowCondition('public', (ctx) => {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
this.registerAllowCondition('allowConfigure', async (ctx) => {
|
|
|
|
const roleName = ctx.state.currentRole;
|
|
|
|
if (!roleName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
const role = acl.getRole(roleName);
|
|
|
|
if (!role) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-24 02:14:46 +00:00
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
return role.getStrategy()?.allowConfigure;
|
2022-04-24 02:14:46 +00:00
|
|
|
});
|
2022-03-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
allow(resourceName: string, actionName: string, condition?: string | ConditionFunc) {
|
2022-03-11 02:10:57 +00:00
|
|
|
const actionMap = this.skipActions.get(resourceName) || new Map<string, string | ConditionFunc>();
|
|
|
|
actionMap.set(actionName, condition || true);
|
|
|
|
|
|
|
|
this.skipActions.set(resourceName, actionMap);
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
getAllowedConditions(resourceName: string, actionName: string): Array<ConditionFunc | true> {
|
2022-03-11 02:10:57 +00:00
|
|
|
const fetchActionSteps: string[] = ['*', resourceName];
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const fetchActionStep of fetchActionSteps) {
|
|
|
|
const resource = this.skipActions.get(fetchActionStep);
|
|
|
|
if (resource) {
|
2023-01-08 23:35:48 +00:00
|
|
|
for (const fetchActionStep of ['*', actionName]) {
|
|
|
|
const condition = resource.get(fetchActionStep);
|
|
|
|
if (condition) {
|
|
|
|
results.push(typeof condition === 'string' ? this.registeredCondition.get(condition) : condition);
|
|
|
|
}
|
2022-03-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
registerAllowCondition(name: string, condition: ConditionFunc) {
|
2022-03-11 02:10:57 +00:00
|
|
|
this.registeredCondition.set(name, condition);
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
async isAllowed(resourceName: string, actionName: string, ctx: any) {
|
|
|
|
const skippedConditions = this.getAllowedConditions(resourceName, actionName);
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
for (const skippedCondition of skippedConditions) {
|
|
|
|
if (skippedCondition) {
|
|
|
|
let skipResult = false;
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
if (typeof skippedCondition === 'function') {
|
|
|
|
skipResult = await skippedCondition(ctx);
|
|
|
|
} else if (skippedCondition) {
|
|
|
|
skipResult = true;
|
|
|
|
}
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
if (skipResult) {
|
|
|
|
return true;
|
2022-03-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 23:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aclMiddleware() {
|
|
|
|
return async (ctx, next) => {
|
|
|
|
const { resourceName, actionName } = ctx.action;
|
2023-04-25 05:12:14 +00:00
|
|
|
const skip = await this.acl.allowManager.isAllowed(resourceName, actionName, ctx);
|
2022-03-11 02:10:57 +00:00
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
ctx.permission = {
|
2023-01-08 23:35:48 +00:00
|
|
|
...(ctx.permission || {}),
|
2022-03-11 02:10:57 +00:00
|
|
|
skip: true,
|
|
|
|
};
|
|
|
|
}
|
2023-01-08 23:35:48 +00:00
|
|
|
|
2022-03-11 02:10:57 +00:00
|
|
|
await next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|