feat: add collection permissions tab

This commit is contained in:
chenos 2021-01-14 10:52:20 +08:00
parent 1242ba5aed
commit 9d95a27a7f
6 changed files with 130 additions and 15 deletions

View File

@ -184,7 +184,7 @@ export default async (ctx, next) => {
// }, // },
})); }));
let throughName; let throughName;
const {associatedName, resourceFieldName} = values; const { associatedName, resourceFieldName, associatedKey } = values;
if (associatedName) { if (associatedName) {
const table = ctx.db.getTable(associatedName); const table = ctx.db.getTable(associatedName);
const resourceField = table.getField(resourceFieldName); const resourceField = table.getField(resourceFieldName);
@ -380,7 +380,11 @@ export default async (ctx, next) => {
} }
], ],
}; };
} else if (resourceFieldName === 'collections' && resourceKey === 'permissionForm') { } else if (
(resourceFieldName === 'collections' && resourceKey === 'permissionForm')
||
(resourceFieldName === 'roles' && resourceKey === 'permissionForm')
) {
ctx.body = { ctx.body = {
...view.get(), ...view.get(),
title, title,
@ -396,7 +400,7 @@ export default async (ctx, next) => {
target: "actions", target: "actions",
schema: { schema: {
"x-component-props": { "x-component-props": {
resourceKey: "{{ $form.values && $form.values.resourceKey }}" resourceKey: resourceFieldName === 'roles' ? associatedKey : "{{ $form.values && $form.values.resourceKey }}"
}, },
}, },
}, },
@ -414,7 +418,7 @@ export default async (ctx, next) => {
target: "fields", target: "fields",
schema: { schema: {
"x-component-props": { "x-component-props": {
resourceKey: "{{ $form.values && $form.values.resourceKey }}" resourceKey: resourceFieldName === 'roles' ? associatedKey : "{{ $form.values && $form.values.resourceKey }}"
}, },
}, },
}, },
@ -432,7 +436,7 @@ export default async (ctx, next) => {
target: "tabs", target: "tabs",
schema: { schema: {
"x-component-props": { "x-component-props": {
resourceKey: "{{ $form.values && $form.values.resourceKey }}" resourceKey: resourceFieldName === 'roles' ? associatedKey : "{{ $form.values && $form.values.resourceKey }}"
}, },
}, },
}, },

View File

@ -0,0 +1,7 @@
import { actions } from '@nocobase/actions';
export async function list(ctx: actions.Context, next: actions.Next) {
// TODO: 暂时 action 中间件就这么写了
ctx.action.mergeParams({associated: null});
return actions.common.list(ctx, next);
}

View File

@ -0,0 +1,65 @@
import { extend } from '@nocobase/database';
export default extend({
name: 'collections',
fields: [
{
interface: 'linkTo',
type: 'hasMany',
name: 'scopes',
target: 'actions_scopes',
title: '数据范围',
sourceKey: 'name',
// actions: {
// list: {
// sort: 'sort',
// },
// },
component: {
type: 'drawerSelect',
},
},
{
interface: 'linkTo',
title: '权限',
type: 'belongsToMany',
name: 'roles',
through: 'permissions',
sourceKey: 'name'
},
],
views: [
{
type: 'table',
name: 'permissionTable',
title: '权限设置表格',
mode: 'simple',
template: 'SimpleTable',
// actionNames: ['create', 'destroy'],
detailsViewName: 'details',
updateViewName: 'permissionForm',
},
{
type: 'form',
name: 'permissionForm',
title: '权限设置表单',
mode: 'simple',
template: 'DrawerForm',
},
],
tabs: [
{
type: 'association',
name: 'permissions',
title: '权限',
association: 'roles',
viewName: 'permissionTable',
},
],
}, {
customMerge(key) {
if (['views', 'tabs'].includes(key)) {
return (x = [], y = []) => x.concat(y);
}
}
});

View File

@ -89,6 +89,23 @@ export default {
actionNames: ['create', 'destroy'], actionNames: ['create', 'destroy'],
default: true, default: true,
}, },
{
type: 'table',
name: 'permissionTable',
title: '权限设置表格',
mode: 'simple',
template: 'SimpleTable',
// actionNames: ['create', 'destroy'],
detailsViewName: 'details',
updateViewName: 'permissionForm',
},
{
type: 'form',
name: 'permissionForm',
title: '权限设置表单',
mode: 'simple',
template: 'DrawerForm',
},
], ],
tabs: [ tabs: [
{ {

View File

@ -0,0 +1,27 @@
import { TableOptions } from '@nocobase/database';
export default {
name: 'users',
fields: [
{
interface: 'linkTo',
type: 'belongsToMany',
name: 'roles',
title: '权限组',
target: 'roles',
labelField: 'title',
through: 'users_roles',
component: {
type: 'drawerSelect',
// showInTable: true,
showInForm: true,
showInDetail: true,
'x-component-props': {
resourceName: 'collections.roles',
labelField: 'title',
valueField: 'id',
},
},
},
],
};

View File

@ -3,6 +3,7 @@ import { Op } from 'sequelize';
import { Application } from '@nocobase/server'; import { Application } from '@nocobase/server';
import Database, { Operator } from '@nocobase/database'; import Database, { Operator } from '@nocobase/database';
import Resourcer from '@nocobase/resourcer'; import Resourcer from '@nocobase/resourcer';
import * as collectionsRolesActions from './actions/collections.roles';
import * as rolesCollectionsActions from './actions/roles.collections'; import * as rolesCollectionsActions from './actions/roles.collections';
import * as rolesPagesActions from './actions/roles.pages'; import * as rolesPagesActions from './actions/roles.pages';
@ -39,6 +40,10 @@ class Permissions {
directory: path.resolve(__dirname, 'collections'), directory: path.resolve(__dirname, 'collections'),
}); });
Object.keys(collectionsRolesActions).forEach(actionName => {
resourcer.registerActionHandler(`collections.roles:${actionName}`, collectionsRolesActions[actionName]);
});
Object.keys(rolesCollectionsActions).forEach(actionName => { Object.keys(rolesCollectionsActions).forEach(actionName => {
resourcer.registerActionHandler(`roles.collections:${actionName}`, rolesCollectionsActions[actionName]); resourcer.registerActionHandler(`roles.collections:${actionName}`, rolesCollectionsActions[actionName]);
}); });
@ -47,16 +52,6 @@ class Permissions {
resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]); resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]);
}); });
// TODO(optimize): 临时处理,相关逻辑还需要更严格
const usersTable = database.getTable('users');
if (!usersTable.hasField('roles')) {
usersTable.addField({
type: 'belongsToMany',
name: 'roles',
through: 'users_roles'
});
}
// 针对“自己创建的” scope 添加特殊的操作符以生成查询条件 // 针对“自己创建的” scope 添加特殊的操作符以生成查询条件
if (!Operator.has('$currentUser')) { if (!Operator.has('$currentUser')) {
Operator.register('$currentUser', (value, { ctx }) => { Operator.register('$currentUser', (value, { ctx }) => {