From 95be672222d19bfde801651b607fe7fbdd285ab8 Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 17 Jan 2021 21:51:24 +0800 Subject: [PATCH] feat: add scope hook --- packages/plugin-pages/src/actions/getView.ts | 2 +- .../src/collections/collections.ts | 10 +++---- packages/plugin-permissions/src/server.ts | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index 44b8ddb1ed..51d9576fef 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -36,7 +36,7 @@ const transforms = { const dataSource = field.get('dataSource').filter(item => item.key !== 'developerMode'); field.set('dataSource', dataSource); } - if (field.get('name') === 'filter' && field.get('collection_name') === 'views') { + if (field.get('component.type') === 'filter') { const { values } = ctx.action.params; const options = Field.parseApiJson(ctx.state.developerMode ? { filter: { diff --git a/packages/plugin-permissions/src/collections/collections.ts b/packages/plugin-permissions/src/collections/collections.ts index 9955a8228a..7c62563dfb 100644 --- a/packages/plugin-permissions/src/collections/collections.ts +++ b/packages/plugin-permissions/src/collections/collections.ts @@ -10,11 +10,11 @@ export default extend({ target: 'actions_scopes', title: '数据范围', sourceKey: 'name', - // actions: { - // list: { - // sort: 'sort', - // }, - // }, + actions: { + list: { + sort: 'id', + }, + }, component: { type: 'drawerSelect', }, diff --git a/packages/plugin-permissions/src/server.ts b/packages/plugin-permissions/src/server.ts index 68e8651c47..711143d989 100644 --- a/packages/plugin-permissions/src/server.ts +++ b/packages/plugin-permissions/src/server.ts @@ -52,6 +52,35 @@ class Permissions { resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]); }); + const defaultScopes = [ + { + title: '全部数据', + filter: {}, + }, + { + title: '用户自己的数据', + filter: { + "created_by_id.$currentUser": true, + }, + }, + ]; + + const Scope = database.getModel('actions_scopes'); + + database.getModel('collections').addHook('afterCreate', async (model, options) => { + // TODO(bug): createScope 存不了 filter 参数 + // for (const scope of defaultScopes) { + // const s = await model.createScope(scope, options); + // console.log(s.toJSON()); + // } + try { + await Scope.bulkCreate(defaultScopes.map(scope => ({...scope, collection_name: model.get('name')}))); + } catch (error) { + console.error(error); + throw error; + } + }); + // 针对“自己创建的” scope 添加特殊的操作符以生成查询条件 if (!Operator.has('$currentUser')) { Operator.register('$currentUser', (value, { ctx }) => {