feat: add scope hook

This commit is contained in:
chenos 2021-01-17 21:51:24 +08:00
parent ae257567bf
commit 95be672222
3 changed files with 35 additions and 6 deletions

View File

@ -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: {

View File

@ -10,11 +10,11 @@ export default extend({
target: 'actions_scopes',
title: '数据范围',
sourceKey: 'name',
// actions: {
// list: {
// sort: 'sort',
// },
// },
actions: {
list: {
sort: 'id',
},
},
component: {
type: 'drawerSelect',
},

View File

@ -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 }) => {