chore: create roles resources

This commit is contained in:
Chareice 2024-01-26 17:19:45 +08:00
parent 283ef503dd
commit 103552d79c
No known key found for this signature in database
7 changed files with 49 additions and 68 deletions

View File

@ -139,31 +139,12 @@ describe('data source with acl', () => {
});
it('should create resources', async () => {
class MockDataSource extends DataSource {
async load(): Promise<void> {}
createCollectionManager(options?: any): any {
return undefined;
}
}
app.dataSourceManager.factory.register('mock', MockDataSource);
const adminUser = await app.db.getRepository('users').create({
values: {
roles: ['root'],
},
});
const adminAgent: any = app.agent().login(adminUser);
await app.db.getRepository('dataSources').create({
values: {
key: 'test',
type: 'mock',
},
});
await app.db.getRepository('roles').create({
values: {
name: 'testRole',
@ -177,37 +158,36 @@ describe('data source with acl', () => {
},
});
const testUserAgent = app.agent().login(testUser);
const adminAgent: any = app.agent().login(adminUser);
const createResourceResp = await adminAgent
.resource('databaseConnections.connectionsRolesResourcesScopes', 'test')
.create({
values: {
name: 'articles title starts with test',
resourceName: 'Articles',
scope: {
title: {
$startsWith: 'test',
},
// should get permission error
const testUserAgent = getDataSourceAgent(app.agent().login(testUser), 'mockInstance1');
const createResourceResp = await adminAgent.resource('dataSources.rolesResourcesScopes', 'mockInstance1').create({
values: {
name: 'posts title starts with test',
resourceName: 'posts',
scope: {
title: {
$startsWith: 'test',
},
},
});
},
});
expect(createResourceResp.status).toBe(200);
// list scopes
const listScopesResp = await adminAgent
.resource('databaseConnections.connectionsRolesResourcesScopes', 'test')
.list({});
const listScopesResp = await adminAgent.resource('dataSources.rolesResourcesScopes', 'mockInstance1').list({});
expect(listScopesResp.status).toBe(200);
const scope = listScopesResp.body.data.find((item) => item.name === 'articles title starts with test');
const scope = listScopesResp.body.data.find((item) => item.name === 'posts title starts with test');
// create user resource permission
const createConnectionResourceResp = await adminAgent.resource('roles.connectionResources', 'testRole').create({
const createConnectionResourceResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').create({
values: {
connectionName: 'test',
dataSourceKey: 'mockInstance1',
usingActionsConfig: true,
actions: [
{
@ -218,7 +198,7 @@ describe('data source with acl', () => {
},
},
],
name: 'Articles',
name: 'posts',
},
});
@ -227,10 +207,10 @@ describe('data source with acl', () => {
const data = createConnectionResourceResp.body.data;
// update scope to null
const updateScopeResp = await adminAgent.resource('roles.connectionResources', 'testRole').update({
const updateScopeResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').update({
filter: {
connectionName: 'test',
name: 'Articles',
dataSourceKey: 'mockInstance1',
name: 'posts',
},
values: {
actions: data.actions.map((action) => {
@ -246,10 +226,10 @@ describe('data source with acl', () => {
expect(updateScopeResp.status).toBe(200);
// get resourcers
const getResourceResp = await adminAgent.resource('roles.connectionResources', 'testRole').get({
const getResourceResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').get({
filter: {
connectionName: 'test',
name: 'Articles',
dataSourceKey: 'mockInstance1',
name: 'posts',
},
appends: ['actions.scope'],
});

View File

@ -2,14 +2,14 @@ import { defineCollection } from '@nocobase/database';
export default defineCollection({
dumpRules: 'required',
name: 'connectionsRolesResourcesActions',
name: 'dataSourcesRolesResourcesActions',
model: 'ConnectionsRolesResourcesActionModel',
fields: [
{
type: 'belongsTo',
name: 'resource',
foreignKey: 'rolesResourceId',
target: 'connectionRolesResources',
target: 'dataSourcesRolesResources',
},
{
type: 'string',
@ -23,8 +23,9 @@ export default defineCollection({
{
type: 'belongsTo',
name: 'scope',
target: 'connectionsRolesResourcesScopes',
target: 'dataSourcesRolesResourcesScopes',
onDelete: 'RESTRICT',
foreignKey: 'scopeId',
},
],
});

View File

@ -2,7 +2,7 @@ import { defineCollection } from '@nocobase/database';
export default defineCollection({
dumpRules: 'required',
name: 'connectionsRolesResourcesScopes',
name: 'dataSourcesRolesResourcesScopes',
fields: [
{
type: 'uid',
@ -10,9 +10,8 @@ export default defineCollection({
},
{
type: 'belongsTo',
name: 'connection',
target: 'databaseConnections',
foreignKey: 'connectionName',
name: 'dataSources',
foreignKey: 'dataSourceKey',
onDelete: 'CASCADE',
},
{

View File

@ -29,7 +29,8 @@ export default defineCollection({
{
type: 'hasMany',
name: 'actions',
target: 'connectionsRolesResourcesActions',
target: 'dataSourcesRolesResourcesActions',
foreignKey: 'rolesResourceId',
},
],
});

View File

@ -26,8 +26,8 @@ export default defineCollection({
},
{
type: 'hasMany',
name: 'connectionsRolesResourcesScopes',
target: 'connectionsRolesResourcesScopes',
name: 'rolesResourcesScopes',
target: 'dataSourcesRolesResourcesScopes',
foreignKey: 'dataSourceKey',
},
],

View File

@ -8,7 +8,7 @@ import { RemoteCollectionModel } from './models/remote-collection-model';
import { RemoteFieldModel } from './models/remote-field-model';
import { rolesRemoteCollectionsResourcer } from './resourcers/roles-remote-collections';
import databaseConnectionsRolesResourcer from './resourcers/data-sources-roles';
import rolesConnectionResourcesResourcer from './resourcers/connection-resources';
import rolesConnectionResourcesResourcer from './resourcers/data-sources-resources';
import { ConnectionsRolesModel } from './models/connections-roles-model';
import { ConnectionsRolesResourcesModel } from './models/connections-roles-resources';

View File

@ -1,5 +1,5 @@
export default {
name: 'roles.connectionResources',
name: 'roles.dataSourceResources',
actions: {
async create(ctx, next) {
const { associatedIndex: roleName } = ctx.action.params;
@ -8,31 +8,31 @@ export default {
const transaction = await db.sequelize.transaction();
const connectionName = ctx.action.params.values.connectionName;
const dataSourceKey = ctx.action.params.values.dataSourceKey;
if (!connectionName) {
throw new Error('connectionName is required');
if (!dataSourceKey) {
throw new Error('dataSourceKey is required');
}
const connectionRole = await db.getRepository('connectionsRoles').findOne({
const connectionRole = await db.getRepository('dataSourcesRoles').findOne({
filter: {
roleName,
connectionName,
dataSourceKey,
},
transaction,
});
if (!connectionRole) {
await db.getRepository('connectionsRoles').create({
await db.getRepository('dataSourcesRoles').create({
values: {
roleName,
connectionName,
dataSourceKey,
},
transaction,
});
}
const record = await db.getRepository('connectionsRolesResources').create({
const record = await db.getRepository('dataSourcesRolesResources').create({
values: {
roleName,
...ctx.action.params.values,
@ -52,11 +52,11 @@ export default {
ctx.body = await ctx.app
.getDb()
.getRepository('connectionsRolesResources')
.getRepository('dataSourcesRolesResources')
.update({
filter: {
roleName,
connectionName: ctx.action.params.filter.connectionName,
dataSourceKey: ctx.action.params.filter.dataSourceKey,
name: ctx.action.params.filter.name,
},
values: ctx.action.params.values,
@ -71,11 +71,11 @@ export default {
const record = await ctx.app
.getDb()
.getRepository('connectionsRolesResources')
.getRepository('dataSourcesRolesResources')
.findOne({
filter: {
roleName,
connectionName: ctx.action.params.filter.connectionName,
dataSourceKey: ctx.action.params.filter.dataSourceKey,
name: ctx.action.params.filter.name,
},
appends: ctx.action.params.appends,