2022-02-11 19:31:53 +08:00
|
|
|
import { Action } from '@nocobase/resourcer';
|
2022-09-29 21:05:31 +08:00
|
|
|
import { Toposort, ToposortOptions } from '@nocobase/utils';
|
2022-02-11 19:31:53 +08:00
|
|
|
import EventEmitter from 'events';
|
2022-07-04 17:50:18 +08:00
|
|
|
import parse from 'json-templates';
|
2022-02-11 19:31:53 +08:00
|
|
|
import compose from 'koa-compose';
|
2022-01-18 16:38:03 +08:00
|
|
|
import lodash from 'lodash';
|
2022-10-06 10:29:53 +08:00
|
|
|
import { ACLAvailableAction, AvailableActionOptions } from './acl-available-action';
|
2022-01-24 14:10:35 +08:00
|
|
|
import { ACLAvailableStrategy, AvailableStrategyOptions, predicate } from './acl-available-strategy';
|
2022-10-06 10:29:53 +08:00
|
|
|
import { ACLRole, ResourceActionsOptions, RoleActionParams } from './acl-role';
|
|
|
|
import { AllowManager, ConditionFunc } from './allow-manager';
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
interface CanResult {
|
|
|
|
role: string;
|
|
|
|
resource: string;
|
|
|
|
action: string;
|
|
|
|
params?: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DefineOptions {
|
|
|
|
role: string;
|
2022-01-24 14:10:35 +08:00
|
|
|
allowConfigure?: boolean;
|
2022-10-06 10:29:53 +08:00
|
|
|
strategy?: string | AvailableStrategyOptions;
|
|
|
|
actions?: ResourceActionsOptions;
|
2022-01-18 16:38:03 +08:00
|
|
|
routes?: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ListenerContext {
|
|
|
|
acl: ACL;
|
|
|
|
role: ACLRole;
|
2022-01-24 14:10:35 +08:00
|
|
|
path: string;
|
|
|
|
actionName: string;
|
|
|
|
resourceName: string;
|
2022-01-18 16:38:03 +08:00
|
|
|
params: RoleActionParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Listener = (ctx: ListenerContext) => void;
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
interface CanArgs {
|
|
|
|
role: string;
|
|
|
|
resource: string;
|
|
|
|
action: string;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
export class ACL extends EventEmitter {
|
2022-10-06 10:29:53 +08:00
|
|
|
protected availableActions = new Map<string, ACLAvailableAction>();
|
2022-01-18 16:38:03 +08:00
|
|
|
protected availableStrategy = new Map<string, ACLAvailableStrategy>();
|
2022-09-29 21:05:31 +08:00
|
|
|
protected middlewares: Toposort<any>;
|
2022-01-18 16:38:03 +08:00
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
public allowManager = new AllowManager(this);
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
roles = new Map<string, ACLRole>();
|
|
|
|
|
|
|
|
actionAlias = new Map<string, string>();
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
configResources: string[] = [];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
this.middlewares = new Toposort<any>();
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
this.beforeGrantAction((ctx) => {
|
|
|
|
if (lodash.isPlainObject(ctx.params) && ctx.params.own) {
|
|
|
|
ctx.params = lodash.merge(ctx.params, predicate.own);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.beforeGrantAction((ctx) => {
|
|
|
|
const actionName = this.resolveActionAlias(ctx.actionName);
|
|
|
|
|
|
|
|
if (lodash.isPlainObject(ctx.params)) {
|
|
|
|
if ((actionName === 'create' || actionName === 'update') && ctx.params.fields) {
|
|
|
|
ctx.params = {
|
|
|
|
...lodash.omit(ctx.params, 'fields'),
|
|
|
|
whitelist: ctx.params.fields,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actionName === 'view' && ctx.params.fields) {
|
|
|
|
const appendFields = ['id', 'createdAt', 'updatedAt'];
|
|
|
|
ctx.params = {
|
|
|
|
...lodash.omit(ctx.params, 'fields'),
|
|
|
|
fields: [...ctx.params.fields, ...appendFields],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
this.middlewares.add(this.allowManager.aclMiddleware());
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
define(options: DefineOptions): ACLRole {
|
|
|
|
const roleName = options.role;
|
|
|
|
const role = new ACLRole(this, roleName);
|
|
|
|
|
|
|
|
if (options.strategy) {
|
|
|
|
role.strategy = options.strategy;
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = options.actions || {};
|
|
|
|
|
|
|
|
for (const [actionName, actionParams] of Object.entries(actions)) {
|
|
|
|
role.grantAction(actionName, actionParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.roles.set(roleName, role);
|
|
|
|
|
|
|
|
return role;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRole(name: string): ACLRole {
|
|
|
|
return this.roles.get(name);
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
removeRole(name: string) {
|
|
|
|
return this.roles.delete(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerConfigResources(names: string[]) {
|
|
|
|
names.forEach((name) => this.registerConfigResource(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
registerConfigResource(name: string) {
|
|
|
|
this.configResources.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
isConfigResource(name: string) {
|
|
|
|
return this.configResources.includes(name);
|
|
|
|
}
|
|
|
|
|
2022-08-16 14:41:29 +08:00
|
|
|
setAvailableAction(name: string, options: AvailableActionOptions = {}) {
|
2022-10-06 10:29:53 +08:00
|
|
|
this.availableActions.set(name, new ACLAvailableAction(name, options));
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
if (options.aliases) {
|
|
|
|
const aliases = lodash.isArray(options.aliases) ? options.aliases : [options.aliases];
|
|
|
|
for (const alias of aliases) {
|
|
|
|
this.actionAlias.set(alias, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:02:58 +08:00
|
|
|
getAvailableAction(name: string) {
|
|
|
|
const actionName = this.actionAlias.get(name) || name;
|
|
|
|
return this.availableActions.get(actionName);
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
getAvailableActions() {
|
|
|
|
return this.availableActions;
|
|
|
|
}
|
|
|
|
|
2022-10-06 10:29:53 +08:00
|
|
|
setAvailableStrategy(name: string, options: AvailableStrategyOptions) {
|
2022-01-24 14:10:35 +08:00
|
|
|
this.availableStrategy.set(name, new ACLAvailableStrategy(this, options));
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
beforeGrantAction(listener?: Listener) {
|
|
|
|
this.addListener('beforeGrantAction', listener);
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-10-16 19:16:14 +08:00
|
|
|
can(options: CanArgs): CanResult | null {
|
|
|
|
const { role, resource, action } = options;
|
2022-01-18 16:38:03 +08:00
|
|
|
const aclRole = this.roles.get(role);
|
2022-02-11 19:31:53 +08:00
|
|
|
|
|
|
|
if (!aclRole) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
const aclResource = aclRole.getResource(resource);
|
|
|
|
|
|
|
|
if (aclResource) {
|
2022-01-18 20:29:41 +08:00
|
|
|
const actionParams = aclResource.getAction(action);
|
2022-01-18 16:38:03 +08:00
|
|
|
|
2022-01-18 20:29:41 +08:00
|
|
|
if (actionParams) {
|
2022-01-18 16:38:03 +08:00
|
|
|
// handle single action config
|
|
|
|
return {
|
|
|
|
role,
|
|
|
|
resource,
|
|
|
|
action,
|
2022-01-18 20:29:41 +08:00
|
|
|
params: actionParams,
|
2022-01-18 16:38:03 +08:00
|
|
|
};
|
2022-05-04 10:16:53 +08:00
|
|
|
} else {
|
|
|
|
return null;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
if (!aclRole.strategy) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
const roleStrategy = lodash.isString(aclRole.strategy)
|
|
|
|
? this.availableStrategy.get(aclRole.strategy)
|
2022-01-24 14:10:35 +08:00
|
|
|
: new ACLAvailableStrategy(this, aclRole.strategy);
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
if (!roleStrategy) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
const roleStrategyParams = roleStrategy.allow(resource, this.resolveActionAlias(action));
|
|
|
|
|
|
|
|
if (roleStrategyParams) {
|
|
|
|
const result = { role, resource, action };
|
|
|
|
|
|
|
|
if (lodash.isPlainObject(roleStrategyParams)) {
|
|
|
|
result['params'] = roleStrategyParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected isAvailableAction(actionName: string) {
|
2022-01-18 20:29:41 +08:00
|
|
|
return this.availableActions.has(this.resolveActionAlias(actionName));
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-18 20:29:41 +08:00
|
|
|
public resolveActionAlias(action: string) {
|
2022-01-18 16:38:03 +08:00
|
|
|
return this.actionAlias.get(action) ? this.actionAlias.get(action) : action;
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
use(fn: any, options?: ToposortOptions) {
|
|
|
|
this.middlewares.add(fn, options);
|
2022-02-11 19:31:53 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 10:29:53 +08:00
|
|
|
allow(resourceName: string, actionNames: string[] | string, condition?: string | ConditionFunc) {
|
2022-04-24 10:14:46 +08:00
|
|
|
if (!Array.isArray(actionNames)) {
|
|
|
|
actionNames = [actionNames];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const actionName of actionNames) {
|
|
|
|
this.allowManager.allow(resourceName, actionName, condition);
|
|
|
|
}
|
2022-03-11 10:10:57 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:02:58 +08:00
|
|
|
parseJsonTemplate(json: any, ctx: any) {
|
|
|
|
return parse(json)({
|
|
|
|
ctx: {
|
|
|
|
state: JSON.parse(JSON.stringify(ctx.state)),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
middleware() {
|
2022-02-11 19:31:53 +08:00
|
|
|
const acl = this;
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-04-12 00:14:33 +08:00
|
|
|
const filterParams = (ctx, resourceName, params) => {
|
|
|
|
if (params?.filter?.createdById) {
|
|
|
|
const collection = ctx.db.getCollection(resourceName);
|
2022-04-12 12:02:58 +08:00
|
|
|
if (collection && !collection.getField('createdById')) {
|
2022-04-12 00:14:33 +08:00
|
|
|
return lodash.omit(params, 'filter.createdById');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
return async function ACLMiddleware(ctx, next) {
|
2022-02-11 19:31:53 +08:00
|
|
|
const roleName = ctx.state.currentRole || 'anonymous';
|
2022-01-24 14:10:35 +08:00
|
|
|
const { resourceName, actionName } = ctx.action;
|
|
|
|
|
|
|
|
const resourcerAction: Action = ctx.action;
|
|
|
|
|
|
|
|
ctx.can = (options: Omit<CanArgs, 'role'>) => {
|
2022-02-11 19:31:53 +08:00
|
|
|
return acl.can({ role: roleName, ...options });
|
2022-01-24 14:10:35 +08:00
|
|
|
};
|
|
|
|
|
2022-02-11 19:31:53 +08:00
|
|
|
ctx.permission = {
|
|
|
|
can: ctx.can({ resource: resourceName, action: actionName }),
|
|
|
|
};
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
return compose(acl.middlewares.nodes)(ctx, async () => {
|
2022-02-11 19:31:53 +08:00
|
|
|
const permission = ctx.permission;
|
|
|
|
|
|
|
|
if (permission.skip) {
|
|
|
|
return next();
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-02-11 19:31:53 +08:00
|
|
|
if (!permission.can || typeof permission.can !== 'object') {
|
2022-04-10 19:22:39 +08:00
|
|
|
ctx.throw(403, 'No permissions');
|
2022-02-11 19:31:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-02-11 19:31:53 +08:00
|
|
|
const { params } = permission.can;
|
|
|
|
|
|
|
|
if (params) {
|
2022-04-12 00:14:33 +08:00
|
|
|
const filteredParams = filterParams(ctx, resourceName, params);
|
2022-04-12 12:02:58 +08:00
|
|
|
const parsedParams = acl.parseJsonTemplate(filteredParams, ctx);
|
2022-04-12 00:14:33 +08:00
|
|
|
resourcerAction.mergeParams(parsedParams);
|
2022-02-11 19:31:53 +08:00
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-02-11 19:31:53 +08:00
|
|
|
await next();
|
|
|
|
});
|
2022-01-24 14:10:35 +08:00
|
|
|
};
|
|
|
|
}
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|