relationAuth把modi上的操作deduce到对应的entity上

This commit is contained in:
Xu Chang 2023-07-14 16:27:57 +08:00
parent a32ba7d0ea
commit 9b6435ebda
5 changed files with 108 additions and 11 deletions

View File

@ -1,6 +1,11 @@
export declare function registerIgnoredForeignKeyMap(map: Record<string, string[]>): void;
export declare function registerSelectFreeEntities(entities: string[]): void;
export declare function registerIgnoredRelationPathMap(map: Record<string, string[]>): void;
/**
* path
* @param map
*/
export declare function registerFixedDestinationPathMap(map: Record<string, string[]>): void;
export declare function registerDeducedRelationMap(map: Record<string, string>): void;
export declare function analyzeEntities(inputDir: string, relativePath?: string): void;
export declare function buildSchema(outputDir: string): void;

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSchema = exports.analyzeEntities = exports.registerDeducedRelationMap = exports.registerIgnoredRelationPathMap = exports.registerSelectFreeEntities = exports.registerIgnoredForeignKeyMap = void 0;
exports.buildSchema = exports.analyzeEntities = exports.registerDeducedRelationMap = exports.registerFixedDestinationPathMap = exports.registerIgnoredRelationPathMap = exports.registerSelectFreeEntities = exports.registerIgnoredForeignKeyMap = void 0;
var tslib_1 = require("tslib");
var path_1 = tslib_1.__importDefault(require("path"));
var assert_1 = tslib_1.__importDefault(require("assert"));
@ -3479,6 +3479,8 @@ var IGNORED_FOREIGN_KEY_MAP = {};
var IGNORED_RELATION_PATH_MAP = {};
var DEDUCED_RELATION_MAP = {};
var SELECT_FREE_ENTITIES = [];
var FIXED_DESTINATION_PATH_MAP = {};
var FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES = [];
function registerIgnoredForeignKeyMap(map) {
IGNORED_FOREIGN_KEY_MAP = map;
}
@ -3493,6 +3495,25 @@ function registerIgnoredRelationPathMap(map) {
}
}
exports.registerIgnoredRelationPathMap = registerIgnoredRelationPathMap;
/**
* 很多路径虽然最后指向同一对象但不能封掉封了会导致查询的时候找不到对应的路径path
* @param map
*/
function registerFixedDestinationPathMap(map) {
var _a;
for (var k in map) {
if (k === '.') {
FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES.push.apply(FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES, tslib_1.__spreadArray([], tslib_1.__read(map[k]), false));
}
else if (FIXED_DESTINATION_PATH_MAP[k]) {
(_a = FIXED_DESTINATION_PATH_MAP[k]).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(map[k]), false));
}
else {
FIXED_DESTINATION_PATH_MAP[k] = map[k];
}
}
}
exports.registerFixedDestinationPathMap = registerFixedDestinationPathMap;
function registerDeducedRelationMap(map) {
var _loop_12 = function (k) {
var entity = (0, string_1.firstLetterUpperCase)(k);
@ -3515,7 +3536,7 @@ exports.registerDeducedRelationMap = registerDeducedRelationMap;
* 输出所有和User相关的对象的后继
*/
function outputRelation(outputDir, printer) {
var ExcludedEntities = ['Oper', 'User', 'OperEntity', 'Modi', 'ModiEntity', 'UserRelation'];
var ExcludedEntities = ['Oper', 'User', 'OperEntity', 'Modi', 'ModiEntity', 'UserRelation', 'Relation', 'RelationAuth', 'ActionAuth'];
var actionPath = [];
var relationPath = [];
var outputRecursively = function (root, entity, path, paths, isRelation) {
@ -3529,7 +3550,7 @@ function outputRelation(outputDir, printer) {
if (paths.length > 12) {
throw new Error('对象之间的关系深度过长,请优化设计加以避免');
}
actionPath.push([(0, string_1.firstLetterLowerCase)(entity), path, root, isRelation]);
actionPath.push([(0, string_1.firstLetterLowerCase)(entity), path, root, isRelation, paths]);
if (Schema[entity].hasRelationDef) {
// assert(!DEDUCED_RELATION_MAP[entity], `${entity}对象定义了deducedRelationMap但它有relation`);
relationPath.push([(0, string_1.firstLetterLowerCase)(entity), path, root, isRelation]);
@ -3584,6 +3605,28 @@ function outputRelation(outputDir, printer) {
hasRelationEntities.forEach(function (entity3) {
outputRecursively((0, string_1.firstLetterLowerCase)(entity3), entity3, '', [], true);
});
actionPath.sort(function (ele1, ele2) {
// 先按sourceEntity来排序
if (ele1[0] > ele2[0]) {
return 1;
}
else if (ele1[0] < ele2[0]) {
return -1;
}
else {
// 再按destEntity
if (ele1[2] > ele2[2]) {
return 1;
}
else if (ele1[2] < ele2[2]) {
return -1;
}
else {
// 最后按paths的长度倒排
return ele1[4].length - ele2[4].length;
}
}
});
var entityRelations = [];
for (var entity in Schema) {
var hasRelationDef = Schema[entity].hasRelationDef;

View File

@ -15,7 +15,9 @@ var RelationAuth = /** @class */ (function () {
this.schema = schema;
this.selectFreeEntities = selectFreeEntities;
this.relationalChecker = {};
this.authDeduceRelationMap = authDeduceRelationMap;
this.authDeduceRelationMap = Object.assign({}, authDeduceRelationMap, {
modi: 'entity',
});
this.constructRelationalChecker();
}
RelationAuth.prototype.constructRelationalChecker = function () {
@ -817,7 +819,7 @@ var RelationAuth = /** @class */ (function () {
var action = operation.action || 'select';
switch (action) {
case 'select': {
if (['relation', 'actionAuth', 'relationAuth', 'user', 'userEntityGrant'].includes(entity)) {
if (['relation', 'actionAuth', 'relationAuth', 'user', 'userEntityGrant', 'oper', 'operEntity'].includes(entity)) {
return '';
}
if (entity === 'userRelation') {

View File

@ -6304,6 +6304,8 @@ let IGNORED_FOREIGN_KEY_MAP: Record<string, string[]> = {};
let IGNORED_RELATION_PATH_MAP: Record<string, string[]> = {};
let DEDUCED_RELATION_MAP: Record<string, string> = {};
let SELECT_FREE_ENTITIES: string[] = [];
let FIXED_DESTINATION_PATH_MAP: Record<string, string[]> = {};
let FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES: string[] = [];
export function registerIgnoredForeignKeyMap(map: Record<string, string[]>) {
IGNORED_FOREIGN_KEY_MAP = map;
@ -6319,6 +6321,24 @@ export function registerIgnoredRelationPathMap(map: Record<string, string[]>) {
}
}
/**
* path
* @param map
*/
export function registerFixedDestinationPathMap(map: Record<string, string[]>) {
for (const k in map) {
if (k === '.') {
FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES.push(...map[k]);
}
else if (FIXED_DESTINATION_PATH_MAP[k]) {
FIXED_DESTINATION_PATH_MAP[k].push(...map[k]);
}
else {
FIXED_DESTINATION_PATH_MAP[k] = map[k];
}
}
}
export function registerDeducedRelationMap(map: Record<string, string>) {
for (const k in map) {
const entity = firstLetterUpperCase(k);
@ -6342,8 +6362,8 @@ export function registerDeducedRelationMap(map: Record<string, string>) {
* User相关的对象的后继
*/
function outputRelation(outputDir: string, printer: ts.Printer) {
const ExcludedEntities = ['Oper', 'User', 'OperEntity', 'Modi', 'ModiEntity', 'UserRelation'];
const actionPath: [string, string, string, boolean][] = [];
const ExcludedEntities = ['Oper', 'User', 'OperEntity', 'Modi', 'ModiEntity', 'UserRelation', 'Relation', 'RelationAuth', 'ActionAuth'];
const actionPath: [string, string, string, boolean, string[]][] = [];
const relationPath: [string, string, string, boolean][] = [];
const outputRecursively = (root: string, entity: string, path: string, paths: string[], isRelation: boolean) => {
if (ExcludedEntities.includes(entity)) {
@ -6360,7 +6380,7 @@ function outputRelation(outputDir: string, printer: ts.Printer) {
throw new Error('对象之间的关系深度过长,请优化设计加以避免');
}
actionPath.push([firstLetterLowerCase(entity), path, root, isRelation]);
actionPath.push([firstLetterLowerCase(entity), path, root, isRelation, paths]);
if (Schema[entity].hasRelationDef) {
// assert(!DEDUCED_RELATION_MAP[entity], `${entity}对象定义了deducedRelationMap但它有relation`);
@ -6420,7 +6440,32 @@ function outputRelation(outputDir: string, printer: ts.Printer) {
(entity3) => {
outputRecursively(firstLetterLowerCase(entity3), entity3, '', [], true);
}
)
);
actionPath.sort(
(ele1, ele2) => {
// 先按sourceEntity来排序
if (ele1[0] > ele2[0]) {
return 1;
}
else if (ele1[0] < ele2[0]) {
return -1;
}
else {
// 再按destEntity
if (ele1[2] > ele2[2]) {
return 1;
}
else if (ele1[2] < ele2[2]) {
return -1;
}
else {
// 最后按paths的长度倒排
return ele1[4].length - ele2[4].length;
}
}
}
);
const entityRelations: [string, string[]][] = [];
for (const entity in Schema) {

View File

@ -523,7 +523,9 @@ export class RelationAuth<ED extends EntityDict & BaseEntityDict>{
this.schema = schema;
this.selectFreeEntities = selectFreeEntities;
this.relationalChecker = {};
this.authDeduceRelationMap = authDeduceRelationMap;
this.authDeduceRelationMap = Object.assign({}, authDeduceRelationMap, {
modi: 'entity',
});
this.constructRelationalChecker();
}
@ -991,7 +993,7 @@ export class RelationAuth<ED extends EntityDict & BaseEntityDict>{
const action = (operation as ED[T]['Operation']).action || 'select';
switch (action) {
case 'select': {
if (['relation', 'actionAuth', 'relationAuth', 'user', 'userEntityGrant'].includes(entity as string)) {
if (['relation', 'actionAuth', 'relationAuth', 'user', 'userEntityGrant', 'oper', 'operEntity'].includes(entity as string)) {
return '';
}
if (entity === 'userRelation') {