2022-01-18 16:38:03 +08:00
|
|
|
import { ACL, DefineOptions } from './acl';
|
2023-01-09 07:35:48 +08:00
|
|
|
import { ACLAvailableStrategy, AvailableStrategyOptions } from './acl-available-strategy';
|
2022-04-10 19:22:39 +08:00
|
|
|
import { ACLResource } from './acl-resource';
|
2023-01-09 07:35:48 +08:00
|
|
|
import lodash from 'lodash';
|
|
|
|
import minimatch from 'minimatch';
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
export interface RoleActionParams {
|
|
|
|
fields?: string[];
|
|
|
|
filter?: any;
|
|
|
|
own?: boolean;
|
|
|
|
whitelist?: string[];
|
|
|
|
blacklist?: string[];
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2022-10-06 10:29:53 +08:00
|
|
|
export interface ResourceActionsOptions {
|
2022-01-18 16:38:03 +08:00
|
|
|
[actionName: string]: RoleActionParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ACLRole {
|
|
|
|
strategy: string | AvailableStrategyOptions;
|
|
|
|
resources = new Map<string, ACLResource>();
|
2023-01-09 07:35:48 +08:00
|
|
|
snippets: Set<string> = new Set();
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
constructor(public acl: ACL, public name: string) {}
|
|
|
|
|
|
|
|
getResource(name: string): ACLResource | undefined {
|
|
|
|
return this.resources.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setStrategy(value: string | AvailableStrategyOptions) {
|
|
|
|
this.strategy = value;
|
|
|
|
}
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
public getStrategy() {
|
|
|
|
if (!this.strategy) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lodash.isString(this.strategy)
|
|
|
|
? this.acl.availableStrategy.get(this.strategy)
|
|
|
|
: new ACLAvailableStrategy(this.acl, this.strategy);
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
public getResourceActionsParams(resourceName: string) {
|
|
|
|
const resource = this.getResource(resourceName);
|
|
|
|
return resource.getActions();
|
|
|
|
}
|
|
|
|
|
2022-10-06 10:29:53 +08:00
|
|
|
public revokeResource(resourceName: string) {
|
2022-01-30 10:37:27 +08:00
|
|
|
for (const key of [...this.resources.keys()]) {
|
|
|
|
if (key === resourceName || key.includes(`${resourceName}.`)) {
|
|
|
|
this.resources.delete(key);
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public grantAction(path: string, options?: RoleActionParams) {
|
|
|
|
let { resource, resourceName, actionName } = this.getResourceActionFromPath(path);
|
|
|
|
|
|
|
|
if (!resource) {
|
|
|
|
resource = new ACLResource({
|
|
|
|
role: this,
|
|
|
|
name: resourceName,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.resources.set(resourceName, resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.setAction(actionName, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getActionParams(path: string): RoleActionParams {
|
|
|
|
const { action } = this.getResourceActionFromPath(path);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public revokeAction(path: string) {
|
|
|
|
const { resource, actionName } = this.getResourceActionFromPath(path);
|
|
|
|
resource.removeAction(actionName);
|
|
|
|
}
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
public effectiveSnippets(): { allowed: Array<string>; rejected: Array<string> } {
|
|
|
|
const allowedSnippets = new Set<string>();
|
|
|
|
const rejectedSnippets = new Set<string>();
|
|
|
|
|
|
|
|
const availableSnippets = this.acl.snippetManager.snippets;
|
|
|
|
|
|
|
|
for (let snippetRule of this.snippets) {
|
|
|
|
const negated = snippetRule.startsWith('!');
|
|
|
|
snippetRule = negated ? snippetRule.slice(1) : snippetRule;
|
|
|
|
|
|
|
|
for (const [_, availableSnippet] of availableSnippets) {
|
|
|
|
if (minimatch(availableSnippet.name, snippetRule)) {
|
|
|
|
if (negated) {
|
|
|
|
rejectedSnippets.add(availableSnippet.name);
|
|
|
|
} else {
|
|
|
|
allowedSnippets.add(availableSnippet.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get difference of allowed and rejected snippets
|
|
|
|
const effectiveSnippets = new Set([...allowedSnippets].filter((x) => !rejectedSnippets.has(x)));
|
|
|
|
return {
|
|
|
|
allowed: [...effectiveSnippets],
|
|
|
|
rejected: [...rejectedSnippets],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public snippetAllowed(actionPath: string) {
|
|
|
|
const effectiveSnippets = this.effectiveSnippets();
|
|
|
|
|
|
|
|
const getActions = (snippets) => {
|
|
|
|
return snippets.map((snippetName) => this.acl.snippetManager.snippets.get(snippetName).actions).flat();
|
|
|
|
};
|
|
|
|
|
|
|
|
const allowedActions = getActions(effectiveSnippets.allowed);
|
|
|
|
const rejectedActions = getActions(effectiveSnippets.rejected);
|
|
|
|
|
|
|
|
const actionMatched = (actionPath, actionRule) => {
|
|
|
|
return minimatch(actionPath, actionRule);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const action of allowedActions) {
|
|
|
|
if (actionMatched(actionPath, action)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const action of rejectedActions) {
|
|
|
|
if (actionMatched(actionPath, action)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
public toJSON(): DefineOptions {
|
|
|
|
const actions = {};
|
|
|
|
|
|
|
|
for (const resourceName of this.resources.keys()) {
|
|
|
|
const resourceActions = this.getResourceActionsParams(resourceName);
|
|
|
|
for (const actionName of Object.keys(resourceActions)) {
|
|
|
|
actions[`${resourceName}:${actionName}`] = resourceActions[actionName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
role: this.name,
|
|
|
|
strategy: this.strategy,
|
|
|
|
actions,
|
2023-01-09 07:35:48 +08:00
|
|
|
snippets: Array.from(this.snippets),
|
2022-01-18 16:38:03 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getResourceActionFromPath(path: string) {
|
|
|
|
const [resourceName, actionName] = path.split(':');
|
|
|
|
|
|
|
|
const resource = this.resources.get(resourceName);
|
2022-01-30 10:37:27 +08:00
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
let action = null;
|
|
|
|
if (resource) {
|
|
|
|
action = resource.getAction(actionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
resourceName,
|
|
|
|
actionName,
|
|
|
|
resource,
|
|
|
|
action,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|