oak-domain 6.1.0 → 6.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/base-app-domain/ActionDefDict.d.ts +1 -0
- package/es/base-app-domain/ActionDefDict.js +3 -1
- package/es/base-app-domain/EntityDict.d.ts +86 -1
- package/es/base-app-domain/Log/Storage.js +4 -0
- package/es/base-app-domain/Log/_baseSchema.d.ts +5 -0
- package/es/base-app-domain/Storage.js +3 -1
- package/es/base-app-domain/StyleDict.js +3 -1
- package/es/base-app-domain/Version/Action.d.ts +6 -0
- package/es/base-app-domain/Version/Action.js +2 -0
- package/es/base-app-domain/Version/Schema.d.ts +19 -0
- package/es/base-app-domain/Version/Schema.js +1 -0
- package/es/base-app-domain/Version/Storage.d.ts +3 -0
- package/es/base-app-domain/Version/Storage.js +54 -0
- package/es/base-app-domain/Version/Style.d.ts +3 -0
- package/es/base-app-domain/Version/Style.js +6 -0
- package/es/base-app-domain/Version/_baseSchema.d.ts +57 -0
- package/es/base-app-domain/Version/_baseSchema.js +1 -0
- package/es/compiler/dependencyBuilder.d.ts +1 -0
- package/es/compiler/dependencyBuilder.js +173 -55
- package/es/compiler/routerBuilder.d.ts +4 -1
- package/es/compiler/routerBuilder.js +22 -19
- package/es/compiler/schemaBuilder.d.ts +5 -1
- package/es/compiler/schemaBuilder.js +10 -0
- package/es/context/BackendRuntimeContext.d.ts +9 -1
- package/es/context/BackendRuntimeContext.js +13 -1
- package/es/entities/Log.d.ts +2 -0
- package/es/entities/Log.js +2 -0
- package/es/entities/Version.d.ts +14 -0
- package/es/entities/Version.js +42 -0
- package/es/store/AsyncRowStore.d.ts +4 -1
- package/es/store/AsyncRowStore.js +7 -0
- package/es/store/CascadeStore.js +22 -0
- package/es/store/RelationAuth/authorizationFilter.d.ts +11 -0
- package/es/store/RelationAuth/authorizationFilter.js +104 -0
- package/es/store/RelationAuth/continuation.d.ts +9 -0
- package/es/store/RelationAuth/continuation.js +33 -0
- package/es/store/RelationAuth/index.d.ts +141 -0
- package/es/store/RelationAuth/index.js +1440 -0
- package/es/store/RelationAuth/model.d.ts +38 -0
- package/es/store/RelationAuth/model.js +24 -0
- package/es/store/RelationAuth/userLookup.d.ts +21 -0
- package/es/store/RelationAuth/userLookup.js +102 -0
- package/es/store/SyncRowStore.d.ts +2 -1
- package/es/store/SyncRowStore.js +3 -0
- package/es/store/filter.fix.d.ts +128 -0
- package/es/store/filter.fix.js +2862 -0
- package/es/store/filter.js +14 -9
- package/es/store/triggers.js +239 -44
- package/es/types/Context.d.ts +2 -1
- package/es/types/Demand.d.ts +11 -1
- package/es/types/Environment.d.ts +13 -2
- package/es/types/Exception.d.ts +3 -3
- package/es/types/Exception.js +25 -22
- package/es/types/Expression.d.ts +4 -1
- package/es/types/Expression.js +1 -1
- package/es/types/Storage.d.ts +15 -2
- package/es/utils/SimpleConnector.js +5 -2
- package/es/utils/decimal.js +2 -1
- package/es/utils/fetch/index.ali.d.ts +3 -0
- package/es/utils/fetch/index.ali.js +31 -0
- package/es/utils/fetch/index.d.ts +3 -0
- package/es/utils/fetch/index.js +5 -0
- package/es/utils/fetch/index.mp.d.ts +5 -3
- package/es/utils/fetch/index.mp.js +5 -280
- package/es/utils/fetch/index.wechat.d.ts +5 -0
- package/es/utils/fetch/index.wechat.js +174 -0
- package/es/utils/fetch/mini-program.shared.d.ts +37 -0
- package/es/utils/fetch/mini-program.shared.js +85 -0
- package/es/utils/geo.d.ts +1 -1
- package/es/utils/geo.js +1 -1
- package/es/utils/random/random.ali.d.ts +6 -0
- package/es/utils/random/random.ali.js +21 -0
- package/es/utils/random/random.mp.d.ts +5 -1
- package/es/utils/random/random.mp.js +11 -5
- package/es/utils/random/random.native.d.ts +1 -1
- package/es/utils/random/random.native.js +7 -4
- package/es/utils/random/random.wechat.d.ts +1 -0
- package/es/utils/random/random.wechat.js +10 -0
- package/lib/base-app-domain/ActionDefDict.d.ts +1 -0
- package/lib/base-app-domain/ActionDefDict.js +3 -1
- package/lib/base-app-domain/EntityDict.d.ts +86 -1
- package/lib/base-app-domain/Log/Storage.js +4 -0
- package/lib/base-app-domain/Log/_baseSchema.d.ts +5 -0
- package/lib/base-app-domain/Storage.js +3 -1
- package/lib/base-app-domain/StyleDict.js +3 -1
- package/lib/base-app-domain/Version/Action.d.ts +6 -0
- package/lib/base-app-domain/Version/Action.js +5 -0
- package/lib/base-app-domain/Version/Schema.d.ts +19 -0
- package/lib/base-app-domain/Version/Schema.js +4 -0
- package/lib/base-app-domain/Version/Storage.d.ts +3 -0
- package/lib/base-app-domain/Version/Storage.js +57 -0
- package/lib/base-app-domain/Version/Style.d.ts +3 -0
- package/lib/base-app-domain/Version/Style.js +9 -0
- package/lib/base-app-domain/Version/_baseSchema.d.ts +57 -0
- package/lib/base-app-domain/Version/_baseSchema.js +2 -0
- package/lib/compiler/dependencyBuilder.d.ts +1 -0
- package/lib/compiler/dependencyBuilder.js +173 -54
- package/lib/compiler/routerBuilder.d.ts +4 -1
- package/lib/compiler/routerBuilder.js +22 -19
- package/lib/compiler/schemaBuilder.d.ts +5 -1
- package/lib/compiler/schemaBuilder.js +10 -0
- package/lib/context/BackendRuntimeContext.d.ts +9 -1
- package/lib/context/BackendRuntimeContext.js +13 -1
- package/lib/entities/Log.d.ts +2 -0
- package/lib/entities/Log.js +2 -0
- package/lib/entities/Version.d.ts +14 -0
- package/lib/entities/Version.js +43 -0
- package/lib/store/AsyncRowStore.d.ts +4 -1
- package/lib/store/AsyncRowStore.js +7 -0
- package/lib/store/CascadeStore.js +22 -0
- package/lib/store/RelationAuth/authorizationFilter.d.ts +11 -0
- package/lib/store/RelationAuth/authorizationFilter.js +110 -0
- package/lib/store/RelationAuth/continuation.d.ts +9 -0
- package/lib/store/RelationAuth/continuation.js +37 -0
- package/lib/store/RelationAuth/index.d.ts +141 -0
- package/lib/store/RelationAuth/index.js +1445 -0
- package/lib/store/RelationAuth/model.d.ts +38 -0
- package/lib/store/RelationAuth/model.js +27 -0
- package/lib/store/RelationAuth/userLookup.d.ts +21 -0
- package/lib/store/RelationAuth/userLookup.js +105 -0
- package/lib/store/SyncRowStore.d.ts +2 -1
- package/lib/store/SyncRowStore.js +3 -0
- package/lib/store/filter.fix.d.ts +128 -0
- package/lib/store/filter.fix.js +2876 -0
- package/lib/store/filter.js +14 -9
- package/lib/store/triggers.js +239 -44
- package/lib/types/Context.d.ts +2 -1
- package/lib/types/Demand.d.ts +11 -1
- package/lib/types/Environment.d.ts +13 -2
- package/lib/types/Exception.d.ts +3 -3
- package/lib/types/Exception.js +25 -22
- package/lib/types/Expression.d.ts +4 -1
- package/lib/types/Expression.js +1 -1
- package/lib/types/Storage.d.ts +15 -2
- package/lib/utils/SimpleConnector.js +5 -2
- package/lib/utils/decimal.js +2 -1
- package/lib/utils/fetch/index.ali.d.ts +3 -0
- package/lib/utils/fetch/index.ali.js +36 -0
- package/lib/utils/fetch/index.d.ts +3 -0
- package/lib/utils/fetch/index.js +7 -0
- package/lib/utils/fetch/index.mp.d.ts +5 -3
- package/lib/utils/fetch/index.mp.js +5 -282
- package/lib/utils/fetch/index.wechat.d.ts +5 -0
- package/lib/utils/fetch/index.wechat.js +180 -0
- package/lib/utils/fetch/mini-program.shared.d.ts +37 -0
- package/lib/utils/fetch/mini-program.shared.js +96 -0
- package/lib/utils/geo.d.ts +1 -1
- package/lib/utils/geo.js +1 -1
- package/lib/utils/random/random.ali.d.ts +6 -0
- package/lib/utils/random/random.ali.js +24 -0
- package/lib/utils/random/random.mp.d.ts +5 -1
- package/lib/utils/random/random.mp.js +11 -5
- package/lib/utils/random/random.native.d.ts +1 -1
- package/lib/utils/random/random.native.js +7 -5
- package/lib/utils/random/random.wechat.d.ts +1 -0
- package/lib/utils/random/random.wechat.js +13 -0
- package/package.json +8 -5
- package/es/store/RelationAuth.d.ts +0 -115
- package/es/store/RelationAuth.js +0 -1340
- package/lib/store/RelationAuth.d.ts +0 -115
- package/lib/store/RelationAuth.js +0 -1345
package/es/store/RelationAuth.js
DELETED
|
@@ -1,1340 +0,0 @@
|
|
|
1
|
-
import { assert } from '../utils/assert';
|
|
2
|
-
import { OakUnloggedInException, OakDataInvisibleException, OakOperationUnpermittedException, PrimaryKeyAttribute } from "../types";
|
|
3
|
-
import { AsyncContext } from "./AsyncRowStore";
|
|
4
|
-
import { checkFilterContains, combineFilters, getRelevantIds, translateCreateDataToFilter } from "./filter";
|
|
5
|
-
import { judgeRelation } from "./relation";
|
|
6
|
-
import { readOnlyActions } from '../actions/action';
|
|
7
|
-
import { difference, intersection, uniq, cloneDeep } from '../utils/toolkit';
|
|
8
|
-
import { SYSTEM_RESERVE_ENTITIES } from "../compiler/entities";
|
|
9
|
-
import { destructDirectUserPath, destructRelationPath } from "../utils/relationPath";
|
|
10
|
-
export class RelationAuth {
|
|
11
|
-
authDeduceRelationMap;
|
|
12
|
-
schema;
|
|
13
|
-
static SPECIAL_ENTITIES = SYSTEM_RESERVE_ENTITIES;
|
|
14
|
-
selectFreeEntities;
|
|
15
|
-
updateFreeDict;
|
|
16
|
-
constructor(schema, authDeduceRelationMap, selectFreeEntities, updateFreeDict) {
|
|
17
|
-
this.schema = schema;
|
|
18
|
-
this.selectFreeEntities = selectFreeEntities || [];
|
|
19
|
-
this.updateFreeDict = updateFreeDict || {};
|
|
20
|
-
this.authDeduceRelationMap = Object.assign({}, authDeduceRelationMap, {
|
|
21
|
-
modi: 'entity',
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
// 前台检查filter是否满足relation约束
|
|
25
|
-
checkRelationSync(entity, operation, context) {
|
|
26
|
-
if (context.isRoot()) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
// @oak-ignore 这里只有纯前台会跑到,一定是同步的
|
|
30
|
-
this.checkActions2(entity, operation, context);
|
|
31
|
-
}
|
|
32
|
-
// 后台检查filter是否满足relation约束
|
|
33
|
-
async checkRelationAsync(entity, operation, context) {
|
|
34
|
-
if (context.isRoot()) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
await this.checkActions2(entity, operation, context);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* 检查当前用户有无权限对filter约束的userRelation进行action操作
|
|
41
|
-
* @param context
|
|
42
|
-
* @param action
|
|
43
|
-
* @param filter
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
checkUserRelation(context, action, filter) {
|
|
47
|
-
const userId = context.getCurrentUserId();
|
|
48
|
-
/**
|
|
49
|
-
* 检查对某一个relationId是否有操作资格
|
|
50
|
-
* @param destRelationId
|
|
51
|
-
* @returns
|
|
52
|
-
*/
|
|
53
|
-
const checkOnRelationId = (entity, destRelationId, filter) => {
|
|
54
|
-
/**
|
|
55
|
-
* 找到能创建此relation的所有父级relation,只要user和其中一个有关联即可以通过
|
|
56
|
-
*/
|
|
57
|
-
// @oak-ignore
|
|
58
|
-
const relationAuths = context.select('relationAuth', {
|
|
59
|
-
data: {
|
|
60
|
-
id: 1,
|
|
61
|
-
pathId: 1,
|
|
62
|
-
path: {
|
|
63
|
-
id: 1,
|
|
64
|
-
sourceEntity: 1,
|
|
65
|
-
destEntity: 1,
|
|
66
|
-
value: 1,
|
|
67
|
-
recursive: 1,
|
|
68
|
-
},
|
|
69
|
-
sourceRelationId: 1,
|
|
70
|
-
sourceRelation: {
|
|
71
|
-
id: 1,
|
|
72
|
-
entity: 1,
|
|
73
|
-
entityId: 1,
|
|
74
|
-
},
|
|
75
|
-
destRelationId: 1,
|
|
76
|
-
destRelation: {
|
|
77
|
-
id: 1,
|
|
78
|
-
entity: 1,
|
|
79
|
-
entityId: 1,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
filter: {
|
|
83
|
-
destRelationId,
|
|
84
|
-
},
|
|
85
|
-
}, { dontCollect: true });
|
|
86
|
-
const checkRelationAuth = (relationAuth) => {
|
|
87
|
-
const { destRelation, sourceRelationId, path } = relationAuth;
|
|
88
|
-
assert(entity === destRelation.entity);
|
|
89
|
-
let destEntityFilter = this.makePathFilter(entity, path, this.schema, {
|
|
90
|
-
userRelation$entity: {
|
|
91
|
-
userId,
|
|
92
|
-
relationId: sourceRelationId,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
if (filter) {
|
|
96
|
-
destEntityFilter = combineFilters(entity, this.schema, [destEntityFilter, filter]);
|
|
97
|
-
}
|
|
98
|
-
return context.count(destRelation.entity, {
|
|
99
|
-
filter: destEntityFilter,
|
|
100
|
-
}, { ignoreAttrMiss: true });
|
|
101
|
-
};
|
|
102
|
-
if (relationAuths instanceof Promise) {
|
|
103
|
-
return relationAuths.then((ras) => Promise.all(ras.map(ra => checkRelationAuth(ra)))).then((result) => !!result.find(ele => {
|
|
104
|
-
assert(typeof ele === 'number');
|
|
105
|
-
return ele > 0;
|
|
106
|
-
}));
|
|
107
|
-
}
|
|
108
|
-
const result = relationAuths.map(
|
|
109
|
-
// @oak-ignore
|
|
110
|
-
ra => checkRelationAuth(ra));
|
|
111
|
-
return !!result.find(ele => ele > 0);
|
|
112
|
-
};
|
|
113
|
-
/**
|
|
114
|
-
* 检查对超过一个的relationId是否有操作资格
|
|
115
|
-
* @param relationFilter 限定relationId的条件
|
|
116
|
-
* @param intersection 是否交集(对每个relationId都得有权限)
|
|
117
|
-
* @param entityFilter 限定entity的条件
|
|
118
|
-
* @param entity 对应的entity
|
|
119
|
-
* @attention 这里为了代码复用,有可能是要通过本函数内部来查询确定entity;所以要保证,如果传入的relationFilter就可以确定relationId,则这里的entity参数必传。
|
|
120
|
-
* @returns
|
|
121
|
-
*/
|
|
122
|
-
const checkOnMultipleRelations = (relationFilter, intersection, entityFilter, entity) => {
|
|
123
|
-
let entity2 = entity;
|
|
124
|
-
const getRelationIds = () => {
|
|
125
|
-
const relevantIds = getRelevantIds(relationFilter);
|
|
126
|
-
if (relevantIds.length > 0) {
|
|
127
|
-
return relevantIds;
|
|
128
|
-
}
|
|
129
|
-
// @oak-ignore
|
|
130
|
-
const relations = context.select('relation', {
|
|
131
|
-
data: {
|
|
132
|
-
id: 1,
|
|
133
|
-
entity: 1,
|
|
134
|
-
entityId: 1,
|
|
135
|
-
},
|
|
136
|
-
filter: relationFilter
|
|
137
|
-
}, { dontCollect: true });
|
|
138
|
-
if (relations instanceof Promise) {
|
|
139
|
-
return relations.then((rs) => {
|
|
140
|
-
if (!entity2) {
|
|
141
|
-
entity2 = rs[0]?.entity;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
assert(entity2 === rs[0]?.entity);
|
|
145
|
-
}
|
|
146
|
-
return rs.map(ele => ele.id);
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
if (!entity2) {
|
|
150
|
-
entity2 = relations[0]?.entity;
|
|
151
|
-
}
|
|
152
|
-
else if (relations.length) {
|
|
153
|
-
assert(entity2 === relations[0]?.entity);
|
|
154
|
-
}
|
|
155
|
-
return relations.map(ele => ele.id);
|
|
156
|
-
};
|
|
157
|
-
const relationIds = getRelationIds();
|
|
158
|
-
if (relationIds instanceof Promise) {
|
|
159
|
-
return relationIds.then((ids) => {
|
|
160
|
-
assert(ids.length > 0);
|
|
161
|
-
return Promise.all(ids.map(ele => checkOnRelationId(entity2, ele, entityFilter))).then((value) => {
|
|
162
|
-
if (intersection) {
|
|
163
|
-
return !(value.includes(false));
|
|
164
|
-
}
|
|
165
|
-
return value.includes(true);
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
if (relationIds.length === 0) {
|
|
170
|
-
// 如果没有relationId(前端cache中),直接返回false
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
// @oak-ignore
|
|
174
|
-
const value = relationIds.map(ele => checkOnRelationId(entity2, ele, entityFilter));
|
|
175
|
-
if (intersection) {
|
|
176
|
-
return !(value.includes(false));
|
|
177
|
-
}
|
|
178
|
-
return value.includes(true);
|
|
179
|
-
};
|
|
180
|
-
if (action === 'create') {
|
|
181
|
-
const { entity, entityId, relationId } = filter;
|
|
182
|
-
assert(typeof entity === 'string');
|
|
183
|
-
let entityFilter;
|
|
184
|
-
if (entityId) {
|
|
185
|
-
entityFilter = {
|
|
186
|
-
id: entityId,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
// userEntityGrant会有这种情况,限定某个对象的范围进行授权
|
|
191
|
-
entityFilter = filter[entity];
|
|
192
|
-
}
|
|
193
|
-
if (relationId) {
|
|
194
|
-
// 如果指定relation,则测试该relation上是否可行
|
|
195
|
-
// 目前可能会有多个relationIds传入(userEntityGrant做测试),但一定是可以确定的relationId集合
|
|
196
|
-
return checkOnMultipleRelations({ id: relationId }, true, entityFilter, entity);
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
// 否则为测试“能否”有权限管理的资格,此时只要有一个就可以
|
|
200
|
-
// 这是为上层的menu所有,真正的创建不可能走到这里
|
|
201
|
-
// bug fixed,目前框架不支持entityId为null,所以这里暂时只支持entityId一种方式的测试
|
|
202
|
-
assert(entityId);
|
|
203
|
-
return checkOnMultipleRelations({
|
|
204
|
-
entity,
|
|
205
|
-
$or: [
|
|
206
|
-
{
|
|
207
|
-
entityId: {
|
|
208
|
-
$exists: false,
|
|
209
|
-
},
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
entityId,
|
|
213
|
-
}
|
|
214
|
-
]
|
|
215
|
-
}, false, entityFilter, entity);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
assert(action === 'remove');
|
|
220
|
-
// 有可能是删除多个userRelation,这时必须检查每一个relation都有对应的权限(有一个不能删除那就不能删除)
|
|
221
|
-
return checkOnMultipleRelations({
|
|
222
|
-
userRelation$relation: filter,
|
|
223
|
-
}, false, {
|
|
224
|
-
userRelation$entity: filter,
|
|
225
|
-
}, filter.entity);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
checkOperateSpecialEntities2(entity2, action, filter, context) {
|
|
229
|
-
switch (entity2) {
|
|
230
|
-
case 'userRelation': {
|
|
231
|
-
assert(!(filter instanceof Array));
|
|
232
|
-
return this.checkUserRelation(context, action, filter);
|
|
233
|
-
}
|
|
234
|
-
case 'user': {
|
|
235
|
-
// 对用户的操作由应用自己去管理权限,这里只检查grant/revoke
|
|
236
|
-
if (['grant', 'revoke'].includes(action)) {
|
|
237
|
-
// assert(filter && Object.keys(filter).length === 1, 'grant/revoke只能操作userRelation$user');
|
|
238
|
-
// assert(filter!.hasOwnProperty('userRelation$user'), 'grant/revoke只能操作userRelation$user');
|
|
239
|
-
return true;
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
// 应用允许用户操作其它用户的逻辑请通过编写类型为relation的checker去控制,在这里不能加以限制
|
|
243
|
-
return true;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
case 'modi': {
|
|
247
|
-
/** 正常情况下对modi的生成是在触发器下openRootMode,不会走到这里
|
|
248
|
-
* 但是有些例外,如extraFile如果在modi中创建,上传成功之后需要显式生成一条modi,这时对modi的
|
|
249
|
-
* 检查可以转化为对其父entity的update权限检查
|
|
250
|
-
*/
|
|
251
|
-
assert(action === 'create');
|
|
252
|
-
const { entity, entityId } = filter;
|
|
253
|
-
return this.checkOperation(entity, {
|
|
254
|
-
action: 'update',
|
|
255
|
-
data: {},
|
|
256
|
-
filter: {
|
|
257
|
-
id: entityId,
|
|
258
|
-
},
|
|
259
|
-
}, context);
|
|
260
|
-
}
|
|
261
|
-
case 'relation': {
|
|
262
|
-
// 创建relation目前不支持,以后再说
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
case 'userEntityGrant': {
|
|
266
|
-
// userEntityGrant的创建相当于授权,领取相当于赋权
|
|
267
|
-
if (['create', 'update', 'remove'].includes(action)) {
|
|
268
|
-
if (action === 'create') {
|
|
269
|
-
const { relationEntity, relationEntityFilter, relationIds } = filter;
|
|
270
|
-
return this.checkOperateSpecialEntities2('userRelation', 'create', {
|
|
271
|
-
entity: relationEntity,
|
|
272
|
-
[relationEntity]: relationEntityFilter,
|
|
273
|
-
relationId: {
|
|
274
|
-
$in: relationIds,
|
|
275
|
-
},
|
|
276
|
-
}, context);
|
|
277
|
-
}
|
|
278
|
-
return this.checkOperateSpecialEntities2('userRelation', 'action', {
|
|
279
|
-
relation: {
|
|
280
|
-
userEntityGrant$relation: filter,
|
|
281
|
-
},
|
|
282
|
-
}, context);
|
|
283
|
-
}
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
286
|
-
default: {
|
|
287
|
-
assert(false, `对象${entity2}的权限控制没有加以控制`);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
getDeducedEntityFilters(entity, filter, actions, context) {
|
|
292
|
-
const entityFilters = [
|
|
293
|
-
{
|
|
294
|
-
entity,
|
|
295
|
-
filter,
|
|
296
|
-
actions,
|
|
297
|
-
}
|
|
298
|
-
];
|
|
299
|
-
if (this.authDeduceRelationMap[entity]) {
|
|
300
|
-
assert(this.authDeduceRelationMap[entity] === 'entity');
|
|
301
|
-
let { entity: deduceEntity, entityId: deduceEntityId } = filter;
|
|
302
|
-
let deduceFilter = {};
|
|
303
|
-
if (deduceEntity && deduceEntityId) {
|
|
304
|
-
deduceFilter = { id: deduceEntityId };
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
// 也可能是用cascade方式进行查找,这里有时候filter上会带有两个不同的entity目标,尚未处理(todo!)
|
|
308
|
-
const { ref } = this.schema[entity].attributes.entity;
|
|
309
|
-
assert(ref instanceof Array);
|
|
310
|
-
for (const refEntity of ref) {
|
|
311
|
-
if (filter[refEntity]) {
|
|
312
|
-
deduceEntity = refEntity;
|
|
313
|
-
deduceFilter = filter[refEntity];
|
|
314
|
-
break;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
const getRecursiveDeducedFilters = (deduceEntity, deduceFilter) => {
|
|
319
|
-
const excludeActions = readOnlyActions.concat([ /* 'create', 'remove' */]);
|
|
320
|
-
const updateActions = this.schema[deduceEntity].actions.filter((a) => !excludeActions.includes(a));
|
|
321
|
-
/* if (!RelationAuth.SPECIAL_ENTITIES.includes(deduceEntity as string)) {
|
|
322
|
-
return this.getDeducedEntityFilters(deduceEntity, deduceFilter, actions[0] === 'select' ? actions : updateActions, context);
|
|
323
|
-
}
|
|
324
|
-
return []; */
|
|
325
|
-
return this.getDeducedEntityFilters(deduceEntity, deduceFilter, actions[0] === 'select' ? actions : updateActions, context);
|
|
326
|
-
};
|
|
327
|
-
if (deduceEntity && deduceFilter) {
|
|
328
|
-
const deducedSelections = getRecursiveDeducedFilters(deduceEntity, deduceFilter);
|
|
329
|
-
if (deducedSelections instanceof Promise) {
|
|
330
|
-
return deducedSelections.then((ds) => {
|
|
331
|
-
entityFilters.push(...ds);
|
|
332
|
-
return entityFilters;
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
entityFilters.push(...deducedSelections);
|
|
336
|
-
return entityFilters;
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
/**
|
|
340
|
-
* 这种情况说明从filter中无法确定相应的deduceFilter,需要查找该实体对应的entity/entityId来进行推导。
|
|
341
|
-
* 这种情况一般发生在entity1 -> entity2上,此时entity2应该是一个固定id查询的filter
|
|
342
|
-
* 在这里先假设如果碰到了list类型的filter,直接不使用deduce路径上的对象来推导
|
|
343
|
-
*/
|
|
344
|
-
// @oak-ignore
|
|
345
|
-
const rows2 = context.select(entity, {
|
|
346
|
-
data: {
|
|
347
|
-
id: 1,
|
|
348
|
-
entity: 1,
|
|
349
|
-
entityId: 1,
|
|
350
|
-
},
|
|
351
|
-
filter,
|
|
352
|
-
indexFrom: 0,
|
|
353
|
-
count: 10,
|
|
354
|
-
}, { dontCollect: true, blockTrigger: true });
|
|
355
|
-
const dealWithData = (rows) => {
|
|
356
|
-
// 这里如果entity指向不同的实体,一般出现这样的查询,则其权限应当不由这条deduce路径处理
|
|
357
|
-
// 同上,如果找到的行数大于1行,说明deduce路径上的对象不确定,也暂不处理 by Xc 20230725
|
|
358
|
-
if (rows.length > 1) {
|
|
359
|
-
if (process.env.NODE_ENV === 'development') {
|
|
360
|
-
console.warn(`进行deduce推导时找到了${rows.length}行${entity}数据`);
|
|
361
|
-
}
|
|
362
|
-
return entityFilters;
|
|
363
|
-
}
|
|
364
|
-
else if (rows.length === 0) {
|
|
365
|
-
// 说明没有找到行,这时候有一种可能是modi。这时候只能假设是指定id更新了,其它情况很难处理。by Xc
|
|
366
|
-
if (filter.id) {
|
|
367
|
-
// 用modi对应的entity/entityId来判定
|
|
368
|
-
// @oak-ignore
|
|
369
|
-
const modies = context.select('modi', {
|
|
370
|
-
data: {
|
|
371
|
-
id: 1,
|
|
372
|
-
data: 1,
|
|
373
|
-
entity: 1,
|
|
374
|
-
entityId: 1,
|
|
375
|
-
},
|
|
376
|
-
filter: {
|
|
377
|
-
filter,
|
|
378
|
-
}
|
|
379
|
-
}, {});
|
|
380
|
-
const getModiEntity = (modies) => {
|
|
381
|
-
if (modies[0]) {
|
|
382
|
-
const { entity, entityId } = modies[0];
|
|
383
|
-
entityFilters.push({
|
|
384
|
-
entity: entity,
|
|
385
|
-
filter: {
|
|
386
|
-
id: entityId,
|
|
387
|
-
},
|
|
388
|
-
actions: ['update'],
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
return entityFilters;
|
|
392
|
-
};
|
|
393
|
-
if (modies instanceof Promise) {
|
|
394
|
-
return modies.then((modies2) => getModiEntity(modies2));
|
|
395
|
-
}
|
|
396
|
-
return getModiEntity(modies);
|
|
397
|
-
}
|
|
398
|
-
return entityFilters;
|
|
399
|
-
}
|
|
400
|
-
const { entity: deducedEntity, entityId: deducedEntityId } = rows[0];
|
|
401
|
-
if (!deducedEntity || !deducedEntityId) {
|
|
402
|
-
// 这种情况会出现在前台缓存里
|
|
403
|
-
return entityFilters;
|
|
404
|
-
}
|
|
405
|
-
const result = getRecursiveDeducedFilters(deducedEntity, {
|
|
406
|
-
id: deducedEntityId,
|
|
407
|
-
});
|
|
408
|
-
if (result instanceof Promise) {
|
|
409
|
-
return result.then((r2) => {
|
|
410
|
-
entityFilters.push(...r2);
|
|
411
|
-
return entityFilters;
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
entityFilters.push(...result);
|
|
415
|
-
return entityFilters;
|
|
416
|
-
};
|
|
417
|
-
if (rows2 instanceof Promise) {
|
|
418
|
-
return rows2.then((r2) => dealWithData(r2));
|
|
419
|
-
}
|
|
420
|
-
return dealWithData(rows2);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return entityFilters;
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* 对于selection,解构出projection最底层的对象,如果最底层的对象可以被访问,则父对象一定可以
|
|
427
|
-
* 假设对象的关系是:A -> B -> C,此时的projection如果是:
|
|
428
|
-
* 1)A: { B: { }}
|
|
429
|
-
* 查询的底层对象是A
|
|
430
|
-
* 2)C: { B$c : { A$b: {}} }
|
|
431
|
-
* 这样写,查询的最底层对象仍然是A
|
|
432
|
-
*
|
|
433
|
-
* 但如果又有一个对象D -> C,此时的projection如果是:
|
|
434
|
-
* 1)C: { B$c : { A$b: {}}, D$c: {} }
|
|
435
|
-
* 查询的底层对象是A和D
|
|
436
|
-
* 2)B: { C: { D$c: {}} }
|
|
437
|
-
* 这种情况,查询的最底层对象是B和D,但这种查询的写法在真实应用中出现的概率非常低,可能想到的情况只有D是静态维表(select free)
|
|
438
|
-
* B和D如果同时是业务敏感对象,出现在同一个页面(逻辑)中的case应当不存在。
|
|
439
|
-
*
|
|
440
|
-
* 所以本函数不处理所有向父对象方向上的递归
|
|
441
|
-
* @param entity
|
|
442
|
-
* @param operation
|
|
443
|
-
*/
|
|
444
|
-
getLowestSelections(entity, selection) {
|
|
445
|
-
const leafSelections = [];
|
|
446
|
-
const { data, filter } = selection;
|
|
447
|
-
/**
|
|
448
|
-
* 把filter中与所有key无关的属性全部去掉,避免后面处理的干扰
|
|
449
|
-
* (实际中出现了#node和$expr跨越多层filter,很难处理)
|
|
450
|
-
* @param entity
|
|
451
|
-
* @param originFilter
|
|
452
|
-
*/
|
|
453
|
-
const refineFilter = (entity, originFilter) => {
|
|
454
|
-
let filter2 = {};
|
|
455
|
-
for (const attr in originFilter) {
|
|
456
|
-
if (['$and', '$or', '$xor'].includes(attr)) {
|
|
457
|
-
const refined = originFilter[attr].map((f) => refineFilter(entity, f)).filter((ele) => !!ele);
|
|
458
|
-
if (refined.length > 1) {
|
|
459
|
-
filter2[attr] = refined;
|
|
460
|
-
}
|
|
461
|
-
else if (refined.length > 0) {
|
|
462
|
-
filter2 = combineFilters(entity, this.schema, [filter2, refined[0]]);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
else if (attr === '$not') {
|
|
466
|
-
const refined = refineFilter(entity, originFilter[attr]);
|
|
467
|
-
if (refined) {
|
|
468
|
-
Object.assign(filter2, {
|
|
469
|
-
[attr]: refined,
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
else if (attr.startsWith('$') || attr.startsWith('#')) {
|
|
474
|
-
// $expr, $text这些条件全部忽略
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
const rel = judgeRelation(this.schema, entity, attr);
|
|
478
|
-
if (rel === 1) {
|
|
479
|
-
if ([PrimaryKeyAttribute, 'entity', 'entityId'].includes(attr) || this.schema[entity].attributes[attr]?.type === 'ref') {
|
|
480
|
-
filter2[attr] = originFilter[attr];
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
else if (rel === 2) {
|
|
484
|
-
const refined = refineFilter(attr, originFilter[attr]);
|
|
485
|
-
if (refined) {
|
|
486
|
-
filter2[attr] = refined;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
else if (typeof rel === 'string') {
|
|
490
|
-
const refined = refineFilter(rel, originFilter[attr]);
|
|
491
|
-
if (refined) {
|
|
492
|
-
filter2[attr] = refined;
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
else if (rel instanceof Array) {
|
|
496
|
-
const { '#sqp': sqp } = originFilter[attr];
|
|
497
|
-
// 一对多的路径在actionAuth中目前只能表达in这一种语义
|
|
498
|
-
if (!sqp || sqp === 'in') {
|
|
499
|
-
const refined = refineFilter(rel[0], originFilter[attr]);
|
|
500
|
-
if (refined) {
|
|
501
|
-
filter2[attr] = refined;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
return filter2; // 直接返回,如果没有关联的entity,如Area,则返回空对象,后续还有selectFree的检查
|
|
508
|
-
};
|
|
509
|
-
const refined = refineFilter(entity, filter || {});
|
|
510
|
-
const getFn = (entity2, projection2, filter2) => {
|
|
511
|
-
let hasOneToMany = false;
|
|
512
|
-
for (const attr in projection2) {
|
|
513
|
-
const rel = judgeRelation(this.schema, entity2, attr);
|
|
514
|
-
if (rel instanceof Array) {
|
|
515
|
-
const [e, foreignKey] = rel;
|
|
516
|
-
if (foreignKey) {
|
|
517
|
-
assert(!this.authDeduceRelationMap[e]);
|
|
518
|
-
hasOneToMany = true;
|
|
519
|
-
const { data: subProjection, filter: subFilter } = projection2[attr];
|
|
520
|
-
const refinedSubFilter = subFilter && refineFilter(e, subFilter);
|
|
521
|
-
getFn(e, subProjection, combineFilters(e, this.schema, [refinedSubFilter, {
|
|
522
|
-
[foreignKey.slice(0, foreignKey.length - 2)]: filter2,
|
|
523
|
-
}]));
|
|
524
|
-
}
|
|
525
|
-
else {
|
|
526
|
-
if (!this.authDeduceRelationMap[e]) {
|
|
527
|
-
hasOneToMany = true;
|
|
528
|
-
const { data: subProjection, filter: subFilter } = projection2[attr];
|
|
529
|
-
const refinedSubFilter = subFilter && refineFilter(e, subFilter);
|
|
530
|
-
getFn(e, subProjection, combineFilters(e, this.schema, [refinedSubFilter, {
|
|
531
|
-
[entity2]: filter2,
|
|
532
|
-
}]));
|
|
533
|
-
}
|
|
534
|
-
else {
|
|
535
|
-
assert(this.authDeduceRelationMap[e] === 'entity');
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
if (!hasOneToMany) {
|
|
541
|
-
leafSelections.push({
|
|
542
|
-
entity: entity2,
|
|
543
|
-
filter: filter2,
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
getFn(entity, data, refined);
|
|
548
|
-
return leafSelections;
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* 对于operation,解构出一个树形结构,以方便自顶向下的进行访问
|
|
552
|
-
* 但对于deduce的子对象,不必再向底层查看
|
|
553
|
-
* @param entity
|
|
554
|
-
* @param selection
|
|
555
|
-
*/
|
|
556
|
-
destructOperation(entity2, operation2, userId) {
|
|
557
|
-
/**
|
|
558
|
-
* 对create动作,把data中的cascade部分剔除后作为filter参与后续的检验
|
|
559
|
-
* @param operation
|
|
560
|
-
* @returns
|
|
561
|
-
*/
|
|
562
|
-
const makeCreateFilter = (entity, operation) => {
|
|
563
|
-
const { data, filter, bornAt } = operation;
|
|
564
|
-
assert(!(data instanceof Array));
|
|
565
|
-
if (data) {
|
|
566
|
-
return translateCreateDataToFilter(this.schema, entity, data, !!bornAt);
|
|
567
|
-
}
|
|
568
|
-
return filter;
|
|
569
|
-
};
|
|
570
|
-
const addChild = (node, path, child) => {
|
|
571
|
-
// 在这里要把可以被node deduce出来的child处理掉
|
|
572
|
-
const paths = path.split('$');
|
|
573
|
-
assert(paths.length >= 2);
|
|
574
|
-
if (this.authDeduceRelationMap[child.entity] === paths[1]) {
|
|
575
|
-
assert(paths[1] === 'entity', '当前只支持entity外键上的deduce');
|
|
576
|
-
return false;
|
|
577
|
-
}
|
|
578
|
-
if (node.children[path]) {
|
|
579
|
-
if (node.children[path] instanceof Array) {
|
|
580
|
-
node.children[path].push(child);
|
|
581
|
-
}
|
|
582
|
-
else {
|
|
583
|
-
node.children[path] = [node.children[path], child];
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
Object.assign(node.children, {
|
|
588
|
-
[path]: child,
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
return true;
|
|
592
|
-
};
|
|
593
|
-
const destructInner = (entity, operation,
|
|
594
|
-
// extraFilter?: ED[T2]['Filter'],
|
|
595
|
-
path, child, hasParent, extraFilter) => {
|
|
596
|
-
const { action, data, filter } = operation;
|
|
597
|
-
const filter2 = action === 'create' ? makeCreateFilter(entity, operation) : filter ? cloneDeep(filter) : {};
|
|
598
|
-
assert(filter2);
|
|
599
|
-
if (extraFilter) {
|
|
600
|
-
Object.assign(filter2, extraFilter);
|
|
601
|
-
}
|
|
602
|
-
// const filter3 = extraFilter ? combineFilters(entity, schema, [filter2, extraFilter]) : filter2;
|
|
603
|
-
const me = {
|
|
604
|
-
entity: entity,
|
|
605
|
-
filter: filter2,
|
|
606
|
-
children: {},
|
|
607
|
-
action,
|
|
608
|
-
};
|
|
609
|
-
let root = me;
|
|
610
|
-
// 如果当前对象是一个toModi的,意味着它的cascadeUpdate会全部被变为modi去缓存,因此不需要再向下检查了
|
|
611
|
-
// modi被apply时,这些modi产生的更新才会被实际检查
|
|
612
|
-
// 这里可能有问题,再思考思考 by Xc 20231111
|
|
613
|
-
const isModiUpdate = this.schema[entity].toModi && action !== 'remove';
|
|
614
|
-
if (child) {
|
|
615
|
-
assert(path);
|
|
616
|
-
addChild(me, path, child);
|
|
617
|
-
}
|
|
618
|
-
assert(!(data instanceof Array));
|
|
619
|
-
for (const attr in data) {
|
|
620
|
-
const rel = judgeRelation(this.schema, entity, attr, !!operation.bornAt);
|
|
621
|
-
if (rel === 2 && !isModiUpdate) {
|
|
622
|
-
assert(root === me && !hasParent, 'cascadeUpdate必须是树结构,避免森林');
|
|
623
|
-
const mtoOperation = data[attr];
|
|
624
|
-
root = destructInner(attr, mtoOperation, `${entity}$entity`, me);
|
|
625
|
-
}
|
|
626
|
-
else if (typeof rel === 'string' && !isModiUpdate) {
|
|
627
|
-
assert(root === me && !hasParent, 'cascadeUpdate必须是树结构,避免森林');
|
|
628
|
-
root = destructInner(rel, data[attr], `${entity}$${attr}`, me);
|
|
629
|
-
}
|
|
630
|
-
else if (rel instanceof Array && !isModiUpdate) {
|
|
631
|
-
const [e, f] = rel;
|
|
632
|
-
const otmOperations = data[attr];
|
|
633
|
-
/**
|
|
634
|
-
* 这里目前在cascadeUpdate的过程中,只有当一对多个userRelation的操作需要将entity和entityId复制到子对象上
|
|
635
|
-
* 因为对userRelation的判断是走的特殊路径,无法利用父对象的actionAuth
|
|
636
|
-
* 其它对象情况不需要复制,因为应用中必须要能保证(前台传来的)父对象的filter不依赖于子对象的条件
|
|
637
|
-
*/
|
|
638
|
-
let extraFilter = undefined;
|
|
639
|
-
if (e === 'userRelation' && entity !== 'user') {
|
|
640
|
-
me.userRelations = [];
|
|
641
|
-
extraFilter = {
|
|
642
|
-
entity,
|
|
643
|
-
entityId: filter2.id,
|
|
644
|
-
};
|
|
645
|
-
const dealWithUserRelation = (userRelation) => {
|
|
646
|
-
const { action, data } = userRelation;
|
|
647
|
-
if (action === 'create') {
|
|
648
|
-
const attrs = Object.keys(data);
|
|
649
|
-
assert(difference(attrs, Object.keys(this.schema.userRelation.attributes).concat('id')).length === 0);
|
|
650
|
-
if (data.userId === userId) {
|
|
651
|
-
me.userRelations?.push(data);
|
|
652
|
-
}
|
|
653
|
-
assert(filter2.id);
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
if (otmOperations instanceof Array) {
|
|
657
|
-
otmOperations.forEach((otmOperation) => dealWithUserRelation(otmOperation));
|
|
658
|
-
}
|
|
659
|
-
else {
|
|
660
|
-
dealWithUserRelation(otmOperations);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
if (otmOperations instanceof Array) {
|
|
664
|
-
otmOperations.forEach((otmOperation) => {
|
|
665
|
-
const son = destructInner(e, otmOperation, undefined, undefined, true, extraFilter);
|
|
666
|
-
addChild(me, attr, son);
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
else {
|
|
670
|
-
const son = destructInner(e, otmOperations, undefined, undefined, true, extraFilter);
|
|
671
|
-
addChild(me, attr, son);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
return root;
|
|
676
|
-
};
|
|
677
|
-
return destructInner(entity2, operation2);
|
|
678
|
-
}
|
|
679
|
-
makePathFilter(entity, path, schema, filter) {
|
|
680
|
-
const { value, recursive } = path;
|
|
681
|
-
if (value === '') {
|
|
682
|
-
assert(!recursive);
|
|
683
|
-
return filter;
|
|
684
|
-
}
|
|
685
|
-
const paths = value.split('.');
|
|
686
|
-
const makeRecursiveFilter = (recursiveDepth) => {
|
|
687
|
-
if (recursiveDepth > 0) {
|
|
688
|
-
return {
|
|
689
|
-
$or: [
|
|
690
|
-
filter,
|
|
691
|
-
{
|
|
692
|
-
parent: makeRecursiveFilter(recursiveDepth - 1)
|
|
693
|
-
}
|
|
694
|
-
]
|
|
695
|
-
};
|
|
696
|
-
}
|
|
697
|
-
return filter;
|
|
698
|
-
};
|
|
699
|
-
const makeInner = (idx, e2) => {
|
|
700
|
-
const attr = paths[idx];
|
|
701
|
-
if (idx === paths.length) {
|
|
702
|
-
if (!recursive) {
|
|
703
|
-
return filter;
|
|
704
|
-
}
|
|
705
|
-
else {
|
|
706
|
-
// 在最后一个对象上存在递归,用or连接处理
|
|
707
|
-
const { recursiveDepth } = schema[e2];
|
|
708
|
-
assert(recursiveDepth > 0);
|
|
709
|
-
return makeRecursiveFilter(recursiveDepth);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
else {
|
|
713
|
-
const rel = judgeRelation(schema, e2, attr);
|
|
714
|
-
let e3;
|
|
715
|
-
if (rel === 2) {
|
|
716
|
-
e3 = attr;
|
|
717
|
-
}
|
|
718
|
-
else if (typeof rel === 'string') {
|
|
719
|
-
e3 = rel;
|
|
720
|
-
}
|
|
721
|
-
else {
|
|
722
|
-
assert(rel instanceof Array);
|
|
723
|
-
e3 = rel[0];
|
|
724
|
-
}
|
|
725
|
-
const f = makeInner(idx + 1, e3);
|
|
726
|
-
return {
|
|
727
|
-
[attr]: f,
|
|
728
|
-
};
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
return makeInner(0, entity);
|
|
732
|
-
}
|
|
733
|
-
/**
|
|
734
|
-
* 对所有满足操作要求的actionAuth加以判断,找到可以满足当前用户身份的actionAuth
|
|
735
|
-
* @param entity
|
|
736
|
-
* @param filter
|
|
737
|
-
* @param actionAuths
|
|
738
|
-
* @param context
|
|
739
|
-
* @return
|
|
740
|
-
*/
|
|
741
|
-
filterActionAuths(entity, filter, actionAuths, context) {
|
|
742
|
-
const result = actionAuths.map((ele) => {
|
|
743
|
-
const { path, relation, relationId } = ele;
|
|
744
|
-
// 在cache中,可能出现relation外键指向的对象为null的情况,要容错
|
|
745
|
-
if (relationId) {
|
|
746
|
-
if (relation) {
|
|
747
|
-
const { userRelation$relation: userRelations } = relation;
|
|
748
|
-
if (userRelations.length > 0) {
|
|
749
|
-
const entityIds = uniq(userRelations.map(ele => ele.entityId));
|
|
750
|
-
const pathFilter = this.makePathFilter(entity, path, this.schema, {
|
|
751
|
-
id: entityIds.length > 0 ? {
|
|
752
|
-
$in: entityIds,
|
|
753
|
-
} : entityIds[0],
|
|
754
|
-
});
|
|
755
|
-
const contains = checkFilterContains(entity, context, pathFilter, filter, true);
|
|
756
|
-
if (contains instanceof Promise) {
|
|
757
|
-
return contains.then((c) => {
|
|
758
|
-
if (c) {
|
|
759
|
-
return ele;
|
|
760
|
-
}
|
|
761
|
-
return;
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
if (contains) {
|
|
765
|
-
return ele;
|
|
766
|
-
}
|
|
767
|
-
return;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return;
|
|
771
|
-
}
|
|
772
|
-
// 说明是通过userId关联
|
|
773
|
-
const pathFilter = this.makePathFilter(entity, path, this.schema, {
|
|
774
|
-
id: context.getCurrentUserId(),
|
|
775
|
-
});
|
|
776
|
-
const contains = checkFilterContains(entity, context, pathFilter, filter, true);
|
|
777
|
-
if (contains instanceof Promise) {
|
|
778
|
-
return contains.then((c) => {
|
|
779
|
-
if (c) {
|
|
780
|
-
return ele;
|
|
781
|
-
}
|
|
782
|
-
return;
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
if (contains) {
|
|
786
|
-
return ele;
|
|
787
|
-
}
|
|
788
|
-
});
|
|
789
|
-
if (result.find(ele => ele instanceof Promise)) {
|
|
790
|
-
return Promise.all(result).then((r2) => r2.filter(ele => !!ele));
|
|
791
|
-
}
|
|
792
|
-
return result.filter(ele => !!ele);
|
|
793
|
-
}
|
|
794
|
-
/**
|
|
795
|
-
* 对于有些特殊的查询(带很多$or的查询,多发生在系统级别),单个actionAuth无法满足,需要共同加以判定
|
|
796
|
-
* @param entity
|
|
797
|
-
* @param filter
|
|
798
|
-
* @param actionAuths
|
|
799
|
-
* @param context
|
|
800
|
-
* @param actions
|
|
801
|
-
*/
|
|
802
|
-
checkActionAuthInGroup(entity, filter, actionAuths, context) {
|
|
803
|
-
const filters = actionAuths.filter(ele => ele.path.destEntity === entity).map((ele) => {
|
|
804
|
-
const { path, relation, relationId } = ele;
|
|
805
|
-
if (relationId) {
|
|
806
|
-
const pathFilter = this.makePathFilter(entity, path, this.schema, {
|
|
807
|
-
userRelation$entity: {
|
|
808
|
-
userId: context.getCurrentUserId(),
|
|
809
|
-
relationId,
|
|
810
|
-
}
|
|
811
|
-
});
|
|
812
|
-
return pathFilter;
|
|
813
|
-
}
|
|
814
|
-
// 说明是通过userId关联
|
|
815
|
-
return this.makePathFilter(entity, path, this.schema, {
|
|
816
|
-
id: context.getCurrentUserId(),
|
|
817
|
-
});
|
|
818
|
-
});
|
|
819
|
-
const groupFilter = combineFilters(entity, this.schema, filters, true);
|
|
820
|
-
if (groupFilter) {
|
|
821
|
-
return checkFilterContains(entity, context, groupFilter, filter, true);
|
|
822
|
-
}
|
|
823
|
-
return false;
|
|
824
|
-
}
|
|
825
|
-
checkSelection(entity, selection, context) {
|
|
826
|
-
const leafSelections = this.getLowestSelections(entity, selection);
|
|
827
|
-
const deducedLeafSelections = leafSelections.map(({ entity, filter }) => this.getDeducedEntityFilters(entity, filter, ['select'], context));
|
|
828
|
-
const checkDeducedLeafSelections = (dlSelections2) => {
|
|
829
|
-
const dlSelections = dlSelections2.filter((ele) => {
|
|
830
|
-
const entities = ele.map(ele2 => ele2.entity);
|
|
831
|
-
// 同一个leaf的deducedSelections中只要有一个能通过就足够了
|
|
832
|
-
if (intersection(this.selectFreeEntities, entities).length > 0) {
|
|
833
|
-
return false;
|
|
834
|
-
}
|
|
835
|
-
if (intersection(RelationAuth.SPECIAL_ENTITIES, entities).length > 0) {
|
|
836
|
-
// todo
|
|
837
|
-
return false;
|
|
838
|
-
}
|
|
839
|
-
return true;
|
|
840
|
-
});
|
|
841
|
-
if (dlSelections.length === 0) {
|
|
842
|
-
return true;
|
|
843
|
-
}
|
|
844
|
-
if (!context.getCurrentUserId()) {
|
|
845
|
-
throw new OakUnloggedInException();
|
|
846
|
-
}
|
|
847
|
-
const allEntities = [];
|
|
848
|
-
dlSelections.forEach((ele) => ele.forEach(({ entity }) => {
|
|
849
|
-
allEntities.push(entity);
|
|
850
|
-
}));
|
|
851
|
-
const actionAuths = context.select('actionAuth', {
|
|
852
|
-
data: {
|
|
853
|
-
id: 1,
|
|
854
|
-
pathId: 1,
|
|
855
|
-
path: {
|
|
856
|
-
id: 1,
|
|
857
|
-
value: 1,
|
|
858
|
-
sourceEntity: 1,
|
|
859
|
-
destEntity: 1,
|
|
860
|
-
recursive: 1,
|
|
861
|
-
},
|
|
862
|
-
deActions: 1,
|
|
863
|
-
relationId: 1,
|
|
864
|
-
},
|
|
865
|
-
filter: {
|
|
866
|
-
deActions: {
|
|
867
|
-
$contains: 'select',
|
|
868
|
-
},
|
|
869
|
-
path: {
|
|
870
|
-
destEntity: {
|
|
871
|
-
$in: allEntities,
|
|
872
|
-
},
|
|
873
|
-
},
|
|
874
|
-
}
|
|
875
|
-
}, { dontCollect: true, ignoreAttrMiss: true });
|
|
876
|
-
/**
|
|
877
|
-
* 返回的结果中,第一层为leafNode,必须全通过,第二层为单个leafNode上的deduce,通过一个就可以
|
|
878
|
-
* @param result
|
|
879
|
-
* @returns
|
|
880
|
-
*/
|
|
881
|
-
const checkResult = (result) => {
|
|
882
|
-
let idx = 0;
|
|
883
|
-
for (const r1 of result) {
|
|
884
|
-
const r2 = r1.find(ele => ele === true);
|
|
885
|
-
if (!r2) {
|
|
886
|
-
if (process.env.NODE_ENV === 'development') {
|
|
887
|
-
console.warn('对象的select权限被否决,请检查', dlSelections[idx]);
|
|
888
|
-
}
|
|
889
|
-
idx++;
|
|
890
|
-
return false;
|
|
891
|
-
}
|
|
892
|
-
idx++;
|
|
893
|
-
}
|
|
894
|
-
return true;
|
|
895
|
-
};
|
|
896
|
-
if (actionAuths instanceof Promise) {
|
|
897
|
-
assert(context instanceof AsyncContext);
|
|
898
|
-
return actionAuths.then((aas) => Promise.all(dlSelections.map((ele) => Promise.all(ele.map((ele2) => this.checkActionAuthInGroup(ele2.entity, ele2.filter, aas, context))))).then((result) => checkResult(result)));
|
|
899
|
-
}
|
|
900
|
-
return checkResult(dlSelections.map(ele => ele.map(
|
|
901
|
-
// @oak-ignore
|
|
902
|
-
ele2 => this.checkActionAuthInGroup(ele2.entity, ele2.filter, actionAuths, context))));
|
|
903
|
-
};
|
|
904
|
-
if (deducedLeafSelections[0] instanceof Promise) {
|
|
905
|
-
return Promise.all(deducedLeafSelections)
|
|
906
|
-
.then((dls) => checkDeducedLeafSelections(dls));
|
|
907
|
-
}
|
|
908
|
-
return checkDeducedLeafSelections(deducedLeafSelections);
|
|
909
|
-
}
|
|
910
|
-
/**
|
|
911
|
-
* 此函数判定一个结点是否能通过权限检测,同时寻找该结点本身对象上成立的actionAuth,用于本结点子孙结点的快速检测
|
|
912
|
-
* 如果结点因其deduce的对象通过了检测,其被推断对象的actionAuth无法用于更低对象的权限检测
|
|
913
|
-
* @param node
|
|
914
|
-
* @param context
|
|
915
|
-
* @returns
|
|
916
|
-
*/
|
|
917
|
-
findActionAuthsOnNode(node, context) {
|
|
918
|
-
const { entity, filter, action, userRelations } = node;
|
|
919
|
-
assert(filter);
|
|
920
|
-
const deducedEntityFilters2 = this.getDeducedEntityFilters(entity, filter, [action], context);
|
|
921
|
-
/**
|
|
922
|
-
* 搜索判定是否允许自建对象,自建的条件是 path = '',destEntity === entity
|
|
923
|
-
* @param actionAuths
|
|
924
|
-
* @returns
|
|
925
|
-
*/
|
|
926
|
-
const findOwnCreateUserRelation = (actionAuths) => {
|
|
927
|
-
if (userRelations && userRelations.length > 0) {
|
|
928
|
-
const ars = actionAuths.filter((ar) => !!userRelations.find((ur) => ur.relationId === ar.relationId) && ar.path.value === '' && ar.path.destEntity === entity);
|
|
929
|
-
if (ars.length > 0) {
|
|
930
|
-
return ars;
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
};
|
|
934
|
-
const actionAuthOnEntities = [];
|
|
935
|
-
const dealWithDeducedEntityFilters = (deducedEntityFilters) => {
|
|
936
|
-
const specialEntities = deducedEntityFilters.filter(ele => RelationAuth.SPECIAL_ENTITIES.includes(ele.entity));
|
|
937
|
-
const unspecicalEntities = deducedEntityFilters.filter(ele => !RelationAuth.SPECIAL_ENTITIES.includes(ele.entity));
|
|
938
|
-
const result = [];
|
|
939
|
-
if (specialEntities.length > 0) {
|
|
940
|
-
// 对于deduce出来的special对象,直接判定create应该问题不大,否则写起来太烦琐(具体情况遇到了再调试)
|
|
941
|
-
result.push(...specialEntities.map(
|
|
942
|
-
// @oak-ignore
|
|
943
|
-
ele => this.checkOperateSpecialEntities2(ele.entity, ele.entity === entity ? node.action : 'create', ele.filter, context)));
|
|
944
|
-
}
|
|
945
|
-
if (unspecicalEntities.length > 0) {
|
|
946
|
-
const allEntities = unspecicalEntities.map(ele => ele.entity);
|
|
947
|
-
const allActions = uniq(unspecicalEntities.map(ele => ele.actions).flat());
|
|
948
|
-
// @oak-ignore
|
|
949
|
-
const actionAuths2 = context.select('actionAuth', {
|
|
950
|
-
data: {
|
|
951
|
-
id: 1,
|
|
952
|
-
path: {
|
|
953
|
-
id: 1,
|
|
954
|
-
destEntity: 1,
|
|
955
|
-
sourceEntity: 1,
|
|
956
|
-
value: 1,
|
|
957
|
-
recursive: 1,
|
|
958
|
-
},
|
|
959
|
-
pathId: 1,
|
|
960
|
-
deActions: 1,
|
|
961
|
-
relationId: 1,
|
|
962
|
-
relation: {
|
|
963
|
-
id: 1,
|
|
964
|
-
entity: 1,
|
|
965
|
-
entityId: 1,
|
|
966
|
-
userRelation$relation: {
|
|
967
|
-
$entity: 'userRelation',
|
|
968
|
-
data: {
|
|
969
|
-
id: 1,
|
|
970
|
-
entity: 1,
|
|
971
|
-
entityId: 1,
|
|
972
|
-
userId: 1,
|
|
973
|
-
relationId: 1,
|
|
974
|
-
},
|
|
975
|
-
filter: {
|
|
976
|
-
userId: context.getCurrentUserId(),
|
|
977
|
-
},
|
|
978
|
-
},
|
|
979
|
-
},
|
|
980
|
-
},
|
|
981
|
-
filter: {
|
|
982
|
-
path: {
|
|
983
|
-
destEntity: {
|
|
984
|
-
$in: allEntities,
|
|
985
|
-
}
|
|
986
|
-
},
|
|
987
|
-
deActions: {
|
|
988
|
-
$overlaps: allActions,
|
|
989
|
-
},
|
|
990
|
-
}
|
|
991
|
-
}, { dontCollect: true, ignoreAttrMiss: true });
|
|
992
|
-
const checkActionAuths = (actionAuths) => {
|
|
993
|
-
const created = findOwnCreateUserRelation(actionAuths);
|
|
994
|
-
if (created) {
|
|
995
|
-
actionAuthOnEntities.push(...created);
|
|
996
|
-
return true;
|
|
997
|
-
}
|
|
998
|
-
const result = deducedEntityFilters.map((ele) => {
|
|
999
|
-
const ars2 = actionAuths.filter(ele2 => ele2.path.destEntity === ele.entity && intersection(ele2.deActions, ele.actions).length > 0 // 这里只要overlap就可以了
|
|
1000
|
-
);
|
|
1001
|
-
const ars3 = this.filterActionAuths(ele.entity, ele.filter, ars2, context);
|
|
1002
|
-
const checkFilteredArs = (actionAuths2) => {
|
|
1003
|
-
if (actionAuths2.length > 0) {
|
|
1004
|
-
if (ele.entity === entity) {
|
|
1005
|
-
actionAuthOnEntities.push(...actionAuths2);
|
|
1006
|
-
}
|
|
1007
|
-
return true;
|
|
1008
|
-
}
|
|
1009
|
-
return false;
|
|
1010
|
-
};
|
|
1011
|
-
if (ars3 instanceof Promise) {
|
|
1012
|
-
return ars3.then((ars4) => checkFilteredArs(ars4));
|
|
1013
|
-
}
|
|
1014
|
-
return checkFilteredArs(ars3);
|
|
1015
|
-
});
|
|
1016
|
-
if (result.find(ele => ele instanceof Promise)) {
|
|
1017
|
-
return Promise.all(result).then((r2) => r2.includes(true));
|
|
1018
|
-
}
|
|
1019
|
-
return result.includes(true);
|
|
1020
|
-
};
|
|
1021
|
-
if (actionAuths2 instanceof Promise) {
|
|
1022
|
-
result.push(actionAuths2.then(
|
|
1023
|
-
// @oak-ignore
|
|
1024
|
-
(ars2) => checkActionAuths(ars2)));
|
|
1025
|
-
}
|
|
1026
|
-
else {
|
|
1027
|
-
result.push(
|
|
1028
|
-
// @oak-ignore
|
|
1029
|
-
checkActionAuths(actionAuths2));
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
if (result.find(ele => ele instanceof Promise)) {
|
|
1033
|
-
return Promise.all(result).then((r2) => {
|
|
1034
|
-
// r2中只有一个通过就能通过
|
|
1035
|
-
if (r2.includes(true)) {
|
|
1036
|
-
return actionAuthOnEntities;
|
|
1037
|
-
}
|
|
1038
|
-
return false;
|
|
1039
|
-
});
|
|
1040
|
-
}
|
|
1041
|
-
if (result.includes(true)) {
|
|
1042
|
-
return actionAuthOnEntities;
|
|
1043
|
-
}
|
|
1044
|
-
return false;
|
|
1045
|
-
};
|
|
1046
|
-
if (deducedEntityFilters2 instanceof Promise) {
|
|
1047
|
-
return deducedEntityFilters2.then((def2) => dealWithDeducedEntityFilters(def2));
|
|
1048
|
-
}
|
|
1049
|
-
return dealWithDeducedEntityFilters(deducedEntityFilters2);
|
|
1050
|
-
}
|
|
1051
|
-
checkOperationTree2(tree, context) {
|
|
1052
|
-
const checkNode = (node, actionAuths) => {
|
|
1053
|
-
const checkChildren = (legalPaths) => {
|
|
1054
|
-
const { children } = node;
|
|
1055
|
-
const childPath = Object.keys(children);
|
|
1056
|
-
if (childPath.length === 0) {
|
|
1057
|
-
return true;
|
|
1058
|
-
}
|
|
1059
|
-
const childResult = childPath.map((childPath) => {
|
|
1060
|
-
const child = children[childPath];
|
|
1061
|
-
const childEntity = child instanceof Array ? child[0].entity : child.entity;
|
|
1062
|
-
// 这里如果该子结点能deduce到父,则直接通过
|
|
1063
|
-
if (this.authDeduceRelationMap[childEntity]) {
|
|
1064
|
-
assert(this.authDeduceRelationMap[childEntity] === 'entity');
|
|
1065
|
-
const rel = judgeRelation(this.schema, childEntity, childPath);
|
|
1066
|
-
if (rel === 2) {
|
|
1067
|
-
return true;
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
const pathToParent = childPath.endsWith('$entity') ? node.entity : childPath.split('$')[1];
|
|
1071
|
-
if (child instanceof Array) {
|
|
1072
|
-
const childActions = child.map(ele => ele.action);
|
|
1073
|
-
const childLegalAuths = legalPaths.map((ele) => {
|
|
1074
|
-
const { path: { value: pv }, relationId } = ele;
|
|
1075
|
-
const pv2 = pv ? `${pathToParent}.${pv}` : pathToParent;
|
|
1076
|
-
// @oak-ignore
|
|
1077
|
-
return context.select('actionAuth', {
|
|
1078
|
-
data: {
|
|
1079
|
-
id: 1,
|
|
1080
|
-
},
|
|
1081
|
-
filter: {
|
|
1082
|
-
path: {
|
|
1083
|
-
value: pv2,
|
|
1084
|
-
destEntity: childEntity,
|
|
1085
|
-
},
|
|
1086
|
-
deActions: {
|
|
1087
|
-
$overlaps: childActions,
|
|
1088
|
-
},
|
|
1089
|
-
relationId: relationId || {
|
|
1090
|
-
$exists: false,
|
|
1091
|
-
},
|
|
1092
|
-
}
|
|
1093
|
-
}, { dontCollect: true });
|
|
1094
|
-
});
|
|
1095
|
-
if (childLegalAuths[0] instanceof Promise) {
|
|
1096
|
-
return Promise.all(childLegalAuths).then((clas) => child.map(
|
|
1097
|
-
// @oak-ignore
|
|
1098
|
-
(c) => checkNode(c, clas.flat())));
|
|
1099
|
-
}
|
|
1100
|
-
return child.map(
|
|
1101
|
-
// @oak-ignore
|
|
1102
|
-
(c) => checkNode(c, childLegalAuths.flat()));
|
|
1103
|
-
}
|
|
1104
|
-
const childLegalAuths = legalPaths.map((ele) => {
|
|
1105
|
-
const { path: { value: pv }, relationId } = ele;
|
|
1106
|
-
const pv2 = pv ? `${pathToParent}.${pv}` : pathToParent;
|
|
1107
|
-
// @oak-ignore
|
|
1108
|
-
return context.select('actionAuth', {
|
|
1109
|
-
data: {
|
|
1110
|
-
id: 1,
|
|
1111
|
-
},
|
|
1112
|
-
filter: {
|
|
1113
|
-
path: {
|
|
1114
|
-
value: pv2,
|
|
1115
|
-
destEntity: childEntity,
|
|
1116
|
-
},
|
|
1117
|
-
deActions: {
|
|
1118
|
-
$overlaps: child.action,
|
|
1119
|
-
},
|
|
1120
|
-
relationId: relationId || {
|
|
1121
|
-
$exists: false,
|
|
1122
|
-
},
|
|
1123
|
-
}
|
|
1124
|
-
}, { dontCollect: true });
|
|
1125
|
-
}).flat();
|
|
1126
|
-
if (childLegalAuths[0] instanceof Promise) {
|
|
1127
|
-
return Promise.all(childLegalAuths).then(
|
|
1128
|
-
// @oak-ignore
|
|
1129
|
-
(clas) => checkNode(child, clas.flat()));
|
|
1130
|
-
}
|
|
1131
|
-
// @oak-ignore
|
|
1132
|
-
return checkNode(child, childLegalAuths);
|
|
1133
|
-
}).flat();
|
|
1134
|
-
if (childResult[0] instanceof Promise) {
|
|
1135
|
-
return Promise.all(childResult).then((r) => !r.includes(false));
|
|
1136
|
-
}
|
|
1137
|
-
return !childResult.includes(false);
|
|
1138
|
-
};
|
|
1139
|
-
// 先根据parent传下来的合法auths来搜寻,只需要查找actionAuth,降低开销
|
|
1140
|
-
// 这里有可能父结点根据actionAuths能通过,但子结点需要重新搜索父结点上的新actionAuths才能通过吗?应该不存在这种情况。 by Xc 20230824
|
|
1141
|
-
if (actionAuths && actionAuths.length > 0) {
|
|
1142
|
-
return checkChildren(actionAuths);
|
|
1143
|
-
}
|
|
1144
|
-
// 如果这个entity是updateFree,直接过掉到子结点判定
|
|
1145
|
-
const { action, entity } = node;
|
|
1146
|
-
if (this.updateFreeDict[entity] && this.updateFreeDict[entity].includes(action)) {
|
|
1147
|
-
return checkChildren([]);
|
|
1148
|
-
}
|
|
1149
|
-
// 没有能根据父亲传下来的actionAuth判定,只能自己找
|
|
1150
|
-
const result = this.findActionAuthsOnNode(node, context);
|
|
1151
|
-
const checkResult = (result2) => {
|
|
1152
|
-
if (result2 === false) {
|
|
1153
|
-
return false;
|
|
1154
|
-
}
|
|
1155
|
-
// 如果是对user对象操作通过,需要增添一条虚假的的actionAuth
|
|
1156
|
-
if (node.entity === 'user') {
|
|
1157
|
-
result2.push({
|
|
1158
|
-
id: 'temp',
|
|
1159
|
-
pathId: 'path_temp',
|
|
1160
|
-
path: {
|
|
1161
|
-
id: 'path_temp',
|
|
1162
|
-
destEntity: 'user',
|
|
1163
|
-
sourceEntity: 'any',
|
|
1164
|
-
value: '',
|
|
1165
|
-
$$createAt$$: 1,
|
|
1166
|
-
$$updateAt$$: 1,
|
|
1167
|
-
$$seq$$: 123,
|
|
1168
|
-
recursive: false,
|
|
1169
|
-
},
|
|
1170
|
-
$$createAt$$: 1,
|
|
1171
|
-
$$updateAt$$: 1,
|
|
1172
|
-
$$seq$$: 123,
|
|
1173
|
-
deActions: [node.action],
|
|
1174
|
-
});
|
|
1175
|
-
}
|
|
1176
|
-
return checkChildren(result2);
|
|
1177
|
-
};
|
|
1178
|
-
if (result instanceof Promise) {
|
|
1179
|
-
return result.then((r2) => checkResult(r2));
|
|
1180
|
-
}
|
|
1181
|
-
return checkResult(result);
|
|
1182
|
-
};
|
|
1183
|
-
return checkNode(tree);
|
|
1184
|
-
}
|
|
1185
|
-
checkOperation(entity, operation, context) {
|
|
1186
|
-
const { action, filter, data } = operation;
|
|
1187
|
-
const userId = context.getCurrentUserId();
|
|
1188
|
-
if (!userId) {
|
|
1189
|
-
throw new OakUnloggedInException();
|
|
1190
|
-
}
|
|
1191
|
-
if (!filter && (!data || action !== 'create')) {
|
|
1192
|
-
if (process.env.NODE_ENV === 'development') {
|
|
1193
|
-
console.warn('operation不能没有限制条件', operation);
|
|
1194
|
-
}
|
|
1195
|
-
return false;
|
|
1196
|
-
}
|
|
1197
|
-
const updateTree = this.destructOperation(entity, operation, userId);
|
|
1198
|
-
return this.checkOperationTree2(updateTree, context);
|
|
1199
|
-
}
|
|
1200
|
-
/**
|
|
1201
|
-
* 检查一个operation是否能被通过权限测试
|
|
1202
|
-
* 一个cascadeOperation是一棵树形结构:
|
|
1203
|
-
* * 对于select,只要叶子通过其父结点必然通过;
|
|
1204
|
-
* * 对于update,自顶向下进行检查,若父亲被权限S通过,则只需要检查子对于S有没有相对路径上的actionAuth
|
|
1205
|
-
* 另外在update中,还需要考虑自建userRelation的case(例如在电子商务网站上购买商品,创建订单同时创建用户和订单的关系)
|
|
1206
|
-
* @param entity
|
|
1207
|
-
* @param operation
|
|
1208
|
-
* @param context
|
|
1209
|
-
* @param actions
|
|
1210
|
-
* @returns
|
|
1211
|
-
*/
|
|
1212
|
-
checkActions2(entity, operation, context, actions) {
|
|
1213
|
-
const { action } = operation;
|
|
1214
|
-
if (!action || readOnlyActions.includes(action)) {
|
|
1215
|
-
const result = this.checkSelection(entity, operation, context);
|
|
1216
|
-
if (result instanceof Promise) {
|
|
1217
|
-
return result.then((r) => {
|
|
1218
|
-
if (!r) {
|
|
1219
|
-
throw new OakDataInvisibleException(entity, operation, context.getCurrentUserId());
|
|
1220
|
-
}
|
|
1221
|
-
});
|
|
1222
|
-
}
|
|
1223
|
-
if (!result) {
|
|
1224
|
-
throw new OakDataInvisibleException(entity, operation, context.getCurrentUserId());
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
else {
|
|
1228
|
-
const result = this.checkOperation(entity, operation, context);
|
|
1229
|
-
if (result instanceof Promise) {
|
|
1230
|
-
return result.then((r) => {
|
|
1231
|
-
if (!r) {
|
|
1232
|
-
throw new OakOperationUnpermittedException(entity, operation, context.getCurrentUserId());
|
|
1233
|
-
}
|
|
1234
|
-
});
|
|
1235
|
-
}
|
|
1236
|
-
if (!result) {
|
|
1237
|
-
throw new OakOperationUnpermittedException(entity, operation, context.getCurrentUserId());
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
|
-
;
|
|
1243
|
-
/**
|
|
1244
|
-
* 获取有对entity进行actions操作权限的userRelation关系
|
|
1245
|
-
* @param params
|
|
1246
|
-
* @param context
|
|
1247
|
-
*/
|
|
1248
|
-
export async function getUserRelationsByActions(params, context) {
|
|
1249
|
-
const { entity, filter, actions, overlap } = params;
|
|
1250
|
-
const actionAuthfilter = {
|
|
1251
|
-
path: {
|
|
1252
|
-
destEntity: entity,
|
|
1253
|
-
},
|
|
1254
|
-
};
|
|
1255
|
-
if (overlap) {
|
|
1256
|
-
Object.assign(actionAuthfilter, {
|
|
1257
|
-
deActions: {
|
|
1258
|
-
$overlaps: actions,
|
|
1259
|
-
},
|
|
1260
|
-
});
|
|
1261
|
-
}
|
|
1262
|
-
else {
|
|
1263
|
-
Object.assign(actionAuthfilter, {
|
|
1264
|
-
deActions: {
|
|
1265
|
-
$contains: actions,
|
|
1266
|
-
},
|
|
1267
|
-
});
|
|
1268
|
-
}
|
|
1269
|
-
const actionAuths = await context.select('actionAuth', {
|
|
1270
|
-
data: {
|
|
1271
|
-
id: 1,
|
|
1272
|
-
pathId: 1,
|
|
1273
|
-
path: {
|
|
1274
|
-
id: 1,
|
|
1275
|
-
value: 1,
|
|
1276
|
-
destEntity: 1,
|
|
1277
|
-
recursive: 1,
|
|
1278
|
-
},
|
|
1279
|
-
relationId: 1,
|
|
1280
|
-
relation: {
|
|
1281
|
-
id: 1,
|
|
1282
|
-
entity: 1,
|
|
1283
|
-
},
|
|
1284
|
-
},
|
|
1285
|
-
filter: actionAuthfilter,
|
|
1286
|
-
}, { dontCollect: true });
|
|
1287
|
-
const getUserRelations = async (urAuths) => {
|
|
1288
|
-
// 相同的path可以groupBy掉
|
|
1289
|
-
const urAuthDict2 = {};
|
|
1290
|
-
urAuths.forEach((auth) => {
|
|
1291
|
-
const { path, relationId } = auth;
|
|
1292
|
-
const { value, recursive } = path;
|
|
1293
|
-
if (!urAuthDict2[value]) {
|
|
1294
|
-
urAuthDict2[value] = [[relationId], recursive];
|
|
1295
|
-
}
|
|
1296
|
-
else if (!urAuthDict2[value][0].includes(relationId)) {
|
|
1297
|
-
assert(urAuthDict2[value][1] === recursive);
|
|
1298
|
-
urAuthDict2[value][0].push(relationId);
|
|
1299
|
-
}
|
|
1300
|
-
});
|
|
1301
|
-
const userRelations = await Promise.all(Object.keys(urAuthDict2).map(async (path) => {
|
|
1302
|
-
const [relationIds, recursive] = urAuthDict2[path];
|
|
1303
|
-
const { projection, getData } = destructRelationPath(context.getSchema(), entity, path, {
|
|
1304
|
-
relationId: {
|
|
1305
|
-
$in: relationIds,
|
|
1306
|
-
},
|
|
1307
|
-
}, recursive);
|
|
1308
|
-
const rows = await context.select(entity, {
|
|
1309
|
-
data: projection,
|
|
1310
|
-
filter,
|
|
1311
|
-
}, { dontCollect: true });
|
|
1312
|
-
const urs = rows.map(ele => getData(ele)).flat().filter(ele => !!ele);
|
|
1313
|
-
return urs;
|
|
1314
|
-
}));
|
|
1315
|
-
return userRelations.flat();
|
|
1316
|
-
};
|
|
1317
|
-
const getDirectUserEntities = async (directAuths) => {
|
|
1318
|
-
const userEntities = await Promise.all(directAuths.map(async ({ path }) => {
|
|
1319
|
-
const { value, recursive } = path;
|
|
1320
|
-
assert(!recursive);
|
|
1321
|
-
const { getData, projection } = destructDirectUserPath(context.getSchema(), entity, value);
|
|
1322
|
-
const rows = await context.select(entity, {
|
|
1323
|
-
data: projection,
|
|
1324
|
-
filter,
|
|
1325
|
-
}, { dontCollect: true });
|
|
1326
|
-
const userEntities = rows.map(ele => getData(ele)).flat().filter(ele => !!ele);
|
|
1327
|
-
return userEntities;
|
|
1328
|
-
}));
|
|
1329
|
-
return userEntities.flat();
|
|
1330
|
-
};
|
|
1331
|
-
const urAuths2 = actionAuths.filter(ele => !!ele.relationId // 有relation说明通过userRelation关联
|
|
1332
|
-
);
|
|
1333
|
-
const directAuths2 = actionAuths.filter(ele => !ele.relationId // 没relation说明通过user关联
|
|
1334
|
-
);
|
|
1335
|
-
const [userRelations, userEntities] = await Promise.all([getUserRelations(urAuths2), getDirectUserEntities(directAuths2)]);
|
|
1336
|
-
return {
|
|
1337
|
-
userRelations,
|
|
1338
|
-
userEntities,
|
|
1339
|
-
};
|
|
1340
|
-
}
|