mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 01:45:53 +00:00
d805fafbfc
* docs: relation-repository * docs: has many repository * docs: acl * docs: acl * docs: acl * docs: acl * docs: acl/AllowManager * docs: acl/ACLAvailableAction * docs: acl * docs: clean up * feat: doc menus Co-authored-by: chenos <chenlinxh@gmail.com>
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import lodash from 'lodash';
|
|
import { ACL } from './acl';
|
|
type StrategyValue = false | '*' | string | string[];
|
|
|
|
export interface AvailableStrategyOptions {
|
|
displayName?: string;
|
|
actions?: false | string | string[];
|
|
allowConfigure?: boolean;
|
|
resource?: '*';
|
|
}
|
|
|
|
export const predicate = {
|
|
own: {
|
|
filter: {
|
|
createdById: '{{ ctx.state.currentUser.id }}',
|
|
},
|
|
},
|
|
all: {},
|
|
};
|
|
|
|
export class ACLAvailableStrategy {
|
|
acl: ACL;
|
|
options: AvailableStrategyOptions;
|
|
actionsAsObject: { [key: string]: string };
|
|
|
|
allowConfigure: boolean;
|
|
|
|
constructor(acl: ACL, options: AvailableStrategyOptions) {
|
|
this.acl = acl;
|
|
this.options = options;
|
|
this.allowConfigure = options.allowConfigure;
|
|
|
|
let actions = this.options.actions;
|
|
if (lodash.isString(actions) && actions != '*') {
|
|
actions = [actions];
|
|
}
|
|
|
|
if (lodash.isArray(actions)) {
|
|
this.actionsAsObject = actions.reduce((carry, action) => {
|
|
const [actionName, predicate] = action.split(':');
|
|
carry[actionName] = predicate;
|
|
return carry;
|
|
}, {});
|
|
}
|
|
}
|
|
|
|
matchAction(actionName: string) {
|
|
if (this.options.actions == '*') {
|
|
return true;
|
|
}
|
|
|
|
if (this.actionsAsObject?.hasOwnProperty(actionName)) {
|
|
const predicateName = this.actionsAsObject[actionName];
|
|
if (predicateName) {
|
|
return predicate[predicateName];
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
allow(resourceName: string, actionName: string) {
|
|
if (this.acl.isConfigResource(resourceName) && this.allowConfigure) {
|
|
return true;
|
|
}
|
|
|
|
return this.matchAction(this.acl.resolveActionAlias(actionName));
|
|
}
|
|
}
|