nocobase/packages/acl/src/acl-role.ts
ChengLei Shao 7a7ab2ef41
feat: add acl plugin (#169)
* feat: getRepository

* getRepository return type

* export action

* add: acl

* feat: setResourceAction

* feat: action alias

* chore: code struct

* feat: removeResourceAction

* chore: file name

* ignorecase

* remove ACL

* feat: ACL

* feat: role toJSON

* using emit

* chore: test

* feat: plugin-acl

* feat: acl with predicate

* grant universal action test

* grant action test

* update resource action test

* revoke resource action

* usingActionsConfig switch

* plugin-ui-schema-storage

* remove global acl instance

* fix: collection manager with sqlite

* add own action listener

* add acl middleware

* add acl allowConfigure strategy option

* add plugin-acl allowConfigure

* change acl resourceName

* add acl middleware merge params

* bugfix

* append fields on acl action params

* acl middleware parse template

* fix: collection-manager migrate

* add acl association field test

* feat(plugin-acl): grant association field actions

* chore(plugin-acl): type name

* feat(plugin-acl): regrant actions on resource action update

* feat(plugin-acl): regrant action on field destroy

* fix(plugin-acl): test

* fix(plugin-acl): test run

* feat(plugin-acl): set default role

* feat(plugin-users): set user default role

* test(plugin-users): create user with role

* feat(plugin-users): create user with role

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 10:37:27 +08:00

122 lines
3.0 KiB
TypeScript

import { ACLResource } from './acl-resource';
import { AvailableStrategyOptions } from './acl-available-strategy';
import { ACL, DefineOptions } from './acl';
export interface RoleActionParams {
fields?: string[];
filter?: any;
own?: boolean;
whitelist?: string[];
blacklist?: string[];
[key: string]: any;
}
interface ResourceActionsOptions {
[actionName: string]: RoleActionParams;
}
export class ACLRole {
strategy: string | AvailableStrategyOptions;
resources = new Map<string, ACLResource>();
constructor(public acl: ACL, public name: string) {}
getResource(name: string): ACLResource | undefined {
return this.resources.get(name);
}
setResource(name: string, resource: ACLResource) {
this.resources.set(name, resource);
}
public setStrategy(value: string | AvailableStrategyOptions) {
this.strategy = value;
}
public grantResource(resourceName: string, options: ResourceActionsOptions) {
const resource = new ACLResource({
role: this,
name: resourceName,
});
for (const [actionName, actionParams] of Object.entries(options)) {
resource.setAction(actionName, actionParams);
}
this.resources.set(resourceName, resource);
}
public getResourceActionsParams(resourceName: string) {
const resource = this.getResource(resourceName);
return resource.getActions();
}
public revokeResource(resourceName) {
for (const key of [...this.resources.keys()]) {
if (key === resourceName || key.includes(`${resourceName}.`)) {
this.resources.delete(key);
}
}
}
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);
}
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,
};
}
protected getResourceActionFromPath(path: string) {
const [resourceName, actionName] = path.split(':');
const resource = this.resources.get(resourceName);
let action = null;
if (resource) {
action = resource.getAction(actionName);
}
return {
resourceName,
actionName,
resource,
action,
};
}
}