diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index 19e3fd6df..44b8ddb1e 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -184,7 +184,7 @@ export default async (ctx, next) => { // }, })); let throughName; - const {associatedName, resourceFieldName} = values; + const { associatedName, resourceFieldName, associatedKey } = values; if (associatedName) { const table = ctx.db.getTable(associatedName); 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 = { ...view.get(), title, @@ -396,7 +400,7 @@ export default async (ctx, next) => { target: "actions", schema: { "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", schema: { "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", schema: { "x-component-props": { - resourceKey: "{{ $form.values && $form.values.resourceKey }}" + resourceKey: resourceFieldName === 'roles' ? associatedKey : "{{ $form.values && $form.values.resourceKey }}" }, }, }, diff --git a/packages/plugin-permissions/src/actions/collections.roles.ts b/packages/plugin-permissions/src/actions/collections.roles.ts new file mode 100644 index 000000000..2b73276e7 --- /dev/null +++ b/packages/plugin-permissions/src/actions/collections.roles.ts @@ -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); +} diff --git a/packages/plugin-permissions/src/collections/collections.ts b/packages/plugin-permissions/src/collections/collections.ts new file mode 100644 index 000000000..9955a8228 --- /dev/null +++ b/packages/plugin-permissions/src/collections/collections.ts @@ -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); + } + } +}); diff --git a/packages/plugin-permissions/src/collections/roles.ts b/packages/plugin-permissions/src/collections/roles.ts index 65506aef6..40230ba9e 100644 --- a/packages/plugin-permissions/src/collections/roles.ts +++ b/packages/plugin-permissions/src/collections/roles.ts @@ -89,6 +89,23 @@ export default { actionNames: ['create', 'destroy'], 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: [ { diff --git a/packages/plugin-permissions/src/collections/users.ts b/packages/plugin-permissions/src/collections/users.ts new file mode 100644 index 000000000..2a5d2d5c6 --- /dev/null +++ b/packages/plugin-permissions/src/collections/users.ts @@ -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', + }, + }, + }, + ], +}; diff --git a/packages/plugin-permissions/src/server.ts b/packages/plugin-permissions/src/server.ts index 13ba54468..68e8651c4 100644 --- a/packages/plugin-permissions/src/server.ts +++ b/packages/plugin-permissions/src/server.ts @@ -3,6 +3,7 @@ import { Op } from 'sequelize'; import { Application } from '@nocobase/server'; import Database, { Operator } from '@nocobase/database'; import Resourcer from '@nocobase/resourcer'; +import * as collectionsRolesActions from './actions/collections.roles'; import * as rolesCollectionsActions from './actions/roles.collections'; import * as rolesPagesActions from './actions/roles.pages'; @@ -39,6 +40,10 @@ class Permissions { directory: path.resolve(__dirname, 'collections'), }); + Object.keys(collectionsRolesActions).forEach(actionName => { + resourcer.registerActionHandler(`collections.roles:${actionName}`, collectionsRolesActions[actionName]); + }); + Object.keys(rolesCollectionsActions).forEach(actionName => { resourcer.registerActionHandler(`roles.collections:${actionName}`, rolesCollectionsActions[actionName]); }); @@ -47,16 +52,6 @@ class Permissions { 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 添加特殊的操作符以生成查询条件 if (!Operator.has('$currentUser')) { Operator.register('$currentUser', (value, { ctx }) => {