2022-01-24 14:10:35 +08:00
|
|
|
import { ACL } from '@nocobase/acl';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-01-24 14:10:35 +08:00
|
|
|
import path from 'path';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { createACL } from './acl';
|
2022-01-24 14:10:35 +08:00
|
|
|
import { availableActionResource } from './actions/available-actions';
|
|
|
|
import { roleCollectionsResource } from './actions/role-collections';
|
2022-01-30 10:37:27 +08:00
|
|
|
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
|
|
|
import { RoleResourceModel } from './model/RoleResourceModel';
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
export interface AssociationFieldAction {
|
|
|
|
associationActions: string[];
|
|
|
|
targetActions?: string[];
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
interface AssociationFieldActions {
|
|
|
|
[availableActionName: string]: AssociationFieldAction;
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
export interface AssociationFieldsActions {
|
|
|
|
[associationType: string]: AssociationFieldActions;
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
export class GrantHelper {
|
|
|
|
resourceTargetActionMap = new Map<string, string[]>();
|
|
|
|
targetActionResourceMap = new Map<string, string[]>();
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
constructor() {}
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-02-11 18:13:14 +08:00
|
|
|
export class PluginACL extends Plugin {
|
2022-01-24 14:10:35 +08:00
|
|
|
acl: ACL;
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
associationFieldsActions: AssociationFieldsActions = {};
|
|
|
|
|
|
|
|
grantHelper = new GrantHelper();
|
|
|
|
|
|
|
|
registerAssociationFieldAction(associationType: string, value: AssociationFieldActions) {
|
|
|
|
this.associationFieldsActions[associationType] = value;
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
getACL() {
|
|
|
|
return this.acl;
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:37:27 +08: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 21:47:26 +08: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-07 01:14:00 +08:00
|
|
|
async beforeLoad() {
|
2022-01-24 14:10:35 +08:00
|
|
|
const acl = createACL();
|
|
|
|
this.acl = acl;
|
|
|
|
|
2022-02-11 19:31:53 +08:00
|
|
|
// @ts-ignore
|
|
|
|
this.app.acl = acl;
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
this.app.db.registerModels({
|
|
|
|
RoleResourceActionModel,
|
|
|
|
RoleResourceModel,
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
this.registerAssociationFieldsActions();
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
this.app.resourcer.define(availableActionResource);
|
|
|
|
this.app.resourcer.define(roleCollectionsResource);
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
this.app.db.on('roles.afterSave', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
2022-01-24 14:10:35 +08:00
|
|
|
const roleName = model.get('name');
|
|
|
|
let role = acl.getRole(roleName);
|
|
|
|
|
|
|
|
if (!role) {
|
|
|
|
role = acl.define({
|
|
|
|
role: model.get('name'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
role.setStrategy({
|
|
|
|
...(model.get('strategy') || {}),
|
|
|
|
allowConfigure: model.get('allowConfigure'),
|
|
|
|
});
|
2022-01-30 10:37:27 +08: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 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.app.db.on('roles.afterDestroy', (model) => {
|
|
|
|
const roleName = model.get('name');
|
|
|
|
acl.removeRole(roleName);
|
|
|
|
});
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
this.app.db.on('rolesResources.afterSaveWithAssociations', async (model: RoleResourceModel, options) => {
|
2022-02-11 21:47:26 +08:00
|
|
|
await this.writeResourceToACL(model, options.transaction);
|
2022-01-30 10:37:27 +08:00
|
|
|
});
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
this.app.db.on('rolesResourcesActions.afterUpdateWithAssociations', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
const resource = await model.getResource({
|
|
|
|
transaction,
|
|
|
|
});
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-02-11 21:47:26 +08:00
|
|
|
await this.writeResourceToACL(resource, transaction);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
this.app.db.on('fields.afterDestroy', async (model, options) => {
|
|
|
|
const collectionName = model.get('collectionName');
|
|
|
|
const fieldName = model.get('name');
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-30 10:37:27 +08: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 14:10:35 +08:00
|
|
|
},
|
2022-01-30 10:37:27 +08:00
|
|
|
transaction: options.transaction,
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.app.db.import({
|
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.resourcer.use(this.acl.middleware());
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
2022-02-11 18:13:14 +08:00
|
|
|
|
|
|
|
export default PluginACL;
|