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
|
|
|
|
|
|
|
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 11:33:23 +00:00
|
|
|
filter: {
|
2022-10-06 02:29:53 +00:00
|
|
|
name: roleName,
|
2022-07-25 11:33:23 +00:00
|
|
|
},
|
2022-04-24 02:14:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return roleInstance?.get('allowConfigure');
|
|
|
|
});
|
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) {
|
|
|
|
const condition = resource.get('*') || resource.get(actionName);
|
|
|
|
if (condition) {
|
|
|
|
results.push(typeof condition === 'string' ? this.registeredCondition.get(condition) : condition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
aclMiddleware() {
|
|
|
|
return async (ctx, next) => {
|
|
|
|
const { resourceName, actionName } = ctx.action;
|
2022-04-24 02:14:46 +00:00
|
|
|
const skippedConditions = ctx.app.acl.allowManager.getAllowedConditions(resourceName, actionName);
|
2022-03-11 02:10:57 +00:00
|
|
|
let skip = false;
|
|
|
|
|
|
|
|
for (const skippedCondition of skippedConditions) {
|
|
|
|
if (skippedCondition) {
|
|
|
|
let skipResult = false;
|
|
|
|
|
|
|
|
if (typeof skippedCondition === 'function') {
|
2022-04-24 02:14:46 +00:00
|
|
|
skipResult = await skippedCondition(ctx);
|
2022-03-11 02:10:57 +00:00
|
|
|
} else if (skippedCondition) {
|
|
|
|
skipResult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skipResult) {
|
|
|
|
skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
ctx.permission = {
|
|
|
|
skip: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|