@puca/puca-core 13.10.8 → 14.0.0
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/dist/core/services/abstract-controller.js +1 -1
- package/dist/core/services/abstract-controller.js.map +1 -1
- package/dist/core/services/auth.service.js +9 -9
- package/dist/core/services/auth.service.js.map +1 -1
- package/dist/core/services/comunication/comunication-http.service.js +2 -2
- package/dist/core/services/comunication/comunication-http.service.js.map +1 -1
- package/dist/core/services/comunication/comunication.service.js +1 -1
- package/dist/core/services/comunication/comunication.service.js.map +1 -1
- package/dist/core/services/crud-base-testable.service.js +2 -2
- package/dist/core/services/crud-base-testable.service.js.map +1 -1
- package/dist/core/services/crud-front-base.service.js +9 -8
- package/dist/core/services/crud-front-base.service.js.map +1 -1
- package/dist/core/services/domain-life-cycle.service.js +6 -6
- package/dist/core/services/domain-life-cycle.service.js.map +1 -1
- package/dist/core/services/email.service.js +8 -8
- package/dist/core/services/email.service.js.map +1 -1
- package/dist/core/services/followers.service.js +1 -1
- package/dist/core/services/followers.service.js.map +1 -1
- package/dist/core/services/incremental-search.service.js +5 -5
- package/dist/core/services/incremental-search.service.js.map +1 -1
- package/dist/core/services/install.service.js +41 -51
- package/dist/core/services/install.service.js.map +1 -1
- package/dist/core/services/job-executor/job-executor.service.js +4 -4
- package/dist/core/services/job-executor/job-executor.service.js.map +1 -1
- package/dist/core/services/kpi.service.js +1 -1
- package/dist/core/services/kpi.service.js.map +1 -1
- package/dist/core/services/llm/llm.service.js +15 -15
- package/dist/core/services/llm/llm.service.js.map +1 -1
- package/dist/core/services/llm/token-budget.service.js +4 -4
- package/dist/core/services/llm/token-budget.service.js.map +1 -1
- package/dist/core/services/logger.d.ts +5 -5
- package/dist/core/services/logger.js +79 -61
- package/dist/core/services/logger.js.map +1 -1
- package/dist/core/services/migration/migration.service.d.ts +5 -1
- package/dist/core/services/migration/migration.service.js +138 -20
- package/dist/core/services/migration/migration.service.js.map +1 -1
- package/dist/core/services/monitor.js +4 -4
- package/dist/core/services/monitor.js.map +1 -1
- package/dist/core/services/mq/mq.service.js +21 -15
- package/dist/core/services/mq/mq.service.js.map +1 -1
- package/dist/core/services/notification.service.js +1 -1
- package/dist/core/services/notification.service.js.map +1 -1
- package/dist/core/services/plugin.service.js +4 -4
- package/dist/core/services/plugin.service.js.map +1 -1
- package/dist/core/services/report.service.js +4 -4
- package/dist/core/services/report.service.js.map +1 -1
- package/dist/core/services/server-life-cycle.service.js +4 -4
- package/dist/core/services/server-life-cycle.service.js.map +1 -1
- package/dist/core/services/storage/storage.service.js +1 -1
- package/dist/core/services/storage/storage.service.js.map +1 -1
- package/dist/core/services/system-parameters-envs.service.js +3 -3
- package/dist/core/services/system-parameters-envs.service.js.map +1 -1
- package/dist/core/services/table-permission.service.js +185 -227
- package/dist/core/services/table-permission.service.js.map +1 -1
- package/dist/core/services/tag.service.js +5 -4
- package/dist/core/services/tag.service.js.map +1 -1
- package/dist/core/services/user.service.d.ts +11 -3
- package/dist/core/services/user.service.js +37 -15
- package/dist/core/services/user.service.js.map +1 -1
- package/dist/core/services/web-socket.service.js +1 -1
- package/dist/core/services/web-socket.service.js.map +1 -1
- package/dist/essential/module-config.service.js +2 -2
- package/dist/essential/module-config.service.js.map +1 -1
- package/dist/models/crud-controller-especification-environment.d.ts +21 -4
- package/dist/models/crud-controller-especification.d.ts +5 -5
- package/dist/models/crud-controller-especification.js.map +1 -1
- package/dist/models/table-dictionary.d.ts +13 -7
- package/dist/orm/cases/bo/ddl.service.d.ts +2 -1
- package/dist/orm/cases/bo/ddl.service.js +51 -3
- package/dist/orm/cases/bo/ddl.service.js.map +1 -1
- package/dist/orm/cases/bo/interfaces.d.ts +6 -3
- package/dist/orm/cases/bo/interfaces.js.map +1 -1
- package/dist/orm/cases/bo/sql.service.js +9 -7
- package/dist/orm/cases/bo/sql.service.js.map +1 -1
- package/dist/orm/cases/vanilla/interfaces.d.ts +16 -10
- package/dist/orm/cases/vanilla/interfaces.js +3 -3
- package/dist/orm/cases/vanilla/interfaces.js.map +1 -1
- package/dist/orm/cases/vanilla/sql-builder/ddl.service.d.ts +2 -1
- package/dist/orm/cases/vanilla/sql-builder/ddl.service.js +6 -5
- package/dist/orm/cases/vanilla/sql-builder/ddl.service.js.map +1 -1
- package/dist/orm/cases/vanilla/sql-builder/mutation.service.js +17 -9
- package/dist/orm/cases/vanilla/sql-builder/mutation.service.js.map +1 -1
- package/dist/orm/cases/vanilla/sql-builder/select.service.d.ts +3 -2
- package/dist/orm/cases/vanilla/sql-builder/select.service.js +39 -22
- package/dist/orm/cases/vanilla/sql-builder/select.service.js.map +1 -1
- package/dist/orm/cases/vanilla/sql-builder/sql-builder.service.d.ts +2 -1
- package/dist/orm/cases/vanilla/sql-builder/sql-builder.service.js +3 -0
- package/dist/orm/cases/vanilla/sql-builder/sql-builder.service.js.map +1 -1
- package/dist/orm/plugins/numeric-sql-format-from-named/index.js +1 -1
- package/dist/orm/plugins/numeric-sql-format-from-named/index.js.map +1 -1
- package/dist/orm/services/cache-repository/cache-repository-redis.js +5 -5
- package/dist/orm/services/cache-repository/cache-repository-redis.js.map +1 -1
- package/dist/orm/services/cache.service.js +1 -1
- package/dist/orm/services/cache.service.js.map +1 -1
- package/dist/orm/services/field-dictionary-cache.service.d.ts +17 -0
- package/dist/orm/services/field-dictionary-cache.service.js +147 -0
- package/dist/orm/services/field-dictionary-cache.service.js.map +1 -0
- package/dist/orm/services/index.d.ts +2 -0
- package/dist/orm/services/index.js +4 -2
- package/dist/orm/services/index.js.map +1 -1
- package/dist/orm/services/managers/database-manager.service.js +10 -8
- package/dist/orm/services/managers/database-manager.service.js.map +1 -1
- package/dist/orm/services/managers/puca-orm-client.service.js +7 -1
- package/dist/orm/services/managers/puca-orm-client.service.js.map +1 -1
- package/dist/orm/services/repository.d.ts +11 -3
- package/dist/orm/services/repository.js +27 -19
- package/dist/orm/services/repository.js.map +1 -1
- package/dist/orm/services/sql/sql-security-layer.service.d.ts +116 -3
- package/dist/orm/services/sql/sql-security-layer.service.js +1449 -20
- package/dist/orm/services/sql/sql-security-layer.service.js.map +1 -1
- package/dist/orm/services/sql/sql.service.d.ts +2 -2
- package/dist/orm/services/sql/sql.service.js +2 -2
- package/dist/orm/services/sql/sql.service.js.map +1 -1
- package/dist/orm/services/sql/table-permission.helper.d.ts +5 -0
- package/dist/orm/services/sql/table-permission.helper.js +187 -0
- package/dist/orm/services/sql/table-permission.helper.js.map +1 -0
- package/dist/orm/services/sql-executor.js +2 -2
- package/dist/orm/services/sql-executor.js.map +1 -1
- package/dist/orm/services/transaction.js +10 -11
- package/dist/orm/services/transaction.js.map +1 -1
- package/dist/packages/crm/crm.service.js +5 -4
- package/dist/packages/crm/crm.service.js.map +1 -1
- package/dist/packages/functions/function.service.d.ts +18 -0
- package/dist/packages/functions/function.service.js +10 -0
- package/dist/packages/functions/function.service.js.map +1 -1
- package/dist/plugins/file-system/file-system.service.js +2 -2
- package/dist/plugins/file-system/file-system.service.js.map +1 -1
- package/dist/plugins/file-system/shared-file-system.service.js +2 -2
- package/dist/plugins/file-system/shared-file-system.service.js.map +1 -1
- package/dist/plugins/grpc-client.service.js +1 -1
- package/dist/plugins/grpc-client.service.js.map +1 -1
- package/dist/plugins/multi-tenant/puca-hub.service.js +11 -11
- package/dist/plugins/multi-tenant/puca-hub.service.js.map +1 -1
- package/dist/plugins/nestjs/puca-auth.guard.js +1 -1
- package/dist/plugins/nestjs/puca-auth.guard.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/fuql-grammar.js +0 -24
- package/fuql-grammar.ne +0 -24
- package/package.json +1 -1
- package/dist/orm/services/sql/sql-layer.service.d.ts +0 -125
- package/dist/orm/services/sql/sql-layer.service.js +0 -1270
- package/dist/orm/services/sql/sql-layer.service.js.map +0 -1
- package/dist/orm/services/sql/sql-status-layer.service.d.ts +0 -0
- package/dist/orm/services/sql/sql-status-layer.service.js +0 -1
- package/dist/orm/services/sql/sql-status-layer.service.js.map +0 -1
|
@@ -3,18 +3,300 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SqlSecurityLayerService = void 0;
|
|
4
4
|
const _ = require("lodash");
|
|
5
5
|
const logger_1 = require("../../../core/services/logger");
|
|
6
|
-
const
|
|
6
|
+
const interfaces_1 = require("../../cases/vanilla/interfaces");
|
|
7
|
+
const helpers_1 = require("./helpers");
|
|
8
|
+
const table_permission_helper_1 = require("./table-permission.helper");
|
|
9
|
+
const user_access_control_mode_1 = require("../../../models/user-access-control-mode");
|
|
10
|
+
const is_primitive_type_1 = require("../../../helpers/is-primitive-type");
|
|
11
|
+
const DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER = process.env.DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER === 'false' ? false : true;
|
|
7
12
|
var PermissionSystemStatus;
|
|
8
13
|
(function (PermissionSystemStatus) {
|
|
9
14
|
PermissionSystemStatus["Off"] = "off";
|
|
10
15
|
PermissionSystemStatus["On"] = "on";
|
|
11
16
|
})(PermissionSystemStatus || (PermissionSystemStatus = {}));
|
|
17
|
+
const isWhere = (field) => {
|
|
18
|
+
if (typeof field === 'string') {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const whereField = field;
|
|
22
|
+
if ('operator' in whereField) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if ('exists' in whereField) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
if ('and' in whereField) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if ('or' in whereField) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if ('not' in whereField) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
class UnsafeResolver {
|
|
40
|
+
unsafeResolveStringValue({ value, alias, parentAlias, path, }) {
|
|
41
|
+
if (!value) {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
if (value.startsWith('$')) {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
const r = value
|
|
48
|
+
.replace(new RegExp('{{alias}}', 'g'), alias)
|
|
49
|
+
.replace(new RegExp('{{parentAlias}}', 'g'), parentAlias);
|
|
50
|
+
return r;
|
|
51
|
+
}
|
|
52
|
+
unsafeResolveValue({ field, alias, parentAlias, path, parentTableName, }) {
|
|
53
|
+
if (!field) {
|
|
54
|
+
return field;
|
|
55
|
+
}
|
|
56
|
+
if (typeof field === 'string') {
|
|
57
|
+
return this.unsafeResolveStringValue({
|
|
58
|
+
value: field,
|
|
59
|
+
alias,
|
|
60
|
+
parentAlias,
|
|
61
|
+
path,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (typeof field === 'number' || typeof field === 'boolean') {
|
|
65
|
+
throw new Error(`invalid typeof ${field} === ${typeof field === 'number'}`);
|
|
66
|
+
}
|
|
67
|
+
const resolveOver = () => {
|
|
68
|
+
if (!field.over) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const partitionBy = (field.over.partitionBy || []).map(p => {
|
|
72
|
+
if (typeof p === 'string') {
|
|
73
|
+
return p;
|
|
74
|
+
}
|
|
75
|
+
return this.unsafeResolveValue({
|
|
76
|
+
field: p,
|
|
77
|
+
alias,
|
|
78
|
+
parentAlias,
|
|
79
|
+
path,
|
|
80
|
+
parentTableName,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
const orderBy = (field.over.orderBy || []).map(o => {
|
|
84
|
+
if (typeof o === 'string') {
|
|
85
|
+
return o;
|
|
86
|
+
}
|
|
87
|
+
const result = this.unsafeResolveValue({
|
|
88
|
+
field: o,
|
|
89
|
+
alias,
|
|
90
|
+
parentAlias,
|
|
91
|
+
path,
|
|
92
|
+
parentTableName,
|
|
93
|
+
});
|
|
94
|
+
if (typeof result === 'string') {
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
return Object.assign(Object.assign({}, result), { sort: o.sort });
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
partitionBy,
|
|
101
|
+
orderBy,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
if ('function' in field) {
|
|
105
|
+
return {
|
|
106
|
+
function: field.function,
|
|
107
|
+
args: (field.args || []).map(arg => {
|
|
108
|
+
if (isWhere(arg)) {
|
|
109
|
+
return this.unsafeResolveWhere({
|
|
110
|
+
where: arg,
|
|
111
|
+
alias,
|
|
112
|
+
parentAlias,
|
|
113
|
+
path,
|
|
114
|
+
parentTableName,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return this.unsafeResolveValue({
|
|
118
|
+
field: arg,
|
|
119
|
+
alias,
|
|
120
|
+
parentAlias,
|
|
121
|
+
path,
|
|
122
|
+
parentTableName,
|
|
123
|
+
});
|
|
124
|
+
}),
|
|
125
|
+
alias: this.unsafeResolveStringValue({
|
|
126
|
+
value: field.alias,
|
|
127
|
+
alias,
|
|
128
|
+
parentAlias,
|
|
129
|
+
path,
|
|
130
|
+
}),
|
|
131
|
+
cast: field.cast,
|
|
132
|
+
over: resolveOver(),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if ('field' in field) {
|
|
136
|
+
return {
|
|
137
|
+
table: this.unsafeResolveStringValue({
|
|
138
|
+
value: field.table,
|
|
139
|
+
alias,
|
|
140
|
+
parentAlias,
|
|
141
|
+
path,
|
|
142
|
+
}),
|
|
143
|
+
field: this.unsafeResolveValue({
|
|
144
|
+
field: field.field,
|
|
145
|
+
alias,
|
|
146
|
+
parentAlias,
|
|
147
|
+
path,
|
|
148
|
+
parentTableName,
|
|
149
|
+
}),
|
|
150
|
+
alias: this.unsafeResolveStringValue({
|
|
151
|
+
value: field.alias,
|
|
152
|
+
alias,
|
|
153
|
+
parentAlias,
|
|
154
|
+
path,
|
|
155
|
+
}),
|
|
156
|
+
cast: field.cast,
|
|
157
|
+
over: resolveOver(),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if ('conditions' in field) {
|
|
161
|
+
return {
|
|
162
|
+
conditions: field.conditions.map(c => ({
|
|
163
|
+
when: this.unsafeResolveWhere({
|
|
164
|
+
where: c.when,
|
|
165
|
+
alias,
|
|
166
|
+
parentAlias,
|
|
167
|
+
path,
|
|
168
|
+
parentTableName,
|
|
169
|
+
}),
|
|
170
|
+
then: this.unsafeResolveValue({
|
|
171
|
+
field: c.then,
|
|
172
|
+
alias,
|
|
173
|
+
parentAlias,
|
|
174
|
+
path,
|
|
175
|
+
parentTableName,
|
|
176
|
+
}),
|
|
177
|
+
})),
|
|
178
|
+
else: this.unsafeResolveValue({
|
|
179
|
+
field: field.else,
|
|
180
|
+
alias,
|
|
181
|
+
parentAlias,
|
|
182
|
+
path,
|
|
183
|
+
parentTableName,
|
|
184
|
+
}),
|
|
185
|
+
alias: field.alias,
|
|
186
|
+
cast: field.cast,
|
|
187
|
+
over: resolveOver(),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
if ('exists' in field) {
|
|
191
|
+
return {
|
|
192
|
+
exists: this.unsafeResolveSelect({
|
|
193
|
+
select: Object.assign(Object.assign({}, field.exists), { user: null }),
|
|
194
|
+
alias,
|
|
195
|
+
parentAlias,
|
|
196
|
+
path: `${path}.e`,
|
|
197
|
+
parentTableName,
|
|
198
|
+
}),
|
|
199
|
+
alias: field.alias,
|
|
200
|
+
cast: field.cast,
|
|
201
|
+
over: resolveOver(),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return Object.assign(Object.assign({}, this.unsafeResolveSelect({
|
|
205
|
+
select: Object.assign(Object.assign({}, field), { user: null }),
|
|
206
|
+
alias,
|
|
207
|
+
parentAlias,
|
|
208
|
+
path: `${path}.f`,
|
|
209
|
+
parentTableName,
|
|
210
|
+
})), { alias: field.alias, cast: field.cast, over: resolveOver() });
|
|
211
|
+
}
|
|
212
|
+
unsafeResolveWhere({ where, alias, parentAlias, path, parentTableName, }) {
|
|
213
|
+
if (!where) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const mapWhere = (w, orOrAnd) => {
|
|
217
|
+
const r = w[orOrAnd].map((where) => this.unsafeResolveWhere({
|
|
218
|
+
where,
|
|
219
|
+
alias,
|
|
220
|
+
path,
|
|
221
|
+
parentAlias,
|
|
222
|
+
parentTableName,
|
|
223
|
+
}));
|
|
224
|
+
return { [orOrAnd]: r };
|
|
225
|
+
};
|
|
226
|
+
if ('and' in where) {
|
|
227
|
+
return mapWhere(where, 'and');
|
|
228
|
+
}
|
|
229
|
+
else if ('or' in where) {
|
|
230
|
+
return mapWhere(where, 'or');
|
|
231
|
+
}
|
|
232
|
+
else if ('exists' in where) {
|
|
233
|
+
return {
|
|
234
|
+
exists: this.unsafeResolveSelect({
|
|
235
|
+
select: Object.assign(Object.assign({}, where.exists), { user: null }),
|
|
236
|
+
alias,
|
|
237
|
+
parentAlias,
|
|
238
|
+
path,
|
|
239
|
+
parentTableName,
|
|
240
|
+
}),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
else if ('not' in where) {
|
|
244
|
+
return {
|
|
245
|
+
not: this.unsafeResolveWhere({
|
|
246
|
+
where: where.not,
|
|
247
|
+
alias,
|
|
248
|
+
parentAlias,
|
|
249
|
+
path,
|
|
250
|
+
parentTableName,
|
|
251
|
+
}),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return Object.assign(Object.assign({}, where), { v1: this.unsafeResolveValue({
|
|
256
|
+
field: where.v1,
|
|
257
|
+
alias,
|
|
258
|
+
parentAlias,
|
|
259
|
+
path,
|
|
260
|
+
parentTableName,
|
|
261
|
+
}), v2: this.unsafeResolveValue({
|
|
262
|
+
field: where.v2,
|
|
263
|
+
alias,
|
|
264
|
+
parentAlias,
|
|
265
|
+
path,
|
|
266
|
+
parentTableName,
|
|
267
|
+
}) });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
unsafeResolveSelect({ select, alias, parentAlias, path, parentTableName, }) {
|
|
271
|
+
return Object.assign(Object.assign({}, select), { join: (select.join || [])
|
|
272
|
+
.map(join => {
|
|
273
|
+
return Object.assign(Object.assign({}, join), { on: this.unsafeResolveWhere({
|
|
274
|
+
where: join.on,
|
|
275
|
+
alias,
|
|
276
|
+
parentAlias,
|
|
277
|
+
path,
|
|
278
|
+
parentTableName,
|
|
279
|
+
}) });
|
|
280
|
+
})
|
|
281
|
+
.map(j => j), where: this.unsafeResolveWhere({
|
|
282
|
+
where: select.where,
|
|
283
|
+
alias,
|
|
284
|
+
parentAlias,
|
|
285
|
+
path,
|
|
286
|
+
parentTableName,
|
|
287
|
+
}) });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
12
290
|
class SqlSecurityLayerService {
|
|
13
|
-
constructor(domain, repositoryFactoryFunc) {
|
|
291
|
+
constructor(domain, repositoryFactoryFunc, fieldDictionaryCacheService) {
|
|
14
292
|
this.domain = domain;
|
|
15
293
|
this.repositoryFactoryFunc = repositoryFactoryFunc;
|
|
294
|
+
this.fieldDictionaryCacheService = fieldDictionaryCacheService;
|
|
295
|
+
this.prefix = 'security';
|
|
16
296
|
this.tableDictionary = new Map();
|
|
17
297
|
this.permissionSystemStatus = PermissionSystemStatus.Off;
|
|
298
|
+
this.MAX_DEEP = 100;
|
|
299
|
+
this.unsafeResolver = new UnsafeResolver();
|
|
18
300
|
this.getSimpleTableDictionary = ({ tableName }) => {
|
|
19
301
|
if (!tableName) {
|
|
20
302
|
throw new Error('Table name is required');
|
|
@@ -25,14 +307,14 @@ class SqlSecurityLayerService {
|
|
|
25
307
|
}
|
|
26
308
|
return {
|
|
27
309
|
name: td.name,
|
|
28
|
-
rules: td.rules
|
|
310
|
+
rules: td.rules,
|
|
311
|
+
module: td.module
|
|
29
312
|
};
|
|
30
313
|
};
|
|
31
314
|
this.logger = new logger_1.Logger(this.domain, 'SqlSecurityLayerService');
|
|
32
|
-
this.sqlLayerService = new sql_layer_service_1.SqlLayerService(this.domain, 'security', this.getSimpleTableDictionary);
|
|
33
315
|
}
|
|
34
316
|
async loadPermissionSystem() {
|
|
35
|
-
const fields = ['puca_pid', 'name', 'rules'];
|
|
317
|
+
const fields = ['puca_pid', 'name', 'rules', 'module'];
|
|
36
318
|
const repository = this.repositoryFactoryFunc().getInstance({
|
|
37
319
|
tableName: 'table_dictionary',
|
|
38
320
|
transaction: null
|
|
@@ -79,12 +361,14 @@ class SqlSecurityLayerService {
|
|
|
79
361
|
return select;
|
|
80
362
|
}
|
|
81
363
|
if (((_a = select.user) === null || _a === void 0 ? void 0 : _a.username) === 'puca-user') {
|
|
82
|
-
|
|
364
|
+
if (DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER) {
|
|
365
|
+
return select;
|
|
366
|
+
}
|
|
83
367
|
}
|
|
84
|
-
const result = this.
|
|
368
|
+
const result = this._select(select);
|
|
85
369
|
if (result.tables.find(t => { var _a; return (_a = t.rules) === null || _a === void 0 ? void 0 : _a.select; }) && !select.user) {
|
|
86
370
|
const msg = 'Permission denied to read on ' + result.tables.map(t => t.name).join(', ');
|
|
87
|
-
this.logger.warn(msg);
|
|
371
|
+
this.logger.warn('select', msg);
|
|
88
372
|
throw new Error(msg);
|
|
89
373
|
}
|
|
90
374
|
return result.select;
|
|
@@ -95,7 +379,9 @@ class SqlSecurityLayerService {
|
|
|
95
379
|
return insert;
|
|
96
380
|
}
|
|
97
381
|
if (((_a = insert.user) === null || _a === void 0 ? void 0 : _a.username) === 'puca-user') {
|
|
98
|
-
|
|
382
|
+
if (DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER) {
|
|
383
|
+
return insert;
|
|
384
|
+
}
|
|
99
385
|
}
|
|
100
386
|
const tableDictionary = this.getSimpleTableDictionary({
|
|
101
387
|
tableName: insert.tableName,
|
|
@@ -104,16 +390,16 @@ class SqlSecurityLayerService {
|
|
|
104
390
|
});
|
|
105
391
|
if (!tableDictionary) {
|
|
106
392
|
const msg = 'Table ' + insert.tableName + ' not found';
|
|
107
|
-
this.logger.warn(msg);
|
|
393
|
+
this.logger.warn('insert', msg);
|
|
108
394
|
throw new Error(msg);
|
|
109
395
|
}
|
|
110
396
|
const rules = (_b = tableDictionary.rules) === null || _b === void 0 ? void 0 : _b.insert;
|
|
111
397
|
if (rules && !insert.user) {
|
|
112
398
|
const msg = 'Permission denied to write on ' + insert.tableName;
|
|
113
|
-
this.logger.warn(msg);
|
|
399
|
+
this.logger.warn('insert', msg);
|
|
114
400
|
throw new Error(msg);
|
|
115
401
|
}
|
|
116
|
-
return this.
|
|
402
|
+
return this._insert(insert);
|
|
117
403
|
}
|
|
118
404
|
update(update) {
|
|
119
405
|
var _a, _b;
|
|
@@ -121,7 +407,9 @@ class SqlSecurityLayerService {
|
|
|
121
407
|
return update;
|
|
122
408
|
}
|
|
123
409
|
if (((_a = update.user) === null || _a === void 0 ? void 0 : _a.username) === 'puca-user') {
|
|
124
|
-
|
|
410
|
+
if (DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER) {
|
|
411
|
+
return update;
|
|
412
|
+
}
|
|
125
413
|
}
|
|
126
414
|
const tableDictionary = this.getSimpleTableDictionary({
|
|
127
415
|
tableName: update.tableName,
|
|
@@ -130,16 +418,16 @@ class SqlSecurityLayerService {
|
|
|
130
418
|
});
|
|
131
419
|
if (!tableDictionary) {
|
|
132
420
|
const msg = 'Table ' + update.tableName + ' not found';
|
|
133
|
-
this.logger.warn(msg);
|
|
421
|
+
this.logger.warn('update', msg);
|
|
134
422
|
throw new Error(msg);
|
|
135
423
|
}
|
|
136
424
|
const rules = (_b = tableDictionary.rules) === null || _b === void 0 ? void 0 : _b.update;
|
|
137
425
|
if (rules && !update.user) {
|
|
138
426
|
const msg = 'Permission denied to write on ' + update.tableName;
|
|
139
|
-
this.logger.warn(msg);
|
|
427
|
+
this.logger.warn('update', msg);
|
|
140
428
|
throw new Error(msg);
|
|
141
429
|
}
|
|
142
|
-
return this.
|
|
430
|
+
return this._update(update);
|
|
143
431
|
}
|
|
144
432
|
delete(d) {
|
|
145
433
|
var _a, _b;
|
|
@@ -147,7 +435,9 @@ class SqlSecurityLayerService {
|
|
|
147
435
|
return d;
|
|
148
436
|
}
|
|
149
437
|
if (((_a = d.user) === null || _a === void 0 ? void 0 : _a.username) === 'puca-user') {
|
|
150
|
-
|
|
438
|
+
if (DISABLE_PERMISSION_SYSTEM_FOR_PUCA_USER) {
|
|
439
|
+
return d;
|
|
440
|
+
}
|
|
151
441
|
}
|
|
152
442
|
const tableDictionary = this.getSimpleTableDictionary({
|
|
153
443
|
tableName: d.tableName,
|
|
@@ -156,16 +446,1155 @@ class SqlSecurityLayerService {
|
|
|
156
446
|
});
|
|
157
447
|
if (!tableDictionary) {
|
|
158
448
|
const msg = 'Table ' + d.tableName + ' not found';
|
|
159
|
-
this.logger.warn(msg);
|
|
449
|
+
this.logger.warn('delete', msg);
|
|
160
450
|
throw new Error(msg);
|
|
161
451
|
}
|
|
162
452
|
const rules = (_b = tableDictionary.rules) === null || _b === void 0 ? void 0 : _b.delete;
|
|
163
453
|
if (rules && !d.user) {
|
|
164
454
|
const msg = 'Permission denied to write on ' + d.tableName;
|
|
165
|
-
this.logger.warn(msg);
|
|
455
|
+
this.logger.warn('delete', msg);
|
|
166
456
|
throw new Error(msg);
|
|
167
457
|
}
|
|
168
|
-
return this.
|
|
458
|
+
return this._delete(d);
|
|
459
|
+
}
|
|
460
|
+
_addRuleWhere({ response, rule }) {
|
|
461
|
+
if (rule.where) {
|
|
462
|
+
response.where.and.push(rule.where);
|
|
463
|
+
}
|
|
464
|
+
Object.entries(rule.args || {}).forEach(([k, v]) => {
|
|
465
|
+
response.args[k] = v;
|
|
466
|
+
});
|
|
467
|
+
response.tables.push(...(rule.tables || []));
|
|
468
|
+
}
|
|
469
|
+
_insert(insert) {
|
|
470
|
+
try {
|
|
471
|
+
if (insert.selectFilter) {
|
|
472
|
+
throw new Error('Securty layer exclusivel attribute');
|
|
473
|
+
}
|
|
474
|
+
const response = {
|
|
475
|
+
args: Object.assign({}, insert.values),
|
|
476
|
+
tables: [],
|
|
477
|
+
where: {
|
|
478
|
+
and: [],
|
|
479
|
+
},
|
|
480
|
+
};
|
|
481
|
+
const td = this.getSimpleTableDictionary({
|
|
482
|
+
tableName: insert.tableName,
|
|
483
|
+
statementType: 'update',
|
|
484
|
+
statement: insert,
|
|
485
|
+
});
|
|
486
|
+
const mode = 'update';
|
|
487
|
+
const path = `${this.prefix}.update`;
|
|
488
|
+
this._addRuleWhere({
|
|
489
|
+
response,
|
|
490
|
+
rule: this._tableRules({
|
|
491
|
+
td,
|
|
492
|
+
alias: insert.tableName,
|
|
493
|
+
mode,
|
|
494
|
+
path,
|
|
495
|
+
parentAlias: null,
|
|
496
|
+
parentTableName: null,
|
|
497
|
+
tableName: insert.tableName,
|
|
498
|
+
})
|
|
499
|
+
});
|
|
500
|
+
const ensureSystemFields = (fieldDictionary) => {
|
|
501
|
+
const systemFields = [
|
|
502
|
+
{ name: 'puca_pid', type: interfaces_1.TableFieldTypeEnum.UUID },
|
|
503
|
+
{ name: 'puca_created_at', type: interfaces_1.TableFieldTypeEnum.TIMESTAMP },
|
|
504
|
+
{ name: 'puca_last_update', type: interfaces_1.TableFieldTypeEnum.TIMESTAMP },
|
|
505
|
+
{ name: 'puca_key', type: interfaces_1.TableFieldTypeEnum.BIGSERIAL },
|
|
506
|
+
{ name: 'user_pid', type: interfaces_1.TableFieldTypeEnum.UUID },
|
|
507
|
+
];
|
|
508
|
+
return _.unionBy(fieldDictionary, systemFields, 'name');
|
|
509
|
+
};
|
|
510
|
+
const fieldDictionary = this.fieldDictionaryCacheService.getFieldDictionaryByTable(insert.tableName);
|
|
511
|
+
const mockTable = {
|
|
512
|
+
fields: ensureSystemFields(fieldDictionary).map(fd => {
|
|
513
|
+
let cast = fd.type;
|
|
514
|
+
if ((0, is_primitive_type_1.isPrimitiveType)(fd.type)) {
|
|
515
|
+
if (fd.type === interfaces_1.TableFieldTypeEnum.BIGSERIAL) {
|
|
516
|
+
cast = interfaces_1.TableFieldTypeEnum.NUMERIC;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
cast = interfaces_1.TableFieldTypeEnum.UUID;
|
|
521
|
+
}
|
|
522
|
+
return {
|
|
523
|
+
field: `$${fd.name}`,
|
|
524
|
+
alias: fd.name,
|
|
525
|
+
cast,
|
|
526
|
+
};
|
|
527
|
+
}),
|
|
528
|
+
from: null
|
|
529
|
+
};
|
|
530
|
+
const f = this._select({
|
|
531
|
+
from: insert.tableName,
|
|
532
|
+
user: insert.user,
|
|
533
|
+
});
|
|
534
|
+
if (!f.select.with) {
|
|
535
|
+
f.select.with = [];
|
|
536
|
+
}
|
|
537
|
+
f.select.with.unshift({
|
|
538
|
+
alias: insert.tableName,
|
|
539
|
+
select: mockTable
|
|
540
|
+
});
|
|
541
|
+
Object.entries(insert.values).forEach(([k, v]) => {
|
|
542
|
+
f.select.args[k] = v;
|
|
543
|
+
});
|
|
544
|
+
fieldDictionary.forEach(fd => {
|
|
545
|
+
const wasSet = insert.values[fd.name] !== undefined;
|
|
546
|
+
if (wasSet) {
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
f.select.args[fd.name] = null;
|
|
550
|
+
});
|
|
551
|
+
const result = {
|
|
552
|
+
selectFilter: f.select,
|
|
553
|
+
tableName: insert.tableName,
|
|
554
|
+
values: insert.values,
|
|
555
|
+
returning: insert.returning,
|
|
556
|
+
user: insert.user,
|
|
557
|
+
};
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
catch (e) {
|
|
561
|
+
this.logger.verbose('insert', e.message, insert);
|
|
562
|
+
throw e;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
_update(update) {
|
|
566
|
+
try {
|
|
567
|
+
if (update.where) {
|
|
568
|
+
throw new Error('Securty layer for where on update not implemented yet');
|
|
569
|
+
}
|
|
570
|
+
const response = {
|
|
571
|
+
args: Object.assign({}, update.args),
|
|
572
|
+
tables: [],
|
|
573
|
+
where: {
|
|
574
|
+
and: [],
|
|
575
|
+
},
|
|
576
|
+
};
|
|
577
|
+
const td = this.getSimpleTableDictionary({
|
|
578
|
+
tableName: update.tableName,
|
|
579
|
+
statementType: 'update',
|
|
580
|
+
statement: update,
|
|
581
|
+
});
|
|
582
|
+
const mode = 'update';
|
|
583
|
+
const path = `${this.prefix}.update`;
|
|
584
|
+
this._addRuleWhere({
|
|
585
|
+
response,
|
|
586
|
+
rule: this._safeResolveWhere({
|
|
587
|
+
where: update.where,
|
|
588
|
+
td,
|
|
589
|
+
tableAlias: update.tableName,
|
|
590
|
+
mode,
|
|
591
|
+
path,
|
|
592
|
+
parentAlias: null,
|
|
593
|
+
parentTableName: null,
|
|
594
|
+
tableName: update.tableName,
|
|
595
|
+
}),
|
|
596
|
+
});
|
|
597
|
+
this._addRuleWhere({
|
|
598
|
+
response,
|
|
599
|
+
rule: this._tableRules({
|
|
600
|
+
td,
|
|
601
|
+
alias: update.tableName,
|
|
602
|
+
mode,
|
|
603
|
+
path,
|
|
604
|
+
parentAlias: null,
|
|
605
|
+
parentTableName: null,
|
|
606
|
+
tableName: update.tableName,
|
|
607
|
+
})
|
|
608
|
+
});
|
|
609
|
+
const ref = this._select({
|
|
610
|
+
from: {
|
|
611
|
+
table: update.tableName,
|
|
612
|
+
alias: 'old',
|
|
613
|
+
},
|
|
614
|
+
user: update.user,
|
|
615
|
+
args: {
|
|
616
|
+
puca_pid: update.puca_pid
|
|
617
|
+
},
|
|
618
|
+
where: {
|
|
619
|
+
and: [{
|
|
620
|
+
v1: 'old.puca_pid',
|
|
621
|
+
operator: interfaces_1.ConditionalOperator.equals,
|
|
622
|
+
v2: '$puca_pid',
|
|
623
|
+
}]
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
return {
|
|
627
|
+
puca_pid: update.puca_pid,
|
|
628
|
+
set: update.set,
|
|
629
|
+
tableName: update.tableName,
|
|
630
|
+
args: Object.assign(Object.assign({ puca_pid: update.puca_pid }, ref.select.args), response.args),
|
|
631
|
+
returning: update.returning,
|
|
632
|
+
where: {
|
|
633
|
+
and: [
|
|
634
|
+
response.where,
|
|
635
|
+
{ exists: ref.select }
|
|
636
|
+
]
|
|
637
|
+
},
|
|
638
|
+
user: update.user,
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
catch (e) {
|
|
642
|
+
this.logger.verbose('update', e.message, update);
|
|
643
|
+
throw e;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
_delete(d) {
|
|
647
|
+
try {
|
|
648
|
+
if (d.where) {
|
|
649
|
+
throw new Error('Securty layer for where on delete not implemented yet');
|
|
650
|
+
}
|
|
651
|
+
const response = {
|
|
652
|
+
args: Object.assign({}, d.args),
|
|
653
|
+
tables: [],
|
|
654
|
+
where: {
|
|
655
|
+
and: [],
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
const td = this.getSimpleTableDictionary({
|
|
659
|
+
tableName: d.tableName,
|
|
660
|
+
statementType: 'delete',
|
|
661
|
+
statement: d,
|
|
662
|
+
});
|
|
663
|
+
const mode = 'delete';
|
|
664
|
+
const path = `${this.prefix}.delete`;
|
|
665
|
+
this._addRuleWhere({
|
|
666
|
+
response,
|
|
667
|
+
rule: this._safeResolveWhere({
|
|
668
|
+
where: d.where,
|
|
669
|
+
td,
|
|
670
|
+
tableAlias: d.tableName,
|
|
671
|
+
mode,
|
|
672
|
+
path,
|
|
673
|
+
parentAlias: null,
|
|
674
|
+
parentTableName: null,
|
|
675
|
+
tableName: d.tableName,
|
|
676
|
+
}),
|
|
677
|
+
});
|
|
678
|
+
this._addRuleWhere({
|
|
679
|
+
response,
|
|
680
|
+
rule: this._tableRules({
|
|
681
|
+
td,
|
|
682
|
+
alias: d.tableName,
|
|
683
|
+
mode,
|
|
684
|
+
path,
|
|
685
|
+
parentAlias: null,
|
|
686
|
+
parentTableName: null,
|
|
687
|
+
tableName: d.tableName,
|
|
688
|
+
}),
|
|
689
|
+
});
|
|
690
|
+
const ref = this._select({
|
|
691
|
+
from: {
|
|
692
|
+
table: d.tableName,
|
|
693
|
+
alias: 'old',
|
|
694
|
+
},
|
|
695
|
+
user: d.user,
|
|
696
|
+
args: {
|
|
697
|
+
puca_pid: d.puca_pid
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
return {
|
|
701
|
+
puca_pid: d.puca_pid,
|
|
702
|
+
tableName: d.tableName,
|
|
703
|
+
returning: d.returning,
|
|
704
|
+
where: {
|
|
705
|
+
and: [
|
|
706
|
+
response.where,
|
|
707
|
+
{ exists: ref.select }
|
|
708
|
+
]
|
|
709
|
+
},
|
|
710
|
+
args: Object.assign(Object.assign(Object.assign({}, ref.select.args), response.args), { puca_pid: d.puca_pid }),
|
|
711
|
+
user: d.user,
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
catch (e) {
|
|
715
|
+
this.logger.verbose('delete', e.message, d);
|
|
716
|
+
throw e;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
_select(select) {
|
|
720
|
+
try {
|
|
721
|
+
let rules;
|
|
722
|
+
const getRules = () => {
|
|
723
|
+
if (rules) {
|
|
724
|
+
return rules;
|
|
725
|
+
}
|
|
726
|
+
rules = this._safeResolveSelect({
|
|
727
|
+
select,
|
|
728
|
+
mode: 'select',
|
|
729
|
+
path: `${this.prefix}.select`,
|
|
730
|
+
parentAlias: null,
|
|
731
|
+
parentTableName: null,
|
|
732
|
+
resolveWith: true
|
|
733
|
+
});
|
|
734
|
+
return rules;
|
|
735
|
+
};
|
|
736
|
+
if (!select.user) {
|
|
737
|
+
const tables = getRules().tables.filter(td => _.has(td, ['rules', 'select']));
|
|
738
|
+
this.logger.verbose('select', 'select with rule, but no user');
|
|
739
|
+
return { select: select, tables };
|
|
740
|
+
}
|
|
741
|
+
this.logger.verbose('select', () => (_.omit(getRules().select, ['user'])));
|
|
742
|
+
return { select: getRules().select, tables: [] };
|
|
743
|
+
}
|
|
744
|
+
catch (e) {
|
|
745
|
+
this.logger.verbose('select', e.message, select);
|
|
746
|
+
throw e;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
_safeResolveWhere({ where, td, tableAlias, mode, path, parentAlias, parentTableName, tableName, }) {
|
|
750
|
+
this._checkCircularReference({ path });
|
|
751
|
+
const defaultResponse = {
|
|
752
|
+
args: {},
|
|
753
|
+
tables: [],
|
|
754
|
+
where,
|
|
755
|
+
};
|
|
756
|
+
if (!where) {
|
|
757
|
+
return defaultResponse;
|
|
758
|
+
}
|
|
759
|
+
if ('exists' in where) {
|
|
760
|
+
const rules = this._safeResolveSelect({
|
|
761
|
+
select: Object.assign(Object.assign({}, where.exists), { user: null }),
|
|
762
|
+
mode: 'select',
|
|
763
|
+
path: `${path}.e`,
|
|
764
|
+
parentAlias: tableAlias,
|
|
765
|
+
parentTableName: tableName
|
|
766
|
+
});
|
|
767
|
+
return {
|
|
768
|
+
where: { exists: rules.select },
|
|
769
|
+
tables: rules.tables,
|
|
770
|
+
args: rules.args,
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
else if ('not' in where) {
|
|
774
|
+
const rules = this._safeResolveWhere({
|
|
775
|
+
where: where.not,
|
|
776
|
+
td,
|
|
777
|
+
tableAlias,
|
|
778
|
+
mode,
|
|
779
|
+
path: `${path}.n`,
|
|
780
|
+
parentAlias,
|
|
781
|
+
parentTableName,
|
|
782
|
+
tableName,
|
|
783
|
+
});
|
|
784
|
+
return {
|
|
785
|
+
where: {
|
|
786
|
+
not: rules.where,
|
|
787
|
+
},
|
|
788
|
+
tables: rules.tables,
|
|
789
|
+
args: rules.args,
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
else if ('or' in where) {
|
|
793
|
+
const or = where.or
|
|
794
|
+
.map((w, i) => {
|
|
795
|
+
return this._safeResolveWhere({
|
|
796
|
+
where: w,
|
|
797
|
+
td,
|
|
798
|
+
tableAlias,
|
|
799
|
+
mode,
|
|
800
|
+
path: `${path}.${i}`,
|
|
801
|
+
parentAlias,
|
|
802
|
+
parentTableName,
|
|
803
|
+
tableName,
|
|
804
|
+
});
|
|
805
|
+
})
|
|
806
|
+
.filter(w => w.where);
|
|
807
|
+
if (!or.length) {
|
|
808
|
+
return defaultResponse;
|
|
809
|
+
}
|
|
810
|
+
return or.reduce((r, rule) => {
|
|
811
|
+
return {
|
|
812
|
+
where: {
|
|
813
|
+
or: [r.where, rule.where],
|
|
814
|
+
},
|
|
815
|
+
tables: [...r.tables, ...rule.tables],
|
|
816
|
+
args: Object.assign(Object.assign({}, r.args), rule.args),
|
|
817
|
+
};
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
else if ('and' in where) {
|
|
821
|
+
const and = where.and
|
|
822
|
+
.map((w, i) => {
|
|
823
|
+
return this._safeResolveWhere({
|
|
824
|
+
where: w,
|
|
825
|
+
td,
|
|
826
|
+
tableAlias,
|
|
827
|
+
mode,
|
|
828
|
+
path: `${path}.${i}`,
|
|
829
|
+
parentAlias,
|
|
830
|
+
parentTableName,
|
|
831
|
+
tableName,
|
|
832
|
+
});
|
|
833
|
+
})
|
|
834
|
+
.filter(w => w.where);
|
|
835
|
+
if (!and.length) {
|
|
836
|
+
return defaultResponse;
|
|
837
|
+
}
|
|
838
|
+
return and.reduce((r, rule) => {
|
|
839
|
+
return {
|
|
840
|
+
where: {
|
|
841
|
+
and: [r.where, rule.where],
|
|
842
|
+
},
|
|
843
|
+
tables: [...r.tables, ...rule.tables],
|
|
844
|
+
args: Object.assign(Object.assign({}, r.args), rule.args),
|
|
845
|
+
};
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
else {
|
|
849
|
+
const resolvedV1 = this._safeResolveValue({
|
|
850
|
+
field: where.v1,
|
|
851
|
+
td,
|
|
852
|
+
tableAlias,
|
|
853
|
+
mode,
|
|
854
|
+
path,
|
|
855
|
+
parentAlias,
|
|
856
|
+
parentTableName,
|
|
857
|
+
tableName,
|
|
858
|
+
});
|
|
859
|
+
const resolvedV2 = this._safeResolveValue({
|
|
860
|
+
field: where.v2,
|
|
861
|
+
td,
|
|
862
|
+
tableAlias,
|
|
863
|
+
mode,
|
|
864
|
+
path,
|
|
865
|
+
parentAlias,
|
|
866
|
+
parentTableName,
|
|
867
|
+
tableName,
|
|
868
|
+
});
|
|
869
|
+
return {
|
|
870
|
+
args: Object.assign(Object.assign({}, resolvedV1.args), resolvedV2.args),
|
|
871
|
+
tables: [...resolvedV1.tables, ...resolvedV2.tables],
|
|
872
|
+
where: {
|
|
873
|
+
v1: resolvedV1.field,
|
|
874
|
+
v2: resolvedV2.field,
|
|
875
|
+
operator: where.operator,
|
|
876
|
+
},
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
_checkCircularReference({ path }) {
|
|
881
|
+
if (path.split('.').length > this.MAX_DEEP) {
|
|
882
|
+
throw new Error('circular reference');
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
_tableRules({ td, alias: tableAlias, tableName, path, mode, parentAlias, parentTableName, }) {
|
|
886
|
+
if (!td) {
|
|
887
|
+
return { where: null, args: {}, tables: [] };
|
|
888
|
+
}
|
|
889
|
+
const where = this.unsafeResolver.unsafeResolveWhere({
|
|
890
|
+
where: _.get(td.rules, [mode, 'where']),
|
|
891
|
+
alias: tableAlias,
|
|
892
|
+
path,
|
|
893
|
+
parentAlias,
|
|
894
|
+
parentTableName,
|
|
895
|
+
});
|
|
896
|
+
if (!where) {
|
|
897
|
+
return { where: null, args: {}, tables: [] };
|
|
898
|
+
}
|
|
899
|
+
const rules = this._safeResolveWhere({
|
|
900
|
+
where,
|
|
901
|
+
mode,
|
|
902
|
+
tableAlias,
|
|
903
|
+
td,
|
|
904
|
+
path: `${path}.tr`,
|
|
905
|
+
parentAlias,
|
|
906
|
+
parentTableName,
|
|
907
|
+
tableName,
|
|
908
|
+
});
|
|
909
|
+
const oArgs = _.get(td.rules, [mode, 'args']) || {};
|
|
910
|
+
Object.keys(oArgs).forEach(k => {
|
|
911
|
+
const oValue = oArgs[k];
|
|
912
|
+
if (typeof oValue !== 'string') {
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
const nValue = oValue
|
|
916
|
+
.replace('{{parentTableName}}', parentTableName);
|
|
917
|
+
oArgs[k] = nValue;
|
|
918
|
+
});
|
|
919
|
+
const args = Object.entries(oArgs).reduce((args, [k, v]) => {
|
|
920
|
+
args[k] = v;
|
|
921
|
+
return args;
|
|
922
|
+
}, rules.args);
|
|
923
|
+
return {
|
|
924
|
+
args,
|
|
925
|
+
tables: [td, ...rules.tables],
|
|
926
|
+
where: rules.where,
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
_safeResolveValue({ field, td, tableAlias, mode, path, parentAlias, parentTableName, tableName, }) {
|
|
930
|
+
this._checkCircularReference({ path });
|
|
931
|
+
if (!field) {
|
|
932
|
+
return {
|
|
933
|
+
field,
|
|
934
|
+
args: {},
|
|
935
|
+
tables: [],
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
if (typeof field === 'string') {
|
|
939
|
+
const result = this.unsafeResolver.unsafeResolveValue({
|
|
940
|
+
field: field,
|
|
941
|
+
alias: tableAlias,
|
|
942
|
+
parentAlias,
|
|
943
|
+
path,
|
|
944
|
+
parentTableName,
|
|
945
|
+
});
|
|
946
|
+
return {
|
|
947
|
+
field: result,
|
|
948
|
+
args: {},
|
|
949
|
+
tables: []
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
if (typeof field === 'number' || typeof field === 'boolean') {
|
|
953
|
+
throw new Error(`invalid typeof ${field} === ${typeof field === 'number'}`);
|
|
954
|
+
}
|
|
955
|
+
const resolveOver = (tables, args) => {
|
|
956
|
+
if (!field.over) {
|
|
957
|
+
return null;
|
|
958
|
+
}
|
|
959
|
+
const partitionBy = (field.over.partitionBy || []).map(p => {
|
|
960
|
+
const result = this._safeResolveValue({
|
|
961
|
+
field: p,
|
|
962
|
+
td,
|
|
963
|
+
tableAlias,
|
|
964
|
+
mode,
|
|
965
|
+
path,
|
|
966
|
+
parentAlias,
|
|
967
|
+
parentTableName,
|
|
968
|
+
tableName,
|
|
969
|
+
});
|
|
970
|
+
tables.push(...result.tables);
|
|
971
|
+
Object.entries(result.args).forEach(([k, v]) => {
|
|
972
|
+
args[k] = v;
|
|
973
|
+
});
|
|
974
|
+
return result.field;
|
|
975
|
+
});
|
|
976
|
+
const orderBy = (field.over.orderBy || []).map(oo => {
|
|
977
|
+
const o = this._ensureFullFieldName(oo, tableAlias);
|
|
978
|
+
const result = this._safeResolveValue({
|
|
979
|
+
field: o,
|
|
980
|
+
td,
|
|
981
|
+
tableAlias,
|
|
982
|
+
mode,
|
|
983
|
+
path,
|
|
984
|
+
parentAlias,
|
|
985
|
+
parentTableName,
|
|
986
|
+
tableName,
|
|
987
|
+
});
|
|
988
|
+
tables.push(...result.tables);
|
|
989
|
+
Object.entries(result.args).forEach(([k, v]) => {
|
|
990
|
+
args[k] = v;
|
|
991
|
+
});
|
|
992
|
+
const field = result.field;
|
|
993
|
+
if (typeof field === 'string') {
|
|
994
|
+
return field;
|
|
995
|
+
}
|
|
996
|
+
if (typeof oo === 'string') {
|
|
997
|
+
return field;
|
|
998
|
+
}
|
|
999
|
+
return Object.assign(Object.assign({}, field), { sort: oo.sort });
|
|
1000
|
+
});
|
|
1001
|
+
return {
|
|
1002
|
+
partitionBy,
|
|
1003
|
+
orderBy,
|
|
1004
|
+
};
|
|
1005
|
+
};
|
|
1006
|
+
if ('function' in field) {
|
|
1007
|
+
const tables = [];
|
|
1008
|
+
const args = {};
|
|
1009
|
+
return {
|
|
1010
|
+
field: {
|
|
1011
|
+
function: field.function,
|
|
1012
|
+
args: (field.args || []).map(arg => {
|
|
1013
|
+
if (isWhere(arg)) {
|
|
1014
|
+
const resolved = this._safeResolveWhere({
|
|
1015
|
+
where: arg,
|
|
1016
|
+
td,
|
|
1017
|
+
tableAlias,
|
|
1018
|
+
mode,
|
|
1019
|
+
path,
|
|
1020
|
+
parentAlias,
|
|
1021
|
+
parentTableName,
|
|
1022
|
+
tableName,
|
|
1023
|
+
});
|
|
1024
|
+
tables.push(...resolved.tables);
|
|
1025
|
+
Object.entries(resolved.args).forEach(([k, v]) => {
|
|
1026
|
+
args[k] = v;
|
|
1027
|
+
});
|
|
1028
|
+
return resolved.where;
|
|
1029
|
+
}
|
|
1030
|
+
const resolved = this._safeResolveValue({
|
|
1031
|
+
field: arg,
|
|
1032
|
+
td,
|
|
1033
|
+
tableAlias,
|
|
1034
|
+
mode,
|
|
1035
|
+
path,
|
|
1036
|
+
parentAlias,
|
|
1037
|
+
parentTableName,
|
|
1038
|
+
tableName
|
|
1039
|
+
});
|
|
1040
|
+
tables.push(...resolved.tables);
|
|
1041
|
+
Object.entries(resolved.args).forEach(([k, v]) => {
|
|
1042
|
+
args[k] = v;
|
|
1043
|
+
});
|
|
1044
|
+
return resolved.field;
|
|
1045
|
+
}),
|
|
1046
|
+
cast: field.cast,
|
|
1047
|
+
alias: field.alias,
|
|
1048
|
+
over: resolveOver(tables, args),
|
|
1049
|
+
},
|
|
1050
|
+
args,
|
|
1051
|
+
tables,
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
if ('field' in field) {
|
|
1055
|
+
if (typeof field.field !== 'string') {
|
|
1056
|
+
const resolved = this._safeResolveValue({
|
|
1057
|
+
field: field.field,
|
|
1058
|
+
td,
|
|
1059
|
+
tableAlias,
|
|
1060
|
+
mode,
|
|
1061
|
+
path,
|
|
1062
|
+
parentAlias,
|
|
1063
|
+
parentTableName,
|
|
1064
|
+
tableName,
|
|
1065
|
+
});
|
|
1066
|
+
const over = resolveOver(resolved.tables, resolved.args);
|
|
1067
|
+
return {
|
|
1068
|
+
field: {
|
|
1069
|
+
alias: field.alias,
|
|
1070
|
+
cast: field.cast,
|
|
1071
|
+
field: resolved.field,
|
|
1072
|
+
over,
|
|
1073
|
+
},
|
|
1074
|
+
args: resolved.args,
|
|
1075
|
+
tables: resolved.tables,
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
if (field.table && typeof field.table !== 'string') {
|
|
1079
|
+
throw new Error(`invalid typeof ${field.table} === ${typeof field.table}`);
|
|
1080
|
+
}
|
|
1081
|
+
if (field.alias && typeof field.alias !== 'string') {
|
|
1082
|
+
throw new Error(`invalid typeof ${field.alias} === ${typeof field.alias}`);
|
|
1083
|
+
}
|
|
1084
|
+
const args = {};
|
|
1085
|
+
const tables = [];
|
|
1086
|
+
const over = resolveOver(tables, args);
|
|
1087
|
+
return {
|
|
1088
|
+
field: {
|
|
1089
|
+
table: this.unsafeResolver.unsafeResolveStringValue({
|
|
1090
|
+
value: field.table,
|
|
1091
|
+
alias: tableAlias,
|
|
1092
|
+
parentAlias,
|
|
1093
|
+
path,
|
|
1094
|
+
}),
|
|
1095
|
+
field: this.unsafeResolver.unsafeResolveValue({
|
|
1096
|
+
field: field.field,
|
|
1097
|
+
alias: tableAlias,
|
|
1098
|
+
parentAlias,
|
|
1099
|
+
path,
|
|
1100
|
+
parentTableName,
|
|
1101
|
+
}),
|
|
1102
|
+
alias: this.unsafeResolver.unsafeResolveStringValue({
|
|
1103
|
+
value: field.alias,
|
|
1104
|
+
alias: tableAlias,
|
|
1105
|
+
parentAlias,
|
|
1106
|
+
path,
|
|
1107
|
+
}),
|
|
1108
|
+
cast: field.cast,
|
|
1109
|
+
over,
|
|
1110
|
+
},
|
|
1111
|
+
args,
|
|
1112
|
+
tables,
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
if ('conditions' in field) {
|
|
1116
|
+
const tables = [];
|
|
1117
|
+
const args = {};
|
|
1118
|
+
const e = this._safeResolveValue({
|
|
1119
|
+
field: field.else,
|
|
1120
|
+
td,
|
|
1121
|
+
tableAlias,
|
|
1122
|
+
mode,
|
|
1123
|
+
path,
|
|
1124
|
+
parentAlias,
|
|
1125
|
+
parentTableName,
|
|
1126
|
+
tableName,
|
|
1127
|
+
});
|
|
1128
|
+
tables.push(...e.tables);
|
|
1129
|
+
Object.entries(e.args).forEach(([k, v]) => {
|
|
1130
|
+
args[k] = v;
|
|
1131
|
+
});
|
|
1132
|
+
return {
|
|
1133
|
+
field: {
|
|
1134
|
+
conditions: field.conditions.map(c => {
|
|
1135
|
+
const when = this._safeResolveWhere({
|
|
1136
|
+
where: c.when,
|
|
1137
|
+
td,
|
|
1138
|
+
tableAlias,
|
|
1139
|
+
mode,
|
|
1140
|
+
path,
|
|
1141
|
+
parentAlias,
|
|
1142
|
+
parentTableName,
|
|
1143
|
+
tableName,
|
|
1144
|
+
});
|
|
1145
|
+
const then = this._safeResolveValue({
|
|
1146
|
+
field: c.then,
|
|
1147
|
+
td,
|
|
1148
|
+
tableAlias,
|
|
1149
|
+
mode,
|
|
1150
|
+
path,
|
|
1151
|
+
parentAlias,
|
|
1152
|
+
parentTableName,
|
|
1153
|
+
tableName,
|
|
1154
|
+
});
|
|
1155
|
+
tables.push(...when.tables, ...then.tables);
|
|
1156
|
+
Object.entries(when.args).forEach(([k, v]) => {
|
|
1157
|
+
args[k] = v;
|
|
1158
|
+
});
|
|
1159
|
+
Object.entries(then.args).forEach(([k, v]) => {
|
|
1160
|
+
args[k] = v;
|
|
1161
|
+
});
|
|
1162
|
+
return {
|
|
1163
|
+
when: when.where,
|
|
1164
|
+
then: then.field,
|
|
1165
|
+
};
|
|
1166
|
+
}),
|
|
1167
|
+
else: e.field,
|
|
1168
|
+
alias: field.alias,
|
|
1169
|
+
cast: field.cast,
|
|
1170
|
+
over: resolveOver(tables, args),
|
|
1171
|
+
},
|
|
1172
|
+
args,
|
|
1173
|
+
tables,
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
if ('exists' in field) {
|
|
1177
|
+
const tables = [];
|
|
1178
|
+
const args = {};
|
|
1179
|
+
const resolved = this._safeResolveSelect({
|
|
1180
|
+
select: Object.assign(Object.assign({}, field.exists), { user: null }),
|
|
1181
|
+
mode: 'select',
|
|
1182
|
+
path: `${path}.e`,
|
|
1183
|
+
parentAlias: tableAlias,
|
|
1184
|
+
parentTableName: tableName
|
|
1185
|
+
});
|
|
1186
|
+
tables.push(...resolved.tables);
|
|
1187
|
+
Object.entries(resolved.args).forEach(([k, v]) => {
|
|
1188
|
+
args[k] = v;
|
|
1189
|
+
});
|
|
1190
|
+
return {
|
|
1191
|
+
field: {
|
|
1192
|
+
cast: field.cast,
|
|
1193
|
+
exists: resolved.select,
|
|
1194
|
+
alias: field.alias,
|
|
1195
|
+
over: resolveOver(tables, args),
|
|
1196
|
+
},
|
|
1197
|
+
args,
|
|
1198
|
+
tables,
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
const resolved = this._safeResolveSelect({
|
|
1202
|
+
select: Object.assign(Object.assign({}, field), { user: null }),
|
|
1203
|
+
mode: 'select',
|
|
1204
|
+
path: `${path}.f`,
|
|
1205
|
+
parentAlias: tableAlias,
|
|
1206
|
+
parentTableName: tableName
|
|
1207
|
+
});
|
|
1208
|
+
return {
|
|
1209
|
+
field: Object.assign(Object.assign({}, resolved.select), { cast: field.cast, alias: field.alias, over: resolveOver(resolved.tables, resolved.args) }),
|
|
1210
|
+
args: resolved.args,
|
|
1211
|
+
tables: resolved.tables,
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
_safeResolveSelect({ select, mode, path, parentAlias, parentTableName, resolveWith, }) {
|
|
1215
|
+
var _a, _b;
|
|
1216
|
+
this._checkCircularReference({ path });
|
|
1217
|
+
const currentTableName = typeof select.from === 'string' ? select.from : (typeof select.from.table === 'string' ? select.from.table : select.from.alias);
|
|
1218
|
+
const currentTableAlias = (typeof select.from === 'string' ? select.from : select.from.alias) ||
|
|
1219
|
+
currentTableName;
|
|
1220
|
+
const td = this.getSimpleTableDictionary({
|
|
1221
|
+
tableName: currentTableName,
|
|
1222
|
+
statementType: 'select',
|
|
1223
|
+
statement: select,
|
|
1224
|
+
});
|
|
1225
|
+
const response = {
|
|
1226
|
+
args: Object.assign({}, select.args),
|
|
1227
|
+
tables: [],
|
|
1228
|
+
where: {
|
|
1229
|
+
and: [],
|
|
1230
|
+
},
|
|
1231
|
+
};
|
|
1232
|
+
const resolvedWhere = this._safeResolveWhere({
|
|
1233
|
+
where: select.where,
|
|
1234
|
+
td,
|
|
1235
|
+
tableAlias: currentTableAlias,
|
|
1236
|
+
mode,
|
|
1237
|
+
path,
|
|
1238
|
+
parentAlias,
|
|
1239
|
+
parentTableName,
|
|
1240
|
+
tableName: currentTableName,
|
|
1241
|
+
});
|
|
1242
|
+
this._addRuleWhere({ response, rule: resolvedWhere });
|
|
1243
|
+
const tableRules = this._tableRules({
|
|
1244
|
+
td,
|
|
1245
|
+
alias: currentTableAlias,
|
|
1246
|
+
mode,
|
|
1247
|
+
path,
|
|
1248
|
+
parentAlias,
|
|
1249
|
+
parentTableName,
|
|
1250
|
+
tableName: currentTableName,
|
|
1251
|
+
});
|
|
1252
|
+
this._addRuleWhere({ response, rule: tableRules });
|
|
1253
|
+
const join = ([
|
|
1254
|
+
...(select.join || []),
|
|
1255
|
+
...(((_b = (_a = td === null || td === void 0 ? void 0 : td.rules) === null || _a === void 0 ? void 0 : _a.select) === null || _b === void 0 ? void 0 : _b.join) || [])
|
|
1256
|
+
]).map((join, i) => {
|
|
1257
|
+
const jResponse = {
|
|
1258
|
+
args: {},
|
|
1259
|
+
tables: [],
|
|
1260
|
+
where: {
|
|
1261
|
+
and: [],
|
|
1262
|
+
},
|
|
1263
|
+
};
|
|
1264
|
+
const hasTableName = 'tableName' in join;
|
|
1265
|
+
const jTableName = hasTableName ? join.tableName : join.function;
|
|
1266
|
+
const jAlias = join.alias || jTableName;
|
|
1267
|
+
const td = this.getSimpleTableDictionary({
|
|
1268
|
+
tableName: jTableName,
|
|
1269
|
+
statementType: 'select',
|
|
1270
|
+
statement: select,
|
|
1271
|
+
});
|
|
1272
|
+
this._addRuleWhere({
|
|
1273
|
+
response: jResponse,
|
|
1274
|
+
rule: this._safeResolveWhere({
|
|
1275
|
+
where: join.on,
|
|
1276
|
+
td,
|
|
1277
|
+
tableAlias: jAlias,
|
|
1278
|
+
mode,
|
|
1279
|
+
path: `${path}.${i}`,
|
|
1280
|
+
parentAlias: currentTableAlias,
|
|
1281
|
+
parentTableName: currentTableName,
|
|
1282
|
+
tableName: jTableName,
|
|
1283
|
+
}),
|
|
1284
|
+
});
|
|
1285
|
+
this._addRuleWhere({
|
|
1286
|
+
response: jResponse,
|
|
1287
|
+
rule: this._tableRules({
|
|
1288
|
+
td,
|
|
1289
|
+
alias: jAlias,
|
|
1290
|
+
mode,
|
|
1291
|
+
path: `${path}.${i}`,
|
|
1292
|
+
parentAlias: currentTableAlias,
|
|
1293
|
+
parentTableName: currentTableName,
|
|
1294
|
+
tableName: jTableName,
|
|
1295
|
+
}),
|
|
1296
|
+
});
|
|
1297
|
+
this._addRuleWhere({
|
|
1298
|
+
response,
|
|
1299
|
+
rule: { where: null, args: jResponse.args, tables: jResponse.tables },
|
|
1300
|
+
});
|
|
1301
|
+
return {
|
|
1302
|
+
on: (0, helpers_1.simplifyWhere)(jResponse.where),
|
|
1303
|
+
tableName: jTableName,
|
|
1304
|
+
alias: jAlias,
|
|
1305
|
+
mode: join.mode,
|
|
1306
|
+
};
|
|
1307
|
+
});
|
|
1308
|
+
const w = (select.with || []).map(w => {
|
|
1309
|
+
const rules = this._safeResolveSelect({
|
|
1310
|
+
mode: 'select',
|
|
1311
|
+
path: `${path}.with`,
|
|
1312
|
+
select: Object.assign(Object.assign({}, w.select), { user: null }),
|
|
1313
|
+
parentAlias: null,
|
|
1314
|
+
parentTableName: null,
|
|
1315
|
+
});
|
|
1316
|
+
this._addRuleWhere({
|
|
1317
|
+
response,
|
|
1318
|
+
rule: { where: null, args: rules.args, tables: rules.tables },
|
|
1319
|
+
});
|
|
1320
|
+
return {
|
|
1321
|
+
select: rules.select,
|
|
1322
|
+
alias: w.alias,
|
|
1323
|
+
recursive: w.recursive,
|
|
1324
|
+
};
|
|
1325
|
+
});
|
|
1326
|
+
const result = {
|
|
1327
|
+
select: {
|
|
1328
|
+
groupBy: (select.groupBy || []).map(og => {
|
|
1329
|
+
const g = this._ensureFullFieldName(og, currentTableAlias);
|
|
1330
|
+
const resolved = this._safeResolveValue({
|
|
1331
|
+
field: g,
|
|
1332
|
+
td,
|
|
1333
|
+
tableAlias: currentTableAlias,
|
|
1334
|
+
mode,
|
|
1335
|
+
path,
|
|
1336
|
+
parentAlias,
|
|
1337
|
+
parentTableName,
|
|
1338
|
+
tableName: currentTableName,
|
|
1339
|
+
});
|
|
1340
|
+
this._addRuleWhere({
|
|
1341
|
+
response,
|
|
1342
|
+
rule: {
|
|
1343
|
+
args: resolved.args,
|
|
1344
|
+
tables: resolved.tables,
|
|
1345
|
+
},
|
|
1346
|
+
});
|
|
1347
|
+
if (typeof resolved.field === 'string') {
|
|
1348
|
+
return resolved.field;
|
|
1349
|
+
}
|
|
1350
|
+
if (typeof og === 'string') {
|
|
1351
|
+
return resolved.field;
|
|
1352
|
+
}
|
|
1353
|
+
return Object.assign(Object.assign({}, resolved.field), { cast: og.cast });
|
|
1354
|
+
}),
|
|
1355
|
+
union: (select.union || []).map(u => {
|
|
1356
|
+
const result = this._safeResolveSelect({
|
|
1357
|
+
mode: 'select',
|
|
1358
|
+
path: `${path}.union`,
|
|
1359
|
+
select: u,
|
|
1360
|
+
parentAlias: currentTableAlias,
|
|
1361
|
+
parentTableName: currentTableName,
|
|
1362
|
+
});
|
|
1363
|
+
this._addRuleWhere({
|
|
1364
|
+
response,
|
|
1365
|
+
rule: { where: null, args: result.args, tables: result.tables },
|
|
1366
|
+
});
|
|
1367
|
+
return result.select;
|
|
1368
|
+
}),
|
|
1369
|
+
unionAll: (select.unionAll || []).map(u => {
|
|
1370
|
+
const result = this._safeResolveSelect({
|
|
1371
|
+
mode: 'select',
|
|
1372
|
+
path: `${path}.unionAll`,
|
|
1373
|
+
select: u,
|
|
1374
|
+
parentAlias: currentTableAlias,
|
|
1375
|
+
parentTableName: currentTableName,
|
|
1376
|
+
});
|
|
1377
|
+
this._addRuleWhere({
|
|
1378
|
+
response,
|
|
1379
|
+
rule: { where: null, args: result.args, tables: result.tables },
|
|
1380
|
+
});
|
|
1381
|
+
return result.select;
|
|
1382
|
+
}),
|
|
1383
|
+
orderBy: (select.orderBy || []).map(oo => {
|
|
1384
|
+
const o = this._ensureFullFieldName(oo, currentTableAlias);
|
|
1385
|
+
const resolved = this._safeResolveValue({
|
|
1386
|
+
field: o,
|
|
1387
|
+
td,
|
|
1388
|
+
tableAlias: currentTableAlias,
|
|
1389
|
+
mode,
|
|
1390
|
+
path,
|
|
1391
|
+
parentAlias,
|
|
1392
|
+
parentTableName,
|
|
1393
|
+
tableName: currentTableName,
|
|
1394
|
+
});
|
|
1395
|
+
this._addRuleWhere({
|
|
1396
|
+
response,
|
|
1397
|
+
rule: {
|
|
1398
|
+
args: resolved.args,
|
|
1399
|
+
tables: resolved.tables,
|
|
1400
|
+
},
|
|
1401
|
+
});
|
|
1402
|
+
if (typeof resolved.field === 'string') {
|
|
1403
|
+
return resolved.field;
|
|
1404
|
+
}
|
|
1405
|
+
if (typeof oo === 'string') {
|
|
1406
|
+
return resolved.field;
|
|
1407
|
+
}
|
|
1408
|
+
return Object.assign(Object.assign({}, resolved.field), { sort: oo.sort, cast: oo.cast });
|
|
1409
|
+
}),
|
|
1410
|
+
fields: (select.fields || []).map(of => {
|
|
1411
|
+
const f = this._ensureFullFieldName(of, currentTableAlias);
|
|
1412
|
+
const resolved = this._safeResolveValue({
|
|
1413
|
+
field: f,
|
|
1414
|
+
td,
|
|
1415
|
+
tableAlias: currentTableAlias,
|
|
1416
|
+
mode,
|
|
1417
|
+
path,
|
|
1418
|
+
parentAlias,
|
|
1419
|
+
parentTableName,
|
|
1420
|
+
tableName: currentTableName,
|
|
1421
|
+
});
|
|
1422
|
+
this._addRuleWhere({
|
|
1423
|
+
response,
|
|
1424
|
+
rule: {
|
|
1425
|
+
args: resolved.args,
|
|
1426
|
+
tables: resolved.tables,
|
|
1427
|
+
},
|
|
1428
|
+
});
|
|
1429
|
+
if (typeof resolved.field === 'string') {
|
|
1430
|
+
return resolved.field;
|
|
1431
|
+
}
|
|
1432
|
+
if (typeof of === 'string') {
|
|
1433
|
+
return resolved.field;
|
|
1434
|
+
}
|
|
1435
|
+
return Object.assign(Object.assign({}, resolved.field), { cast: of.cast, alias: of.alias });
|
|
1436
|
+
}),
|
|
1437
|
+
query: (select.query || []).map(q => {
|
|
1438
|
+
const resolvedField = this._safeResolveValue({
|
|
1439
|
+
field: q.field,
|
|
1440
|
+
td,
|
|
1441
|
+
tableAlias: currentTableAlias,
|
|
1442
|
+
mode,
|
|
1443
|
+
path,
|
|
1444
|
+
parentAlias,
|
|
1445
|
+
parentTableName,
|
|
1446
|
+
tableName: currentTableName,
|
|
1447
|
+
});
|
|
1448
|
+
this._addRuleWhere({
|
|
1449
|
+
response,
|
|
1450
|
+
rule: {
|
|
1451
|
+
args: resolvedField.args,
|
|
1452
|
+
tables: resolvedField.tables,
|
|
1453
|
+
},
|
|
1454
|
+
});
|
|
1455
|
+
const resolvedValue = this._safeResolveValue({
|
|
1456
|
+
field: q.value,
|
|
1457
|
+
td,
|
|
1458
|
+
tableAlias: currentTableAlias,
|
|
1459
|
+
mode,
|
|
1460
|
+
path,
|
|
1461
|
+
parentAlias,
|
|
1462
|
+
parentTableName,
|
|
1463
|
+
tableName: currentTableName,
|
|
1464
|
+
});
|
|
1465
|
+
this._addRuleWhere({
|
|
1466
|
+
response,
|
|
1467
|
+
rule: {
|
|
1468
|
+
args: resolvedValue.args,
|
|
1469
|
+
tables: resolvedValue.tables,
|
|
1470
|
+
},
|
|
1471
|
+
});
|
|
1472
|
+
return {
|
|
1473
|
+
field: resolvedField.field,
|
|
1474
|
+
value: resolvedValue.field,
|
|
1475
|
+
};
|
|
1476
|
+
}),
|
|
1477
|
+
from: select.from,
|
|
1478
|
+
where: (0, helpers_1.simplifyWhere)(response.where),
|
|
1479
|
+
join,
|
|
1480
|
+
args: response.args,
|
|
1481
|
+
limit: select.limit,
|
|
1482
|
+
offset: select.offset,
|
|
1483
|
+
user: select.user,
|
|
1484
|
+
forUpdate: select.forUpdate,
|
|
1485
|
+
distinct: select.distinct,
|
|
1486
|
+
distinctOn: (select.distinctOn || []).map(od => {
|
|
1487
|
+
const g = this._ensureFullFieldName(od, currentTableAlias);
|
|
1488
|
+
const resolved = this._safeResolveValue({
|
|
1489
|
+
field: g,
|
|
1490
|
+
td,
|
|
1491
|
+
tableAlias: currentTableAlias,
|
|
1492
|
+
mode,
|
|
1493
|
+
path,
|
|
1494
|
+
parentAlias,
|
|
1495
|
+
parentTableName,
|
|
1496
|
+
tableName: currentTableName,
|
|
1497
|
+
});
|
|
1498
|
+
this._addRuleWhere({
|
|
1499
|
+
response,
|
|
1500
|
+
rule: {
|
|
1501
|
+
args: resolved.args,
|
|
1502
|
+
tables: resolved.tables,
|
|
1503
|
+
},
|
|
1504
|
+
});
|
|
1505
|
+
if (typeof resolved.field === 'string') {
|
|
1506
|
+
return resolved.field;
|
|
1507
|
+
}
|
|
1508
|
+
if (typeof od === 'string') {
|
|
1509
|
+
return resolved.field;
|
|
1510
|
+
}
|
|
1511
|
+
return Object.assign(Object.assign({}, resolved.field), { cast: od.cast });
|
|
1512
|
+
}),
|
|
1513
|
+
with: w,
|
|
1514
|
+
},
|
|
1515
|
+
args: response.args,
|
|
1516
|
+
tables: _.uniqBy(response.tables, 'name'),
|
|
1517
|
+
};
|
|
1518
|
+
if (resolveWith) {
|
|
1519
|
+
const withSet = new Map();
|
|
1520
|
+
const tableList = _.uniqBy(response.tables, 'name');
|
|
1521
|
+
tableList.forEach((table) => {
|
|
1522
|
+
const rules = table.rules;
|
|
1523
|
+
if (!rules.select) {
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
const variablePrefix = table.name;
|
|
1527
|
+
const moduleName = table.module;
|
|
1528
|
+
const uModuleName = moduleName.replace(/-/g, '_');
|
|
1529
|
+
if (rules.select.useAccessControl === user_access_control_mode_1.UserAccessControlMode.User) {
|
|
1530
|
+
const select = (0, table_permission_helper_1.userPermissionByName)(variablePrefix, `${moduleName}-access`);
|
|
1531
|
+
Object.entries(select.args).forEach(([k, v]) => {
|
|
1532
|
+
result.select.args[k] = v;
|
|
1533
|
+
});
|
|
1534
|
+
withSet.set(`user_access_${uModuleName}`, {
|
|
1535
|
+
alias: `user_access_${moduleName}`,
|
|
1536
|
+
select: select
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1539
|
+
if (rules.select.useAccessControl === user_access_control_mode_1.UserAccessControlMode.Rule) {
|
|
1540
|
+
const select = (0, table_permission_helper_1.rulePermissionByName)(variablePrefix, `${moduleName}-access`);
|
|
1541
|
+
Object.entries(select.args).forEach(([k, v]) => {
|
|
1542
|
+
result.select.args[k] = v;
|
|
1543
|
+
});
|
|
1544
|
+
withSet.set(`rule_access_${uModuleName}`, {
|
|
1545
|
+
alias: `rule_access_${uModuleName}`,
|
|
1546
|
+
select
|
|
1547
|
+
});
|
|
1548
|
+
}
|
|
1549
|
+
if (rules.select.accessControl === user_access_control_mode_1.UserAccessControlMode.User) {
|
|
1550
|
+
const select = (0, table_permission_helper_1.userPermissionByName)(variablePrefix, `${table.name}-list`);
|
|
1551
|
+
Object.entries(select.args).forEach(([k, v]) => {
|
|
1552
|
+
result.select.args[k] = v;
|
|
1553
|
+
});
|
|
1554
|
+
withSet.set(`user_access_${table.name}`, {
|
|
1555
|
+
alias: `user_access_${table.name}`,
|
|
1556
|
+
select
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
if (rules.select.accessControl === user_access_control_mode_1.UserAccessControlMode.Rule) {
|
|
1560
|
+
const select = (0, table_permission_helper_1.rulePermissionByName)(variablePrefix, `${table.name}-list`);
|
|
1561
|
+
Object.entries(select.args).forEach(([k, v]) => {
|
|
1562
|
+
result.select.args[k] = v;
|
|
1563
|
+
});
|
|
1564
|
+
withSet.set(`rule_access_${table.name}`, {
|
|
1565
|
+
alias: `rule_access_${table.name}`,
|
|
1566
|
+
select
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
if (rules.select.useAccessControlEachRow) {
|
|
1570
|
+
const select = (0, table_permission_helper_1.permissionByRow)(variablePrefix, table.name, ['allow', 'admin'], rules.select.useAccessControlEachRow);
|
|
1571
|
+
Object.entries(select.args).forEach(([k, v]) => {
|
|
1572
|
+
result.select.args[k] = v;
|
|
1573
|
+
});
|
|
1574
|
+
withSet.set(`user_access_by_row_${table.name}`, {
|
|
1575
|
+
alias: `user_access_by_row_${table.name}`,
|
|
1576
|
+
select
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
});
|
|
1580
|
+
result.select.with = [...withSet.values(), ...result.select.with];
|
|
1581
|
+
}
|
|
1582
|
+
return result;
|
|
1583
|
+
}
|
|
1584
|
+
_ensureFullFieldName(field, tableAlias) {
|
|
1585
|
+
if (typeof field !== 'string') {
|
|
1586
|
+
return field;
|
|
1587
|
+
}
|
|
1588
|
+
if (field.startsWith('$')) {
|
|
1589
|
+
return field;
|
|
1590
|
+
}
|
|
1591
|
+
if (field.includes('.')) {
|
|
1592
|
+
return field;
|
|
1593
|
+
}
|
|
1594
|
+
if (field.includes('__')) {
|
|
1595
|
+
return field;
|
|
1596
|
+
}
|
|
1597
|
+
return { field: `${tableAlias}.${field}`, alias: field };
|
|
169
1598
|
}
|
|
170
1599
|
}
|
|
171
1600
|
exports.SqlSecurityLayerService = SqlSecurityLayerService;
|