chore: acl api

This commit is contained in:
Chareice 2024-01-26 20:24:46 +08:00
parent ff6af998eb
commit d47ded993d
No known key found for this signature in database
2 changed files with 33 additions and 23 deletions

View File

@ -261,5 +261,15 @@ describe('data source with acl', () => {
}, },
}); });
expect(collectionListRep.status).toBe(200); expect(collectionListRep.status).toBe(200);
// call roles check
// @ts-ignore
const checkRep = await app.agent().login(testUser).resource('roles').check({});
expect(checkRep.status).toBe(200);
const checkData = checkRep.body;
expect(checkData.meta.dataSources.mockInstance1).toBeDefined();
console.log(JSON.stringify(checkData, null, 2));
}); });
}); });

View File

@ -105,11 +105,11 @@ export class PluginDataSourceManagerServer extends Plugin {
this.app.resourcer.define(rolesConnectionResourcesResourcer); this.app.resourcer.define(rolesConnectionResourcesResourcer);
this.app.resourcer.define({ this.app.resourcer.define({
name: 'databaseConnections', name: 'dataSources',
}); });
this.app.resourcer this.app.resourcer
.getResource('databaseConnections') .getResource('dataSources')
.getAction('list') .getAction('list')
.middlewares.push( .middlewares.push(
new Middleware(async (ctx, next) => { new Middleware(async (ctx, next) => {
@ -119,10 +119,10 @@ export class PluginDataSourceManagerServer extends Plugin {
const mapData = (row) => { const mapData = (row) => {
const data = row.toJSON(); const data = row.toJSON();
if (hasCollections) { if (hasCollections) {
const database = ctx.app.getDb(data.name); const dataSource = this.app.dataSourceManager.dataSources.get(data.key);
const collections = [...database.collections.values()].filter(
(collection) => collection.options.introspected, const collections = dataSource.collectionManager.getCollections();
);
data.collections = collections.map((collection) => { data.collections = collections.map((collection) => {
const collectionOptions = collection.options; const collectionOptions = collection.options;
const fields = [...collection.fields.values()].map((field) => field.options); const fields = [...collection.fields.values()].map((field) => field.options);
@ -168,20 +168,18 @@ export class PluginDataSourceManagerServer extends Plugin {
app, app,
}); });
} }
});
this.app.on('afterStart', async (app: Application) => {
// load roles // load roles
// const rolesModel: ConnectionsRolesModel[] = await this.app.db.getRepository('connectionsRoles').find(); const rolesModel: ConnectionsRolesModel[] = await this.app.db.getRepository('dataSourcesRoles').find();
// const pluginACL: any = this.app.pm.get('acl'); const pluginACL: any = this.app.pm.get('acl');
//
// for (const roleModel of rolesModel) { for (const roleModel of rolesModel) {
// await roleModel.writeToAcl({ await roleModel.writeToAcl({
// grantHelper: pluginACL.grantHelper, grantHelper: pluginACL.grantHelper,
// associationFieldsActions: pluginACL.associationFieldsActions, associationFieldsActions: pluginACL.associationFieldsActions,
// acl: this.app.acls.get(roleModel.get('connectionName')), acl: this.app.dataSourceManager.dataSources.get(roleModel.get('dataSourceKey')).acl,
// }); });
// } }
}); });
this.app.db.on('dataSourcesRolesResources.afterSaveWithAssociations', async (model, options) => { this.app.db.on('dataSourcesRolesResources.afterSaveWithAssociations', async (model, options) => {
@ -219,11 +217,13 @@ export class PluginDataSourceManagerServer extends Plugin {
const { resourceName, actionName } = action.params; const { resourceName, actionName } = action.params;
if (resourceName === 'roles' && actionName == 'check') { if (resourceName === 'roles' && actionName == 'check') {
const roleName = ctx.state.currentRole; const roleName = ctx.state.currentRole;
const connections = await ctx.db.getRepository('databaseConnections').find(); const dataSources = await ctx.db.getRepository('dataSources').find();
ctx.bodyMeta = { ctx.bodyMeta = {
dataSources: connections.reduce((carry, connectionModel) => { dataSources: dataSources.reduce((carry, dataSourceModel) => {
const aclInstance = this.app.acls.get(connectionModel.get('name')); const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceModel.get('key'));
const aclInstance = dataSource.acl;
const roleInstance = aclInstance.getRole(roleName); const roleInstance = aclInstance.getRole(roleName);
const dataObj = { const dataObj = {
@ -239,7 +239,7 @@ export class PluginDataSourceManagerServer extends Plugin {
dataObj['actions'] = data['actions']; dataObj['actions'] = data['actions'];
} }
carry[connectionModel.get('name')] = dataObj; carry[dataSourceModel.get('key')] = dataObj;
return carry; return carry;
}, {}), }, {}),
@ -249,7 +249,7 @@ export class PluginDataSourceManagerServer extends Plugin {
this.app.acl.registerSnippet({ this.app.acl.registerSnippet({
name: `pm.${this.name}`, name: `pm.${this.name}`,
actions: ['databaseConnections:*', 'remoteCollections:*', 'roles.connectionResources'], actions: ['dataSources:*', 'roles.dataSourceResources'],
}); });
} }