2022-02-11 10:13:14 +00:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-02-11 15:59:03 +00:00
|
|
|
import { resolve } from 'path';
|
2022-01-24 06:10:35 +00:00
|
|
|
import { availableActionResource } from './actions/available-actions';
|
2022-03-13 11:36:37 +00:00
|
|
|
import { checkAction } from './actions/role-check';
|
2022-04-08 08:17:39 +00:00
|
|
|
import { roleCollectionsResource } from './actions/role-collections';
|
2022-02-28 06:25:50 +00:00
|
|
|
import { RoleModel } from './model/RoleModel';
|
2022-01-30 02:37:27 +00:00
|
|
|
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
|
|
|
import { RoleResourceModel } from './model/RoleResourceModel';
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
export interface AssociationFieldAction {
|
|
|
|
associationActions: string[];
|
|
|
|
targetActions?: string[];
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
interface AssociationFieldActions {
|
|
|
|
[availableActionName: string]: AssociationFieldAction;
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
export interface AssociationFieldsActions {
|
|
|
|
[associationType: string]: AssociationFieldActions;
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
export class GrantHelper {
|
|
|
|
resourceTargetActionMap = new Map<string, string[]>();
|
|
|
|
targetActionResourceMap = new Map<string, string[]>();
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
constructor() {}
|
2022-01-24 06:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:13:14 +00:00
|
|
|
export class PluginACL extends Plugin {
|
2022-01-30 02:37:27 +00:00
|
|
|
associationFieldsActions: AssociationFieldsActions = {};
|
|
|
|
|
|
|
|
grantHelper = new GrantHelper();
|
|
|
|
|
2022-02-11 15:59:03 +00:00
|
|
|
get acl() {
|
|
|
|
return this.app.acl;
|
2022-01-30 02:37:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 15:59:03 +00:00
|
|
|
registerAssociationFieldAction(associationType: string, value: AssociationFieldActions) {
|
|
|
|
this.associationFieldsActions[associationType] = value;
|
2022-01-24 06:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
registerAssociationFieldsActions() {
|
|
|
|
this.registerAssociationFieldAction('linkTo', {
|
|
|
|
view: {
|
|
|
|
associationActions: ['list', 'get'],
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
associationActions: ['add'],
|
|
|
|
targetActions: ['view'],
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
associationActions: ['add', 'remove', 'toggle'],
|
|
|
|
targetActions: ['view'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.registerAssociationFieldAction('attachments', {
|
|
|
|
view: {
|
|
|
|
associationActions: ['list', 'get'],
|
|
|
|
},
|
|
|
|
add: {
|
|
|
|
associationActions: ['upload', 'add'],
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
associationActions: ['update', 'add', 'remove', 'toggle'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.registerAssociationFieldAction('subTable', {
|
|
|
|
view: {
|
|
|
|
associationActions: ['list', 'get'],
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
associationActions: ['create'],
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
associationActions: ['update', 'destroy'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:47:26 +00:00
|
|
|
async writeResourceToACL(resourceModel: RoleResourceModel, transaction) {
|
|
|
|
await resourceModel.writeToACL({
|
|
|
|
acl: this.acl,
|
|
|
|
associationFieldsActions: this.associationFieldsActions,
|
|
|
|
transaction: transaction,
|
|
|
|
grantHelper: this.grantHelper,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeActionToACL(actionModel: RoleResourceActionModel, transaction) {
|
|
|
|
const resource = actionModel.get('resource') as RoleResourceModel;
|
|
|
|
const role = this.acl.getRole(resource.get('roleName') as string);
|
|
|
|
await actionModel.writeToACL({
|
|
|
|
acl: this.acl,
|
|
|
|
role,
|
|
|
|
resourceName: resource.get('name') as string,
|
|
|
|
associationFieldsActions: this.associationFieldsActions,
|
|
|
|
grantHelper: this.grantHelper,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
async writeRolesToACL() {
|
|
|
|
const roles = (await this.app.db.getRepository('roles').find({
|
|
|
|
appends: ['resources', 'resources.actions'],
|
|
|
|
})) as RoleModel[];
|
|
|
|
for (const role of roles) {
|
|
|
|
role.writeToAcl({ acl: this.acl });
|
|
|
|
for (const resource of role.get('resources') as RoleResourceModel[]) {
|
|
|
|
await this.writeResourceToACL(resource, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 11:31:53 +00:00
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
async beforeLoad() {
|
2022-02-06 17:14:00 +00:00
|
|
|
this.app.db.registerModels({
|
|
|
|
RoleResourceActionModel,
|
|
|
|
RoleResourceModel,
|
2022-02-19 17:23:04 +00:00
|
|
|
RoleModel,
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
this.registerAssociationFieldsActions();
|
|
|
|
|
2022-01-24 06:10:35 +00:00
|
|
|
this.app.resourcer.define(availableActionResource);
|
|
|
|
this.app.resourcer.define(roleCollectionsResource);
|
|
|
|
|
2022-03-13 11:36:37 +00:00
|
|
|
this.app.resourcer.registerActionHandler('roles:check', checkAction);
|
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
this.app.db.on('roles.afterSave', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
model.writeToAcl({
|
|
|
|
acl: this.acl,
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
2022-01-30 02:37:27 +00:00
|
|
|
|
|
|
|
// model is default
|
|
|
|
if (model.get('default')) {
|
|
|
|
await this.app.db.getRepository('roles').update({
|
|
|
|
values: {
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
'name.$ne': model.get('name'),
|
|
|
|
},
|
|
|
|
hooks: false,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.app.db.on('roles.afterDestroy', (model) => {
|
|
|
|
const roleName = model.get('name');
|
2022-02-11 15:59:03 +00:00
|
|
|
this.acl.removeRole(roleName);
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
this.app.db.on('rolesResources.afterSaveWithAssociations', async (model: RoleResourceModel, options) => {
|
2022-02-11 13:47:26 +00:00
|
|
|
await this.writeResourceToACL(model, options.transaction);
|
2022-01-30 02:37:27 +00:00
|
|
|
});
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
this.app.db.on('rolesResourcesActions.afterUpdateWithAssociations', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
const resource = await model.getResource({
|
|
|
|
transaction,
|
|
|
|
});
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-02-11 13:47:26 +00:00
|
|
|
await this.writeResourceToACL(resource, transaction);
|
|
|
|
});
|
|
|
|
|
2022-02-11 14:00:11 +00:00
|
|
|
this.app.db.on('rolesResources.afterDestroy', async (model, options) => {
|
|
|
|
const role = this.acl.getRole(model.get('roleName'));
|
|
|
|
role.revokeResource(model.get('name'));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.db.on('collections.afterDestroy', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
await this.app.db.getRepository('rolesResources').destroy({
|
|
|
|
filter: {
|
|
|
|
name: model.get('name'),
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-11 13:47:26 +00:00
|
|
|
this.app.db.on('fields.afterCreate', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const collectionName = model.get('collectionName');
|
|
|
|
const fieldName = model.get('name');
|
|
|
|
|
|
|
|
const resourceActions = (await this.app.db.getRepository('rolesResourcesActions').find({
|
|
|
|
filter: {
|
|
|
|
'resource.name': collectionName,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
appends: ['resource'],
|
|
|
|
})) as RoleResourceActionModel[];
|
|
|
|
|
|
|
|
for (const resourceAction of resourceActions) {
|
|
|
|
const fields = resourceAction.get('fields') as string[];
|
|
|
|
const newFields = [...fields, fieldName];
|
|
|
|
|
|
|
|
await this.app.db.getRepository('rolesResourcesActions').update({
|
|
|
|
filterByTk: resourceAction.get('id') as number,
|
|
|
|
values: {
|
|
|
|
fields: newFields,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
this.app.db.on('fields.afterDestroy', async (model, options) => {
|
|
|
|
const collectionName = model.get('collectionName');
|
|
|
|
const fieldName = model.get('name');
|
2022-01-24 06:10:35 +00:00
|
|
|
|
2022-01-30 02:37:27 +00:00
|
|
|
const resourceActions = await this.app.db.getRepository('rolesResourcesActions').find({
|
|
|
|
filter: {
|
|
|
|
'resource.name': collectionName,
|
|
|
|
'fields.$anyOf': [fieldName],
|
|
|
|
},
|
|
|
|
transaction: options.transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const resourceAction of resourceActions) {
|
|
|
|
const fields = resourceAction.get('fields') as string[];
|
|
|
|
const newFields = fields.filter((field) => field != fieldName);
|
|
|
|
|
|
|
|
await this.app.db.getRepository('rolesResourcesActions').update({
|
|
|
|
filterByTk: resourceAction.get('id') as number,
|
|
|
|
values: {
|
|
|
|
fields: newFields,
|
2022-01-24 06:10:35 +00:00
|
|
|
},
|
2022-01-30 02:37:27 +00:00
|
|
|
transaction: options.transaction,
|
2022-01-24 06:10:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-02-19 17:23:04 +00:00
|
|
|
|
|
|
|
// sync database role data to acl
|
|
|
|
this.app.on('beforeStart', async () => {
|
|
|
|
await this.writeRolesToACL();
|
|
|
|
});
|
2022-02-28 06:25:50 +00:00
|
|
|
|
2022-02-28 14:10:04 +00:00
|
|
|
this.app.on('beforeInstallPlugin', async (plugin) => {
|
|
|
|
if (plugin.constructor.name !== 'UsersPlugin') {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-10 11:22:39 +00:00
|
|
|
const roles = this.app.db.getRepository('roles');
|
|
|
|
await roles.createMany({
|
2022-02-28 06:25:50 +00:00
|
|
|
records: [
|
2022-04-10 11:22:39 +00:00
|
|
|
{
|
|
|
|
name: 'root',
|
|
|
|
title: 'Root',
|
|
|
|
hidden: true,
|
|
|
|
},
|
2022-02-28 06:25:50 +00:00
|
|
|
{
|
|
|
|
name: 'admin',
|
|
|
|
title: 'Admin',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'member',
|
|
|
|
title: 'Member',
|
|
|
|
default: true,
|
|
|
|
},
|
2022-04-08 08:17:39 +00:00
|
|
|
{
|
|
|
|
name: 'anonymous',
|
|
|
|
title: 'Anonymous',
|
|
|
|
},
|
2022-02-28 06:25:50 +00:00
|
|
|
],
|
|
|
|
});
|
2022-04-10 11:22:39 +00:00
|
|
|
const rolesResourcesScopes = this.app.db.getRepository('rolesResourcesScopes');
|
|
|
|
await rolesResourcesScopes.createMany({
|
|
|
|
records: [
|
|
|
|
{
|
|
|
|
name: '{{t("All records")}}',
|
|
|
|
scope: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '{{t("Own records")}}',
|
|
|
|
scope: {
|
|
|
|
createdById: '{{ ctx.state.currentUser.id }}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2022-02-28 06:25:50 +00:00
|
|
|
});
|
2022-04-10 11:22:39 +00:00
|
|
|
this.app.acl.skip('roles.menuUiSchemas', 'set', 'logged-in');
|
|
|
|
this.app.acl.skip('roles.menuUiSchemas', 'toggle', 'logged-in');
|
|
|
|
this.app.acl.skip('roles.menuUiSchemas', 'list', 'logged-in');
|
2022-03-13 11:36:37 +00:00
|
|
|
this.app.acl.skip('roles', 'check', 'logged-in');
|
2022-04-10 11:22:39 +00:00
|
|
|
this.app.acl.skip('*', '*', (ctx) => {
|
|
|
|
return ctx.state.currentRole === 'root';
|
|
|
|
});
|
|
|
|
|
|
|
|
// root role
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
|
|
const { actionName, resourceName } = ctx.action.params;
|
|
|
|
if (actionName === 'list' && resourceName === 'roles') {
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
filter: {
|
|
|
|
'name.$ne': 'root',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
2022-01-24 06:10:35 +00:00
|
|
|
}
|
2022-02-06 17:14:00 +00:00
|
|
|
|
2022-03-02 10:35:49 +00:00
|
|
|
async install() {
|
|
|
|
const repo = this.db.getRepository<any>('collections');
|
|
|
|
if (repo) {
|
|
|
|
await repo.db2cm('roles');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
async load() {
|
|
|
|
await this.app.db.import({
|
2022-02-11 15:59:03 +00:00
|
|
|
directory: resolve(__dirname, 'collections'),
|
2022-02-06 17:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.app.resourcer.use(this.acl.middleware());
|
|
|
|
}
|
2022-03-28 14:01:10 +00:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-01-24 06:10:35 +00:00
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
|
|
|
|
export default PluginACL;
|