feat: role check action (#234)
This commit is contained in:
parent
fe8c2576d0
commit
9e27e50595
35
packages/plugin-acl/src/__tests__/role-check.test.ts
Normal file
35
packages/plugin-acl/src/__tests__/role-check.test.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { MockServer } from '@nocobase/test';
|
||||||
|
import { changeMockRole, changeMockUser, prepareApp } from './prepare';
|
||||||
|
import { Database } from '@nocobase/database';
|
||||||
|
|
||||||
|
describe('role check action', () => {
|
||||||
|
let app: MockServer;
|
||||||
|
let db: Database;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
app = await prepareApp();
|
||||||
|
db = app.db;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return role info', async () => {
|
||||||
|
const role = await db.getRepository('roles').create({
|
||||||
|
values: {
|
||||||
|
name: 'test',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
changeMockUser({
|
||||||
|
id: 2,
|
||||||
|
});
|
||||||
|
|
||||||
|
changeMockRole('test');
|
||||||
|
|
||||||
|
const response = await app.agent().get('/roles:check');
|
||||||
|
|
||||||
|
expect(response.statusCode).toEqual(200);
|
||||||
|
});
|
||||||
|
});
|
19
packages/plugin-acl/src/actions/role-check.ts
Normal file
19
packages/plugin-acl/src/actions/role-check.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export async function checkAction(ctx, next) {
|
||||||
|
const currentRole = ctx.state.currentRole;
|
||||||
|
if (currentRole) {
|
||||||
|
const roleInstance = await ctx.db.getRepository('roles').findOne({
|
||||||
|
filter: {
|
||||||
|
name: currentRole,
|
||||||
|
},
|
||||||
|
appends: ['menuUiSchemas'],
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
role: ctx.app.acl.getRole(currentRole).toJSON(),
|
||||||
|
allowConfigure: roleInstance.get('allowConfigure'),
|
||||||
|
roleMenuItemIds: roleInstance.get('menuUiSchemas').map((uiSchema) => uiSchema.get('x-uid')),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await next();
|
||||||
|
}
|
@ -2,6 +2,8 @@ import { Plugin } from '@nocobase/server';
|
|||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { availableActionResource } from './actions/available-actions';
|
import { availableActionResource } from './actions/available-actions';
|
||||||
import { roleCollectionsResource } from './actions/role-collections';
|
import { roleCollectionsResource } from './actions/role-collections';
|
||||||
|
import { checkAction } from './actions/role-check';
|
||||||
|
|
||||||
import { RoleModel } from './model/RoleModel';
|
import { RoleModel } from './model/RoleModel';
|
||||||
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
||||||
import { RoleResourceModel } from './model/RoleResourceModel';
|
import { RoleResourceModel } from './model/RoleResourceModel';
|
||||||
@ -124,6 +126,8 @@ export class PluginACL extends Plugin {
|
|||||||
this.app.resourcer.define(availableActionResource);
|
this.app.resourcer.define(availableActionResource);
|
||||||
this.app.resourcer.define(roleCollectionsResource);
|
this.app.resourcer.define(roleCollectionsResource);
|
||||||
|
|
||||||
|
this.app.resourcer.registerActionHandler('roles:check', checkAction);
|
||||||
|
|
||||||
this.app.db.on('roles.afterSave', async (model, options) => {
|
this.app.db.on('roles.afterSave', async (model, options) => {
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
|
|
||||||
@ -257,6 +261,7 @@ export class PluginACL extends Plugin {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
this.app.acl.skip('roles', 'check', 'logged-in');
|
||||||
}
|
}
|
||||||
|
|
||||||
async install() {
|
async install() {
|
||||||
|
Loading…
Reference in New Issue
Block a user