Feature: filter for linkTo field (#64)

* feat: add scope select for relation field

* fix: add associatedKey support for drawerSelect component

* refactor: use 'component.x-component-props.filter' as filter type

* fix: remove user group field

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
Junyi 2021-02-18 08:55:46 +08:00 committed by GitHub
parent cef64bb7f5
commit df2137da79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 40 deletions

View File

@ -5,12 +5,12 @@ import Database from '@nocobase/database';
await api.loadPlugins();
const database: Database = api.database;
await api.database.sync({
tables: ['actions_scopes'],
tables: ['scopes'],
});
const [Collection, User, Role] = database.getModels(['collections', 'users', 'roles']);
const tables = database.getTables(['actions_scopes']);
const tables = database.getTables(['scopes']);
for (let table of tables) {
console.log(table.getName());

View File

@ -38,7 +38,7 @@ import Database from '@nocobase/database';
parent_id: parent.id,
});
const Scope = database.getModel('actions_scopes');
const Scope = database.getModel('scopes');
const collections = await Collection.findAll();

View File

@ -5,12 +5,12 @@ import Database from '@nocobase/database';
await api.loadPlugins();
const database: Database = api.database;
await api.database.sync({
tables: ['actions_scopes', 'action_logs', 'action_changes'],
tables: ['scopes', 'action_logs', 'action_changes'],
});
const [Collection, Page] = database.getModels(['collections', 'pages']);
const tables = database.getTables(['actions_scopes', 'action_logs', 'action_changes']);
const tables = database.getTables(['scopes', 'action_logs', 'action_changes']);
for (let table of tables) {
console.log(table.getName());

View File

@ -72,10 +72,10 @@ Permissions.Actions = connect({
const index = findIndex(values, (item: any) => item && item.name === `${resourceKey}:${record.name}`);
return (
<Scope
resourceTarget={'actions_scopes'}
resourceTarget={'scopes'}
associatedName={'collections'}
associatedKey={resourceKey}
target={'scopes'}
target={'scopes'}
multiple={false}
labelField={'title'}
valueField={'id'}

View File

@ -305,6 +305,32 @@ export default {
type: 'drawerSelect',
},
},
{
interface: 'linkTo',
type: 'hasMany',
name: 'scopes',
target: 'scopes',
title: '数据范围',
sourceKey: 'name',
actions: {
list: {
sort: 'id',
},
update: {
filter: {
locked: false
}
},
destroy: {
filter: {
locked: false
}
}
},
component: {
type: 'drawerSelect',
},
},
],
actions: [
{

View File

@ -304,6 +304,20 @@ export default {
},
},
},
{
type: 'value:visible',
target: 'component.x-component-props.filter',
condition: '{{ !!$self.value }}'
},
{
type: "value:schema",
target: "component.x-component-props.filter",
schema: {
"x-component-props": {
"associatedKey": "{{ $self.value }}"
},
},
},
],
},
},
@ -337,6 +351,19 @@ export default {
default: true,
},
},
{
name: 'component.x-component-props.filter',
interface: 'json',
type: 'virtual',
title: '数据范围',
component: {
type: 'filter',
'x-component-props': {
resourceName: 'collections.fields',
},
showInForm: true,
}
},
{
interface: 'boolean',
type: 'virtual',

View File

@ -1,7 +1,7 @@
import { TableOptions } from '@nocobase/database';
export default {
name: 'actions_scopes',
name: 'scopes',
title: '表操作范围',
developerMode: true,
internal: true,

View File

@ -32,7 +32,7 @@ function getActionPermissions(permissions) {
return;
}
// 以 or 关合并两个 scope 中的 filter
// 以 or 关合并两个 scope 中的 filter
existedScope.set('filter', { 'or': [existedScope.get('filter'), newScope.get('filter')] });
});
});

View File

@ -16,6 +16,11 @@ export default async function(db) {
{ title: '编辑' },
{ title: '管理员', type: ROLE_TYPE_ROOT },
]);
const Scope = db.getModel('scopes');
const scopePublished = await Scope.create({ collection_name: 'posts', filter: { status: 'published' } }, { logging: true });
// TODO(bug): 字段应使用 'created_by' 名称,通过程序映射成外键
const scopeCreatedBy = await Scope.create({ collection_name: 'posts', filter: { status: 'draft', 'created_by_id.$currentUser': true } });
const Field = db.getModel('fields');
const postTitleField = await Field.findOne({
@ -45,7 +50,7 @@ export default async function(db) {
actions: [
{
name: 'posts:list',
scope: { filter: { status: 'published' }, collection_name: 'posts' },
scope: scopePublished,
}
],
fields_permissions: [
@ -73,12 +78,11 @@ export default async function(db) {
actions: [
{
name: 'posts:list',
// TODO(bug): 字段应使用 'created_by' 名称,通过程序映射成外键
scope: { filter: { status: 'draft', 'created_by_id.$currentUser': true }, collection_name: 'posts' },
scope: scopeCreatedBy,
},
{
name: 'posts:update',
scope: { filter: { status: 'draft', 'created_by_id.$currentUser': true }, collection_name: 'posts' },
scope: scopeCreatedBy,
}
],
fields: [

View File

@ -15,7 +15,7 @@ export default {
comment: '操作范围',
type: 'belongsTo',
name: 'scope',
target: 'actions_scopes'
target: 'scopes'
},
{
type: 'belongsTo',

View File

@ -3,32 +3,6 @@ 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: 'id',
},
update: {
filter: {
locked: false
}
},
destroy: {
filter: {
locked: false
}
}
},
component: {
type: 'drawerSelect',
},
},
{
interface: 'linkTo',
title: '权限',