2023-01-09 07:35:48 +08:00
|
|
|
import { Context, utils as actionUtils } from '@nocobase/actions';
|
2024-01-08 18:59:56 +08:00
|
|
|
import { Cache } from '@nocobase/cache';
|
2024-03-03 23:06:24 +08:00
|
|
|
import { Collection, RelationField } from '@nocobase/database';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2023-09-12 22:39:23 +08:00
|
|
|
import { Mutex } from 'async-mutex';
|
2023-08-02 00:07:52 +08:00
|
|
|
import lodash from 'lodash';
|
2022-02-11 23:59:03 +08:00
|
|
|
import { resolve } from 'path';
|
2022-01-24 14:10:35 +08:00
|
|
|
import { availableActionResource } from './actions/available-actions';
|
2022-03-13 19:36:37 +08:00
|
|
|
import { checkAction } from './actions/role-check';
|
2022-04-08 16:17:39 +08:00
|
|
|
import { roleCollectionsResource } from './actions/role-collections';
|
2022-07-25 19:33:23 +08:00
|
|
|
import { setDefaultRole } from './actions/user-setDefaultRole';
|
|
|
|
import { setCurrentRole } from './middlewares/setCurrentRole';
|
2024-03-17 23:30:32 +08:00
|
|
|
import { createWithACLMetaMiddleware } from './middlewares/with-acl-meta';
|
2022-02-28 14:25:50 +08:00
|
|
|
import { RoleModel } from './model/RoleModel';
|
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-05-04 20:44:59 +08:00
|
|
|
// association field actions config
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
associationFieldsActions: AssociationFieldsActions = {};
|
|
|
|
|
|
|
|
grantHelper = new GrantHelper();
|
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
get acl() {
|
|
|
|
return this.app.acl;
|
2022-01-30 10:37:27 +08:00
|
|
|
}
|
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
registerAssociationFieldAction(associationType: string, value: AssociationFieldActions) {
|
|
|
|
this.associationFieldsActions[associationType] = value;
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:37:27 +08:00
|
|
|
registerAssociationFieldsActions() {
|
2022-05-04 20:44:59 +08:00
|
|
|
// if grant create action to role, it should
|
|
|
|
// also grant add action and association target's view action
|
2023-01-09 07:35:48 +08:00
|
|
|
|
|
|
|
this.registerAssociationFieldAction('hasOne', {
|
2022-01-30 10:37:27 +08:00
|
|
|
view: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['list', 'get', 'view'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
create: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['create', 'set'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
update: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['update', 'remove', 'set'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.registerAssociationFieldAction('hasMany', {
|
2022-01-30 10:37:27 +08:00
|
|
|
view: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['list', 'get', 'view'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
2023-01-09 07:35:48 +08:00
|
|
|
create: {
|
|
|
|
associationActions: ['create', 'set', 'add'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
update: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['update', 'remove', 'set'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.registerAssociationFieldAction('belongsTo', {
|
2022-01-30 10:37:27 +08:00
|
|
|
view: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['list', 'get', 'view'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
create: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['create', 'set'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
update: {
|
2023-01-09 07:35:48 +08:00
|
|
|
associationActions: ['update', 'remove', 'set'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.registerAssociationFieldAction('belongsToMany', {
|
|
|
|
view: {
|
|
|
|
associationActions: ['list', 'get', 'view'],
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
associationActions: ['create', 'set', 'add'],
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
associationActions: ['update', 'remove', 'set', 'toggle'],
|
2022-01-30 10:37:27 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
async writeRolesToACL(options) {
|
2022-02-20 01:23:04 +08:00
|
|
|
const roles = (await this.app.db.getRepository('roles').find({
|
|
|
|
appends: ['resources', 'resources.actions'],
|
|
|
|
})) as RoleModel[];
|
2022-05-04 20:44:59 +08:00
|
|
|
|
2022-02-20 01:23:04 +08:00
|
|
|
for (const role of roles) {
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.writeRoleToACL(role, options);
|
2023-06-07 15:01:50 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
async writeRoleToACL(role: RoleModel, options: any = {}) {
|
|
|
|
const transaction = options?.transaction;
|
|
|
|
|
|
|
|
role.writeToAcl({ acl: this.acl, withOutStrategy: true });
|
|
|
|
|
|
|
|
if (options.withOutResources) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-07 15:01:50 +08:00
|
|
|
|
|
|
|
let resources = role.get('resources') as RoleResourceModel[];
|
|
|
|
|
|
|
|
if (!resources) {
|
|
|
|
resources = await role.getResources({ transaction });
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const resource of resources as RoleResourceModel[]) {
|
|
|
|
await this.writeResourceToACL(resource, transaction);
|
2022-02-20 01:23:04 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-11 19:31:53 +08:00
|
|
|
|
2022-02-20 01:23:04 +08:00
|
|
|
async beforeLoad() {
|
2023-01-09 07:35:48 +08:00
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: this.name,
|
|
|
|
directory: resolve(__dirname, './migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
this.app.db.registerModels({
|
|
|
|
RoleResourceActionModel,
|
|
|
|
RoleResourceModel,
|
2022-02-20 01:23:04 +08:00
|
|
|
RoleModel,
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.acl.registerSnippet({
|
|
|
|
name: `pm.${this.name}.roles`,
|
|
|
|
actions: [
|
|
|
|
'roles:*',
|
|
|
|
'roles.snippets:*',
|
|
|
|
'availableActions:list',
|
|
|
|
'roles.collections:list',
|
|
|
|
'roles.resources:*',
|
|
|
|
'uiSchemas:getProperties',
|
|
|
|
'roles.menuUiSchemas:*',
|
2024-03-17 23:30:32 +08:00
|
|
|
'roles.users:*',
|
2023-01-09 07:35:48 +08:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// change resource fields to association fields
|
|
|
|
this.app.acl.beforeGrantAction((ctx) => {
|
|
|
|
const actionName = this.app.acl.resolveActionAlias(ctx.actionName);
|
|
|
|
const collection = this.app.db.getCollection(ctx.resourceName);
|
|
|
|
|
|
|
|
if (!collection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fieldsParams = ctx.params.fields;
|
|
|
|
|
|
|
|
if (!fieldsParams) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actionName == 'view' || actionName == 'export') {
|
|
|
|
const associationsFields = fieldsParams.filter((fieldName) => {
|
|
|
|
const field = collection.getField(fieldName);
|
|
|
|
return field instanceof RelationField;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.params = {
|
|
|
|
...ctx.params,
|
|
|
|
fields: lodash.difference(fieldsParams, associationsFields),
|
|
|
|
appends: associationsFields,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-03-13 19:36:37 +08:00
|
|
|
this.app.resourcer.registerActionHandler('roles:check', checkAction);
|
|
|
|
|
2022-07-25 19:33:23 +08:00
|
|
|
this.app.resourcer.registerActionHandler(`users:setDefaultRole`, setDefaultRole);
|
|
|
|
|
|
|
|
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
const repository = this.app.db.getRepository('roles');
|
|
|
|
const defaultRole = await repository.findOne({
|
|
|
|
filter: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-07-25 19:33:23 +08:00
|
|
|
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
|
|
|
await model.addRoles(defaultRole, { transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-07 15:01:50 +08:00
|
|
|
this.app.on('acl:writeRoleToACL', async (roleModel: RoleModel) => {
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.writeRoleToACL(roleModel, {
|
|
|
|
withOutResources: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.app.db.getRepository('dataSourcesRoles').updateOrCreate({
|
|
|
|
values: {
|
|
|
|
roleName: roleModel.get('name'),
|
|
|
|
dataSourceKey: 'main',
|
|
|
|
strategy: roleModel.get('strategy'),
|
|
|
|
},
|
|
|
|
filterKeys: ['roleName', 'dataSourceKey'],
|
|
|
|
});
|
2023-06-07 15:01:50 +08:00
|
|
|
});
|
|
|
|
|
2022-04-11 10:09:55 +08:00
|
|
|
this.app.db.on('roles.afterSaveWithAssociations', async (model, options) => {
|
2022-01-30 10:37:27 +08:00
|
|
|
const { transaction } = options;
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.writeRoleToACL(model, {
|
|
|
|
withOutResources: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// this will update or create a record in dataSourcesRoles
|
|
|
|
await this.app.db.getRepository('dataSourcesRoles').updateOrCreate({
|
|
|
|
values: {
|
|
|
|
roleName: model.get('name'),
|
|
|
|
dataSourceKey: 'main',
|
|
|
|
strategy: model.get('strategy'),
|
|
|
|
},
|
|
|
|
filterKeys: ['roleName', 'dataSourceKey'],
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.app.emitAsync('acl:writeResources', {
|
|
|
|
roleName: model.get('name'),
|
|
|
|
transaction,
|
|
|
|
});
|
2022-04-11 10:09:55 +08:00
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
// role is default
|
2022-01-30 10:37:27 +08:00
|
|
|
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');
|
2022-02-11 23:59:03 +08:00
|
|
|
this.acl.removeRole(roleName);
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2022-02-11 22:00:11 +08:00
|
|
|
this.app.db.on('collections.afterDestroy', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.app.db.getRepository('dataSourcesRolesResources').destroy({
|
2022-02-11 22:00:11 +08:00
|
|
|
filter: {
|
|
|
|
name: model.get('name'),
|
2024-03-03 23:06:24 +08:00
|
|
|
dataSourceKey: 'main',
|
2022-02-11 22:00:11 +08:00
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-11 21:47:26 +08:00
|
|
|
this.app.db.on('fields.afterCreate', async (model, options) => {
|
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const collectionName = model.get('collectionName');
|
2023-02-13 21:38:47 +08:00
|
|
|
|
2022-02-11 21:47:26 +08:00
|
|
|
const fieldName = model.get('name');
|
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
const resourceActions = await this.app.db.getRepository('dataSourcesRolesResourcesActions').find({
|
2022-02-11 21:47:26 +08:00
|
|
|
filter: {
|
|
|
|
'resource.name': collectionName,
|
2024-03-03 23:06:24 +08:00
|
|
|
'resource.dataSourceKey': 'main',
|
2022-02-11 21:47:26 +08:00
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
appends: ['resource'],
|
2024-03-03 23:06:24 +08:00
|
|
|
});
|
2022-02-11 21:47:26 +08:00
|
|
|
|
|
|
|
for (const resourceAction of resourceActions) {
|
|
|
|
const fields = resourceAction.get('fields') as string[];
|
|
|
|
const newFields = [...fields, fieldName];
|
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.app.db.getRepository('dataSourcesRolesResourcesActions').update({
|
|
|
|
filterByTk: resourceAction.get('id'),
|
2022-02-11 21:47:26 +08:00
|
|
|
values: {
|
|
|
|
fields: newFields,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
|
|
|
|
2023-09-01 13:51:48 +08:00
|
|
|
const mutex = new Mutex();
|
2022-01-30 10:37:27 +08:00
|
|
|
|
2023-09-01 13:51:48 +08:00
|
|
|
this.app.db.on('fields.afterDestroy', async (model, options) => {
|
|
|
|
await mutex.runExclusive(async () => {
|
|
|
|
const collectionName = model.get('collectionName');
|
|
|
|
const fieldName = model.get('name');
|
2022-01-30 10:37:27 +08:00
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
const resourceActions = await this.app.db.getRepository('dataSourcesRolesResourcesActions').find({
|
2023-09-01 13:51:48 +08:00
|
|
|
filter: {
|
|
|
|
'resource.name': collectionName,
|
|
|
|
'fields.$anyOf': [fieldName],
|
2024-03-03 23:06:24 +08:00
|
|
|
'resource.dataSourceKey': 'main',
|
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
|
|
|
});
|
2023-09-01 13:51:48 +08:00
|
|
|
|
|
|
|
for (const resourceAction of resourceActions) {
|
|
|
|
const fields = resourceAction.get('fields') as string[];
|
|
|
|
const newFields = fields.filter((field) => field != fieldName);
|
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.app.db.getRepository('dataSourcesRolesResourcesActions').update({
|
2023-09-01 13:51:48 +08:00
|
|
|
filterByTk: resourceAction.get('id') as number,
|
|
|
|
values: {
|
|
|
|
fields: newFields,
|
|
|
|
},
|
|
|
|
transaction: options.transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-01-24 14:10:35 +08:00
|
|
|
});
|
2022-02-20 01:23:04 +08:00
|
|
|
|
2023-12-12 23:02:09 +08:00
|
|
|
// Delete cache when the roles of a user changed
|
|
|
|
this.app.db.on('rolesUsers.afterSave', async (model) => {
|
|
|
|
const cache = this.app.cache as Cache;
|
|
|
|
await cache.del(`roles:${model.get('userId')}`);
|
|
|
|
});
|
|
|
|
this.app.db.on('rolesUsers.afterDestroy', async (model) => {
|
|
|
|
const cache = this.app.cache as Cache;
|
|
|
|
await cache.del(`roles:${model.get('userId')}`);
|
|
|
|
});
|
|
|
|
|
2023-08-24 17:47:45 +08:00
|
|
|
const writeRolesToACL = async (app, options) => {
|
2022-10-27 15:32:58 +08:00
|
|
|
const exists = await this.app.db.collectionExistsInDb('roles');
|
|
|
|
if (exists) {
|
2023-12-27 13:56:13 +08:00
|
|
|
this.log.info('write roles to ACL', { method: 'writeRolesToACL' });
|
2024-03-03 23:06:24 +08:00
|
|
|
await this.writeRolesToACL(options);
|
2022-10-27 15:27:08 +08:00
|
|
|
}
|
2023-08-24 17:47:45 +08:00
|
|
|
};
|
2022-02-28 14:25:50 +08:00
|
|
|
|
2023-08-24 17:47:45 +08:00
|
|
|
// sync database role data to acl
|
2024-03-20 14:40:11 +08:00
|
|
|
this.app.on('afterStart', async () => {
|
2024-03-03 23:06:24 +08:00
|
|
|
await writeRolesToACL(this.app, {
|
|
|
|
withOutResources: true,
|
|
|
|
});
|
|
|
|
});
|
2024-03-20 14:40:11 +08:00
|
|
|
|
2024-01-08 19:05:14 +08:00
|
|
|
// this.app.on('afterInstall', writeRolesToACL);
|
2022-10-28 15:09:14 +08:00
|
|
|
|
2022-10-29 14:07:51 +08:00
|
|
|
this.app.on('afterInstallPlugin', async (plugin) => {
|
|
|
|
if (plugin.getName() !== 'users') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const User = this.db.getCollection('users');
|
|
|
|
await User.repository.update({
|
|
|
|
values: {
|
|
|
|
roles: ['root', 'admin', 'member'],
|
|
|
|
},
|
|
|
|
forceUpdate: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const RolesUsers = this.db.getCollection('rolesUsers');
|
|
|
|
await RolesUsers.repository.update({
|
|
|
|
filter: {
|
|
|
|
userId: 1,
|
|
|
|
roleName: 'root',
|
|
|
|
},
|
|
|
|
values: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-28 22:10:04 +08:00
|
|
|
this.app.on('beforeInstallPlugin', async (plugin) => {
|
2022-10-29 14:07:51 +08:00
|
|
|
if (plugin.getName() !== 'users') {
|
2022-02-28 22:10:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-04-10 19:22:39 +08:00
|
|
|
const roles = this.app.db.getRepository('roles');
|
|
|
|
await roles.createMany({
|
2022-02-28 14:25:50 +08:00
|
|
|
records: [
|
2022-04-10 19:22:39 +08:00
|
|
|
{
|
|
|
|
name: 'root',
|
2022-04-22 23:58:19 +08:00
|
|
|
title: '{{t("Root")}}',
|
2022-04-10 19:22:39 +08:00
|
|
|
hidden: true,
|
2023-01-09 07:35:48 +08:00
|
|
|
snippets: ['ui.*', 'pm', 'pm.*'],
|
2022-04-10 19:22:39 +08:00
|
|
|
},
|
2022-02-28 14:25:50 +08:00
|
|
|
{
|
|
|
|
name: 'admin',
|
2022-04-22 23:58:19 +08:00
|
|
|
title: '{{t("Admin")}}',
|
2022-04-12 12:02:58 +08:00
|
|
|
allowConfigure: true,
|
|
|
|
allowNewMenu: true,
|
2022-07-25 19:33:23 +08:00
|
|
|
strategy: { actions: ['create', 'view', 'update', 'destroy'] },
|
2023-01-09 07:35:48 +08:00
|
|
|
snippets: ['ui.*', 'pm', 'pm.*'],
|
2022-02-28 14:25:50 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'member',
|
2022-04-22 23:58:19 +08:00
|
|
|
title: '{{t("Member")}}',
|
2022-04-12 12:02:58 +08:00
|
|
|
allowNewMenu: true,
|
|
|
|
strategy: { actions: ['view', 'update:own', 'destroy:own', 'create'] },
|
2022-02-28 14:25:50 +08:00
|
|
|
default: true,
|
2023-01-09 07:35:48 +08:00
|
|
|
snippets: ['!ui.*', '!pm', '!pm.*'],
|
2022-02-28 14:25:50 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2024-03-03 23:06:24 +08:00
|
|
|
|
|
|
|
const rolesResourcesScopes = this.app.db.getRepository('dataSourcesRolesResourcesScopes');
|
2022-04-10 19:22:39 +08:00
|
|
|
await rolesResourcesScopes.createMany({
|
|
|
|
records: [
|
|
|
|
{
|
2022-04-12 12:02:58 +08:00
|
|
|
key: 'all',
|
2022-04-10 19:22:39 +08:00
|
|
|
name: '{{t("All records")}}',
|
|
|
|
scope: {},
|
|
|
|
},
|
|
|
|
{
|
2022-04-12 12:02:58 +08:00
|
|
|
key: 'own',
|
2022-04-10 19:22:39 +08:00
|
|
|
name: '{{t("Own records")}}',
|
|
|
|
scope: {
|
|
|
|
createdById: '{{ ctx.state.currentUser.id }}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2022-02-28 14:25:50 +08:00
|
|
|
});
|
2022-04-24 10:14:46 +08:00
|
|
|
|
2023-12-12 23:02:09 +08:00
|
|
|
this.app.on('beforeSignOut', ({ userId }) => {
|
|
|
|
this.app.cache.del(`roles:${userId}`);
|
|
|
|
});
|
2023-06-07 23:46:42 +08:00
|
|
|
this.app.resourcer.use(setCurrentRole, { tag: 'setCurrentRole', before: 'acl', after: 'auth' });
|
2022-07-25 19:33:23 +08:00
|
|
|
|
|
|
|
this.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
|
2022-04-24 10:14:46 +08:00
|
|
|
this.app.acl.allow('roles', 'check', 'loggedIn');
|
|
|
|
|
|
|
|
this.app.acl.allow('*', '*', (ctx) => {
|
2022-04-10 19:22:39 +08:00
|
|
|
return ctx.state.currentRole === 'root';
|
|
|
|
});
|
2022-04-13 12:18:44 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.acl.addFixedParams('collections', 'destroy', () => {
|
|
|
|
return {
|
|
|
|
filter: {
|
|
|
|
$and: [{ 'name.$ne': 'roles' }, { 'name.$ne': 'rolesUsers' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.acl.addFixedParams('rolesResourcesScopes', 'destroy', () => {
|
|
|
|
return {
|
|
|
|
filter: {
|
|
|
|
$and: [{ 'key.$ne': 'all' }, { 'key.$ne': 'own' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.acl.addFixedParams('rolesResourcesScopes', 'update', () => {
|
|
|
|
return {
|
|
|
|
filter: {
|
|
|
|
$and: [{ 'key.$ne': 'all' }, { 'key.$ne': 'own' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.acl.addFixedParams('roles', 'destroy', () => {
|
|
|
|
return {
|
|
|
|
filter: {
|
|
|
|
$and: [{ 'name.$ne': 'root' }, { 'name.$ne': 'admin' }, { 'name.$ne': 'member' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-04-10 19:22:39 +08:00
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
2022-04-13 12:18:44 +08:00
|
|
|
const { actionName, resourceName, params } = ctx.action;
|
|
|
|
const { showAnonymous } = params || {};
|
2022-04-10 19:22:39 +08:00
|
|
|
if (actionName === 'list' && resourceName === 'roles') {
|
2022-04-13 12:18:44 +08:00
|
|
|
if (!showAnonymous) {
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
filter: {
|
|
|
|
'name.$ne': 'anonymous',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-04-10 19:22:39 +08:00
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-07-07 17:46:19 +08:00
|
|
|
if (actionName === 'update' && resourceName === 'roles.resources') {
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
updateAssociationValues: ['actions'],
|
|
|
|
});
|
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-04-10 19:22:39 +08:00
|
|
|
await next();
|
|
|
|
});
|
2022-04-12 12:02:58 +08:00
|
|
|
|
|
|
|
this.app.acl.use(async (ctx: Context, next) => {
|
|
|
|
const { actionName, resourceName } = ctx.action;
|
|
|
|
if (actionName === 'get' || actionName === 'list') {
|
|
|
|
if (!Array.isArray(ctx?.permission?.can?.params?.fields)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
let collection: Collection;
|
|
|
|
if (resourceName.includes('.')) {
|
|
|
|
const [collectionName, associationName] = resourceName.split('.');
|
|
|
|
const field = ctx.db.getCollection(collectionName)?.getField?.(associationName);
|
|
|
|
if (field.target) {
|
|
|
|
collection = ctx.db.getCollection(field.target);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
collection = ctx.db.getCollection(resourceName);
|
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-04-12 12:02:58 +08:00
|
|
|
if (collection && collection.hasField('createdById')) {
|
|
|
|
ctx.permission.can.params.fields.push('createdById');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
const parseJsonTemplate = this.app.acl.parseJsonTemplate;
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2023-08-29 17:01:14 +08:00
|
|
|
this.app.acl.beforeGrantAction(async (ctx) => {
|
|
|
|
const actionName = this.app.acl.resolveActionAlias(ctx.actionName);
|
|
|
|
|
|
|
|
if (lodash.isPlainObject(ctx.params)) {
|
|
|
|
if (actionName === 'view' && ctx.params.fields) {
|
|
|
|
const appendFields = [];
|
|
|
|
|
|
|
|
const collection = this.app.db.getCollection(ctx.resourceName);
|
|
|
|
|
|
|
|
if (!collection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection.model.primaryKeyAttribute) {
|
|
|
|
appendFields.push(collection.model.primaryKeyAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection.model.rawAttributes['createdAt']) {
|
|
|
|
appendFields.push('createdAt');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection.model.rawAttributes['updatedAt']) {
|
|
|
|
appendFields.push('updatedAt');
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.params = {
|
|
|
|
...lodash.omit(ctx.params, 'fields'),
|
|
|
|
fields: [...ctx.params.fields, ...appendFields],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.acl.use(
|
|
|
|
async (ctx: Context, next) => {
|
|
|
|
const { actionName, resourceName, resourceOf } = ctx.action;
|
|
|
|
// is association request
|
|
|
|
if (resourceName.includes('.') && resourceOf) {
|
|
|
|
if (!ctx?.permission?.can?.params) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
// 关联数据去掉 filter
|
|
|
|
delete ctx.permission.can.params.filter;
|
|
|
|
// 关联数据能不能处理取决于 source 是否有权限
|
|
|
|
const [collectionName] = resourceName.split('.');
|
|
|
|
const action = ctx.can({ resource: collectionName, action: actionName });
|
|
|
|
|
|
|
|
const availableAction = this.app.acl.getAvailableAction(actionName);
|
|
|
|
if (availableAction?.options?.onNewRecord) {
|
|
|
|
if (action) {
|
|
|
|
ctx.permission.skip = true;
|
|
|
|
} else {
|
|
|
|
ctx.permission.can = false;
|
|
|
|
}
|
2022-04-12 12:02:58 +08:00
|
|
|
} else {
|
2023-09-06 09:19:10 +08:00
|
|
|
const filteredParams = this.app.acl.filterParams(ctx, collectionName, action?.params || {});
|
|
|
|
const params = await parseJsonTemplate(filteredParams, ctx);
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
const sourceInstance = await ctx.db.getRepository(collectionName).findOne({
|
|
|
|
filterByTk: resourceOf,
|
2023-09-06 09:19:10 +08:00
|
|
|
filter: params.filter || {},
|
2023-01-09 07:35:48 +08:00
|
|
|
});
|
2023-09-06 09:19:10 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
if (!sourceInstance) {
|
|
|
|
ctx.permission.can = false;
|
|
|
|
}
|
2022-04-12 12:02:58 +08:00
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
}
|
|
|
|
await next();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
before: 'core',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// throw error when user has no fixed params permissions
|
|
|
|
this.app.acl.use(
|
|
|
|
async (ctx: any, next) => {
|
|
|
|
const action = ctx.permission?.can?.action;
|
|
|
|
|
|
|
|
if (action == 'destroy' && !ctx.action.resourceName.includes('.')) {
|
|
|
|
const repository = actionUtils.getRepositoryFromParams(ctx);
|
|
|
|
|
2024-03-04 17:46:58 +08:00
|
|
|
if (!repository) {
|
|
|
|
await next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
// params after merge with fixed params
|
|
|
|
const filteredCount = await repository.count(ctx.permission.mergedParams);
|
|
|
|
|
|
|
|
// params user requested
|
|
|
|
const queryCount = await repository.count(ctx.permission.rawParams);
|
|
|
|
|
|
|
|
if (queryCount > filteredCount) {
|
|
|
|
ctx.throw(403, 'No permissions');
|
|
|
|
return;
|
2022-04-12 12:02:58 +08:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
|
|
|
|
await next();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
after: 'core',
|
|
|
|
group: 'after',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-03-03 23:06:24 +08:00
|
|
|
const withACLMeta = createWithACLMetaMiddleware();
|
2023-01-09 07:35:48 +08:00
|
|
|
|
|
|
|
// append allowedActions to list & get response
|
|
|
|
this.app.use(
|
|
|
|
async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await withACLMeta(ctx, next);
|
|
|
|
} catch (error) {
|
|
|
|
ctx.logger.error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ after: 'restApi', group: 'after' },
|
|
|
|
);
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
|
2022-03-02 18:35:49 +08:00
|
|
|
async install() {
|
|
|
|
const repo = this.db.getRepository<any>('collections');
|
2024-03-03 23:06:24 +08:00
|
|
|
|
2022-03-02 18:35:49 +08:00
|
|
|
if (repo) {
|
|
|
|
await repo.db2cm('roles');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
async load() {
|
2023-01-08 12:45:02 +08:00
|
|
|
await this.importCollections(resolve(__dirname, 'collections'));
|
2024-01-08 18:59:56 +08:00
|
|
|
|
2023-02-13 09:57:03 +08:00
|
|
|
this.db.extendCollection({
|
|
|
|
name: 'rolesUischemas',
|
2024-01-08 18:59:56 +08:00
|
|
|
dumpRules: 'required',
|
2024-01-08 19:05:14 +08:00
|
|
|
origin: this.options.packageName,
|
2023-02-13 09:57:03 +08:00
|
|
|
});
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
2022-02-11 18:13:14 +08:00
|
|
|
|
2023-04-12 17:44:33 +08:00
|
|
|
export default PluginACL;
|