2022-03-11 10:10:57 +08:00
|
|
|
import { ACL } from './acl';
|
|
|
|
|
2022-10-06 17:21:20 +08:00
|
|
|
export type ConditionFunc = (ctx: any) => Promise<boolean> | boolean;
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
export class AllowManager {
|
2022-03-11 10:10:57 +08:00
|
|
|
protected skipActions = new Map<string, Map<string, string | ConditionFunc | true>>();
|
|
|
|
|
|
|
|
protected registeredCondition = new Map<string, ConditionFunc>();
|
|
|
|
|
|
|
|
constructor(public acl: ACL) {
|
2022-04-24 10:14:46 +08:00
|
|
|
this.registerAllowCondition('loggedIn', (ctx) => {
|
2022-03-11 10:10:57 +08:00
|
|
|
return ctx.state.currentUser;
|
|
|
|
});
|
2022-04-24 10:14:46 +08:00
|
|
|
|
|
|
|
this.registerAllowCondition('allowConfigure', async (ctx) => {
|
|
|
|
const roleName = ctx.state.currentRole;
|
|
|
|
if (!roleName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const roleInstance = await ctx.db.getRepository('roles').findOne({
|
2022-07-25 19:33:23 +08:00
|
|
|
filter: {
|
2022-10-06 10:29:53 +08:00
|
|
|
name: roleName,
|
2022-07-25 19:33:23 +08:00
|
|
|
},
|
2022-04-24 10:14:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return roleInstance?.get('allowConfigure');
|
|
|
|
});
|
2022-03-11 10:10:57 +08:00
|
|
|
}
|
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
allow(resourceName: string, actionName: string, condition?: string | ConditionFunc) {
|
2022-03-11 10:10:57 +08: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 10:14:46 +08:00
|
|
|
getAllowedConditions(resourceName: string, actionName: string): Array<ConditionFunc | true> {
|
2022-03-11 10:10:57 +08:00
|
|
|
const fetchActionSteps: string[] = ['*', resourceName];
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const fetchActionStep of fetchActionSteps) {
|
|
|
|
const resource = this.skipActions.get(fetchActionStep);
|
|
|
|
if (resource) {
|
|
|
|
const condition = resource.get('*') || resource.get(actionName);
|
|
|
|
if (condition) {
|
|
|
|
results.push(typeof condition === 'string' ? this.registeredCondition.get(condition) : condition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
registerAllowCondition(name: string, condition: ConditionFunc) {
|
2022-03-11 10:10:57 +08:00
|
|
|
this.registeredCondition.set(name, condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
aclMiddleware() {
|
|
|
|
return async (ctx, next) => {
|
|
|
|
const { resourceName, actionName } = ctx.action;
|
2022-04-24 10:14:46 +08:00
|
|
|
const skippedConditions = ctx.app.acl.allowManager.getAllowedConditions(resourceName, actionName);
|
2022-03-11 10:10:57 +08:00
|
|
|
let skip = false;
|
|
|
|
|
|
|
|
for (const skippedCondition of skippedConditions) {
|
|
|
|
if (skippedCondition) {
|
|
|
|
let skipResult = false;
|
|
|
|
|
|
|
|
if (typeof skippedCondition === 'function') {
|
2022-04-24 10:14:46 +08:00
|
|
|
skipResult = await skippedCondition(ctx);
|
2022-03-11 10:10:57 +08:00
|
|
|
} else if (skippedCondition) {
|
|
|
|
skipResult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skipResult) {
|
|
|
|
skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
ctx.permission = {
|
|
|
|
skip: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|