feat: role check action (#234)

This commit is contained in:
ChengLei Shao 2022-03-13 19:36:37 +08:00 committed by GitHub
parent fe8c2576d0
commit 9e27e50595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 0 deletions

View 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);
});
});

View 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();
}

View File

@ -2,6 +2,8 @@ import { Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { availableActionResource } from './actions/available-actions';
import { roleCollectionsResource } from './actions/role-collections';
import { checkAction } from './actions/role-check';
import { RoleModel } from './model/RoleModel';
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
import { RoleResourceModel } from './model/RoleResourceModel';
@ -124,6 +126,8 @@ export class PluginACL extends Plugin {
this.app.resourcer.define(availableActionResource);
this.app.resourcer.define(roleCollectionsResource);
this.app.resourcer.registerActionHandler('roles:check', checkAction);
this.app.db.on('roles.afterSave', async (model, options) => {
const { transaction } = options;
@ -257,6 +261,7 @@ export class PluginACL extends Plugin {
],
});
});
this.app.acl.skip('roles', 'check', 'logged-in');
}
async install() {