fix: scopes

This commit is contained in:
chenos 2021-01-23 17:09:33 +08:00
parent e36415330f
commit 82355e1422
3 changed files with 19 additions and 43 deletions

View File

@ -73,23 +73,11 @@ const defaultValues = {
default: true,
},
],
scopes: [
{
title: '全部数据',
filter: {},
},
{
title: '用户自己的数据',
filter: {
"created_by_id.$currentUser": true,
},
},
],
};
export default async function (model: CollectionModel, options: any = {}) {
const { migrate = true } = options;
console.log({migrate})
console.log('plugin-collections hook', {migrate})
if (migrate) {
await model.migrate({...options, isNewRecord: true});
}

View File

@ -1,6 +1,5 @@
import { Model, ModelCtor, BELONGSTOMANY } from '@nocobase/database';
import { get, set } from 'lodash';
import { Op } from 'sequelize';
import { get, set, isString } from 'lodash';
const transforms = {
table: async (fields: Model[], context?: any) => {
@ -50,7 +49,7 @@ const transforms = {
field.set('dataSource', dataSource);
}
const { values } = ctx.action.params;
if (field.get('component.type') === 'filter' && get(values, 'associatedKey')) {
if (field.get('component.type') === 'filter' && get(values, 'associatedKey') && isString(get(values, 'associatedKey'))) {
const options = Field.parseApiJson(ctx.state.developerMode ? {
filter: {
collection_name: get(values, 'associatedKey'),

View File

@ -34,33 +34,22 @@ export class Permissions {
resourcer.registerActionHandler(`roles.collections:${actionName}`, rolesCollectionsActions[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;
// }
database.getModel('collections').addHook('afterCreate', async (model: any, options) => {
console.log('plugin-permissions hook');
await model.updateAssociations({
scopes: [
{
title: '全部数据',
filter: {},
},
{
title: '用户自己的数据',
filter: {
"created_by_id.$currentUser": true,
},
},
]
}, options);
});
database.getModel('users').addHook('afterCreate', async(model, options) => {