tachybase_todo/packages/acl/src/acl-resource.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

66 lines
1.6 KiB
TypeScript

import { ACLRole, RoleActionParams } from './acl-role';
import { ACL, ListenerContext } from './acl';
export type ResourceActions = { [key: string]: RoleActionParams };
interface AclResourceOptions {
name: string;
role: ACLRole;
actions?: ResourceActions;
}
export class ACLResource {
actions = new Map<string, RoleActionParams>();
acl: ACL;
role: ACLRole;
name: string;
constructor(options: AclResourceOptions) {
this.acl = options.role.acl;
this.role = options.role;
this.name = options.name;
const actionsOption: ResourceActions = options.actions || {};
for (const actionName of Object.keys(actionsOption)) {
this.actions.set(actionName, actionsOption[actionName]);
}
}
getActions() {
return Array.from(this.actions.keys()).reduce((carry, key) => {
carry[key] = this.actions.get(key);
return carry;
}, {});
}
getAction(name: string) {
return this.actions.get(name) || this.actions.get(this.acl.resolveActionAlias(name));
}
setAction(name: string, params: RoleActionParams) {
const context: ListenerContext = {
role: this.role,
acl: this.role.acl,
params: params || {},
path: `${this.name}:${name}`,
resourceName: this.name,
actionName: name,
};
this.acl.emit('beforeGrantAction', context);
this.actions.set(name, context.params);
}
setActions(actions: { [key: string]: RoleActionParams }) {
for (const actionName of Object.keys(actions)) {
this.setAction(actionName, actions[actionName]);
}
}
removeAction(name: string) {
this.actions.delete(name);
}
}