2022-01-24 14:10:35 +08:00
|
|
|
import { Database } from '@nocobase/database';
|
|
|
|
|
|
|
|
type UsingConfigType = 'strategy' | 'resourceAction';
|
|
|
|
|
|
|
|
const roleCollectionsResource = {
|
|
|
|
name: 'roles.collections',
|
|
|
|
actions: {
|
|
|
|
async list(ctx, next) {
|
|
|
|
const role = ctx.action.params.associatedIndex;
|
|
|
|
|
|
|
|
const db: Database = ctx.db;
|
|
|
|
const collectionRepository = db.getRepository('collections');
|
|
|
|
const collections = await collectionRepository.find();
|
|
|
|
|
|
|
|
const roleResources = await db.getRepository('rolesResources').find({
|
|
|
|
filter: {
|
|
|
|
roleName: role,
|
|
|
|
usingActionsConfig: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const roleResourcesNames = roleResources.map((roleResource) => roleResource.get('name'));
|
|
|
|
|
2022-02-15 22:32:02 +08:00
|
|
|
ctx.body = collections
|
|
|
|
.map((collection) => {
|
|
|
|
const usingConfig: UsingConfigType = roleResourcesNames.includes(collection.get('name'))
|
|
|
|
? 'resourceAction'
|
|
|
|
: 'strategy';
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: collection.get('name') as string,
|
|
|
|
title: collection.get('title') as string,
|
|
|
|
usingConfig,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.sort((a, b) => (a.name > b.name ? 1 : -1));
|
2022-02-11 23:59:03 +08:00
|
|
|
|
|
|
|
await next();
|
2022-01-24 14:10:35 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export { roleCollectionsResource };
|