tachybase_todo/packages/actions/src/middlewares/associated.ts
Junyi c5f089d7b7
feat: add permissions plugin (#53)
* test: skip bug test cases for ci passing

* feat: add base structure of plugin-permissions

* fix: user token parsing

* Refactor action parameter for better mergeParams (#55)

* refactor: add parameter types to handle parameters in action [WIP]

* fix: action parameter

* fix: test cases

* test: try to fix build error

* remove unused packages

* fix: revert compatibility back

Co-authored-by: chenos <chenlinxh@gmail.com>

* 补充权限界面相关功能

* bugfix

* fix: developer mode does not work

* feat: add action scope and fields limitation in permission

* 改进权限配置表单

* feat: get/update action for role.collection

* add scope select component

* add role users tabs

* typings

* test: temp skip

Co-authored-by: chenos <chenlinxh@gmail.com>
2021-01-13 16:23:15 +08:00

52 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Context, Next } from '../actions';
import { Action } from '@nocobase/resourcer';
import { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';
export async function associated(ctx: Context, next: Next) {
if (!(ctx.action instanceof Action)) {
return next();
}
const { associated, associatedName, associatedKey, resourceName } = ctx.action.params;
if (!associatedName || !associatedKey) {
return next();
}
if (associated) {
return next();
}
const Model = ctx.db.getModel(associatedName);
const field = ctx.db.getTable(associatedName).getField(resourceName);
let key: string;
switch (true) {
case field instanceof BELONGSTO:
// 如fields.collection对应的 API 为 /fields/119/collection此时 key 为 PK
key = Model.primaryKeyAttribute;
break;
case field instanceof HASONE:
case field instanceof HASMANY:
case field instanceof BELONGSTOMANY:
key = field.options.sourceKey;
break;
}
if (key) {
const model = await Model.findOne({
where: {
[key]: associatedKey,
}
});
if (model) {
ctx.action.mergeParams({ associated: model, resourceField: field });
}
}
await next();
}
export default associated;