json-rest-api 1.0.25 → 1.0.26
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.
|
@@ -204,6 +204,8 @@ column('id', { scopeName: 'users', alias: 'visible_users' })
|
|
|
204
204
|
column('status', { alias: null }) // translated but deliberately unqualified
|
|
205
205
|
```
|
|
206
206
|
|
|
207
|
+
The active resource's default qualifier and storage adapter are fixed when the policy hook begins. Calling `column()` later from a Knex grouping callback therefore still targets the table or alias of that query, even after the surrounding hook has returned. This applies to normal Knex and AnyAPI queries. A `scopeName` option resolves that target resource through the storage registry; use the explicit `alias` option only when the policy itself introduces another query alias.
|
|
208
|
+
|
|
207
209
|
`value()` accepts the same `scopeName` option:
|
|
208
210
|
|
|
209
211
|
```js
|
package/package.json
CHANGED
|
@@ -71,14 +71,6 @@ const SELECT_METHODS = new Set(['select', 'columns', 'distinct'])
|
|
|
71
71
|
|
|
72
72
|
const AGGREGATE_METHODS = new Set(['count', 'min', 'max', 'sum', 'avg'])
|
|
73
73
|
|
|
74
|
-
const CALLBACK_FIRST_METHODS = new Set([
|
|
75
|
-
'where',
|
|
76
|
-
'andWhere',
|
|
77
|
-
'orWhere',
|
|
78
|
-
'having',
|
|
79
|
-
'orHaving',
|
|
80
|
-
])
|
|
81
|
-
|
|
82
74
|
const isFunction = (value) => typeof value === 'function'
|
|
83
75
|
const isObject = (value) => value && typeof value === 'object' && !Array.isArray(value)
|
|
84
76
|
|
|
@@ -149,13 +141,6 @@ const translateJoinObject = (adapter, obj) => {
|
|
|
149
141
|
return result
|
|
150
142
|
}
|
|
151
143
|
|
|
152
|
-
const maybeWrapCallback = (adapter, callback) => {
|
|
153
|
-
if (!isFunction(callback)) return callback
|
|
154
|
-
return function wrappedCallback (...args) {
|
|
155
|
-
return callback.apply(adapter.proxy, args)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
144
|
const isRaw = (value) => Boolean(value && typeof value === 'object' && typeof value.toSQL === 'function' && value.toSQL().method)
|
|
160
145
|
|
|
161
146
|
const ensureArray = (value) => (Array.isArray(value) ? value : [value])
|
|
@@ -317,12 +302,39 @@ export class AnyapiQueryAdapter {
|
|
|
317
302
|
this.aliasActualNames.set(alias, alias)
|
|
318
303
|
}
|
|
319
304
|
|
|
320
|
-
#
|
|
305
|
+
#wrapQueryCallback (callback) {
|
|
306
|
+
if (!isFunction(callback)) return callback
|
|
307
|
+
const adapter = this
|
|
308
|
+
|
|
309
|
+
return function wrappedCallback (...args) {
|
|
310
|
+
const callbackTarget = this
|
|
311
|
+
const callbackProxy = adapter.#createProxy(callbackTarget)
|
|
312
|
+
const callbackArgs = args.map((arg) => arg === callbackTarget ? callbackProxy : arg)
|
|
313
|
+
return callback.apply(callbackProxy, callbackArgs)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
#wrapJoinCallback (callback) {
|
|
318
|
+
if (!isFunction(callback)) return callback
|
|
319
|
+
const adapter = this
|
|
320
|
+
|
|
321
|
+
return function wrappedCallback (...args) {
|
|
322
|
+
const callbackTarget = this
|
|
323
|
+
const callbackProxy = adapter.#createJoinProxy(callbackTarget)
|
|
324
|
+
const callbackArgs = args.map((arg) => arg === callbackTarget ? callbackProxy : arg)
|
|
325
|
+
return callback.apply(callbackProxy, callbackArgs)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
#translateWhereArgs (args, { joinClause = false } = {}) {
|
|
321
330
|
if (args.length === 0) return args
|
|
322
331
|
const [first, ...rest] = args
|
|
323
332
|
|
|
324
333
|
if (isFunction(first)) {
|
|
325
|
-
|
|
334
|
+
const wrappedGroup = joinClause
|
|
335
|
+
? this.#wrapJoinCallback(first)
|
|
336
|
+
: this.#wrapQueryCallback(first)
|
|
337
|
+
return [wrappedGroup, ...rest]
|
|
326
338
|
}
|
|
327
339
|
|
|
328
340
|
if (isObject(first) && !isRaw(first)) {
|
|
@@ -337,7 +349,7 @@ export class AnyapiQueryAdapter {
|
|
|
337
349
|
|
|
338
350
|
if (rest.length > 0 && isFunction(rest[rest.length - 1])) {
|
|
339
351
|
const newRest = [...rest]
|
|
340
|
-
newRest[newRest.length - 1] =
|
|
352
|
+
newRest[newRest.length - 1] = this.#wrapQueryCallback(rest[rest.length - 1])
|
|
341
353
|
return [translatedFirst, ...newRest]
|
|
342
354
|
}
|
|
343
355
|
|
|
@@ -350,7 +362,7 @@ export class AnyapiQueryAdapter {
|
|
|
350
362
|
const translatedColumn = this.translateColumn(column)
|
|
351
363
|
|
|
352
364
|
if (isFunction(values)) {
|
|
353
|
-
return [translatedColumn,
|
|
365
|
+
return [translatedColumn, this.#wrapQueryCallback(values), ...rest]
|
|
354
366
|
}
|
|
355
367
|
|
|
356
368
|
return [translatedColumn, values, ...rest]
|
|
@@ -439,7 +451,7 @@ export class AnyapiQueryAdapter {
|
|
|
439
451
|
const [first, ...rest] = args
|
|
440
452
|
|
|
441
453
|
if (isFunction(first)) {
|
|
442
|
-
return [
|
|
454
|
+
return [this.#wrapJoinCallback(first), ...rest]
|
|
443
455
|
}
|
|
444
456
|
|
|
445
457
|
if (isObject(first) && !isRaw(first)) {
|
|
@@ -454,7 +466,7 @@ export class AnyapiQueryAdapter {
|
|
|
454
466
|
|
|
455
467
|
const translatedRest = rest.map((value, index) => {
|
|
456
468
|
if (isFunction(value)) {
|
|
457
|
-
return
|
|
469
|
+
return this.#wrapJoinCallback(value)
|
|
458
470
|
}
|
|
459
471
|
if (typeof value !== 'string') {
|
|
460
472
|
return value
|
|
@@ -475,7 +487,7 @@ export class AnyapiQueryAdapter {
|
|
|
475
487
|
const translatedColumn = typeof column === 'string' ? this.translateColumn(column) : column
|
|
476
488
|
|
|
477
489
|
if (isFunction(values)) {
|
|
478
|
-
return [translatedColumn,
|
|
490
|
+
return [translatedColumn, this.#wrapQueryCallback(values), ...rest]
|
|
479
491
|
}
|
|
480
492
|
|
|
481
493
|
return [translatedColumn, values, ...rest]
|
|
@@ -510,7 +522,7 @@ export class AnyapiQueryAdapter {
|
|
|
510
522
|
let translatedArgs = args
|
|
511
523
|
|
|
512
524
|
if (WHERE_METHODS.has(prop)) {
|
|
513
|
-
translatedArgs = adapter.#translateWhereArgs(
|
|
525
|
+
translatedArgs = adapter.#translateWhereArgs(args, { joinClause: true })
|
|
514
526
|
} else if (WHERE_IN_METHODS.has(prop)) {
|
|
515
527
|
translatedArgs = adapter.#translateWhereInArgs(args)
|
|
516
528
|
} else if (BETWEEN_METHODS.has(prop)) {
|
|
@@ -607,7 +619,7 @@ export class AnyapiQueryAdapter {
|
|
|
607
619
|
let translatedArgs = args
|
|
608
620
|
|
|
609
621
|
if (WHERE_METHODS.has(prop)) {
|
|
610
|
-
translatedArgs = adapter.#translateWhereArgs(
|
|
622
|
+
translatedArgs = adapter.#translateWhereArgs(args)
|
|
611
623
|
} else if (WHERE_IN_METHODS.has(prop)) {
|
|
612
624
|
translatedArgs = adapter.#translateWhereInArgs(args)
|
|
613
625
|
} else if (BETWEEN_METHODS.has(prop)) {
|
|
@@ -630,8 +642,6 @@ export class AnyapiQueryAdapter {
|
|
|
630
642
|
translatedArgs = adapter.#translateSelectArgs(args)
|
|
631
643
|
} else if (AGGREGATE_METHODS.has(prop)) {
|
|
632
644
|
translatedArgs = adapter.#translateAggregateArgs(args)
|
|
633
|
-
} else if (CALLBACK_FIRST_METHODS.has(prop) && args.length && isFunction(args[0])) {
|
|
634
|
-
translatedArgs = [maybeWrapCallback(adapter, args[0]), ...args.slice(1)]
|
|
635
645
|
}
|
|
636
646
|
|
|
637
647
|
const result = value.apply(target, translatedArgs)
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export const createStorageAdapterUtilities = (hookParams, { getStorageAdapter } = {}) => {
|
|
2
2
|
const context = hookParams?.context || {}
|
|
3
|
+
const activeQuery = context.knexQuery || {}
|
|
4
|
+
const activeScopeName = activeQuery.scopeName
|
|
5
|
+
const activeTableName = activeQuery.tableName
|
|
6
|
+
const activeStorageAdapter = activeQuery.storageAdapter || context.storageAdapter
|
|
3
7
|
const storageCache = new Map()
|
|
4
8
|
|
|
5
9
|
const fetchStorageAdapter = (scopeName) => {
|
|
@@ -7,13 +11,9 @@ export const createStorageAdapterUtilities = (hookParams, { getStorageAdapter }
|
|
|
7
11
|
if (storageCache.has(scopeName)) return storageCache.get(scopeName)
|
|
8
12
|
|
|
9
13
|
let adapter = null
|
|
10
|
-
if (scopeName ===
|
|
11
|
-
adapter =
|
|
12
|
-
context.knexQuery?.storageAdapter ||
|
|
14
|
+
if (scopeName === activeScopeName) {
|
|
15
|
+
adapter = activeStorageAdapter ||
|
|
13
16
|
(typeof getStorageAdapter === 'function' ? getStorageAdapter(scopeName) : null)
|
|
14
|
-
if (adapter && !context.storageAdapter) {
|
|
15
|
-
context.storageAdapter = adapter
|
|
16
|
-
}
|
|
17
17
|
} else {
|
|
18
18
|
adapter = typeof getStorageAdapter === 'function' ? getStorageAdapter(scopeName) : null
|
|
19
19
|
}
|
|
@@ -23,8 +23,8 @@ export const createStorageAdapterUtilities = (hookParams, { getStorageAdapter }
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const defaultAliasForScope = (scopeName) => {
|
|
26
|
-
if (scopeName ===
|
|
27
|
-
return
|
|
26
|
+
if (scopeName === activeScopeName) {
|
|
27
|
+
return activeTableName || scopeName
|
|
28
28
|
}
|
|
29
29
|
return scopeName
|
|
30
30
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Api } from 'hooked-api'
|
|
2
2
|
import {
|
|
3
|
+
AutoFilterPlugin,
|
|
3
4
|
FileHandlingPlugin,
|
|
4
5
|
QueryProjectionsPlugin,
|
|
5
6
|
RestApiAnyapiKnexPlugin,
|
|
@@ -1733,21 +1734,38 @@ export async function createRowPolicyApi (knex, pluginOptions = {}) {
|
|
|
1733
1734
|
return false
|
|
1734
1735
|
}
|
|
1735
1736
|
|
|
1736
|
-
query.
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1737
|
+
query.where(function visibilityGrants () {
|
|
1738
|
+
this.whereRaw('1 = 0')
|
|
1739
|
+
this.orWhere(function groupVisibilityGrant () {
|
|
1740
|
+
this.whereIn(
|
|
1741
|
+
column('access_group'),
|
|
1742
|
+
groups.map((group) => value('access_group', group))
|
|
1743
|
+
)
|
|
1744
|
+
})
|
|
1745
|
+
})
|
|
1740
1746
|
return true
|
|
1741
1747
|
},
|
|
1742
1748
|
...(rowPolicyOptions.policies || {})
|
|
1743
1749
|
}
|
|
1744
1750
|
})
|
|
1745
1751
|
|
|
1752
|
+
await api.use(AutoFilterPlugin, {
|
|
1753
|
+
resolvers: {
|
|
1754
|
+
workspace: ({ context }) => context.scopeValues?.workspaceId
|
|
1755
|
+
},
|
|
1756
|
+
presets: {
|
|
1757
|
+
workspace: {
|
|
1758
|
+
filters: [{ field: 'workspace_id', resolver: 'workspace' }]
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
})
|
|
1762
|
+
|
|
1746
1763
|
await api.addResource('policy_projects', {
|
|
1747
1764
|
schema: {
|
|
1748
1765
|
id: { type: 'id' },
|
|
1749
1766
|
name: { type: 'string', required: true, max: 200 },
|
|
1750
|
-
access_group: { type: 'string', required: true }
|
|
1767
|
+
access_group: { type: 'string', required: true },
|
|
1768
|
+
workspace_id: { type: 'string', required: true }
|
|
1751
1769
|
},
|
|
1752
1770
|
relationships: {
|
|
1753
1771
|
tasks: { type: 'hasMany', target: 'policy_tasks', foreignKey: 'project_id' },
|
|
@@ -1759,6 +1777,7 @@ export async function createRowPolicyApi (knex, pluginOptions = {}) {
|
|
|
1759
1777
|
otherKey: 'task_id'
|
|
1760
1778
|
}
|
|
1761
1779
|
},
|
|
1780
|
+
autofilter: 'workspace',
|
|
1762
1781
|
rowPolicy: 'groupVisibility',
|
|
1763
1782
|
tableName: 'row_policy_projects'
|
|
1764
1783
|
})
|
|
@@ -1770,6 +1789,7 @@ export async function createRowPolicyApi (knex, pluginOptions = {}) {
|
|
|
1770
1789
|
id: { type: 'id' },
|
|
1771
1790
|
title: { type: 'string', required: true, max: 200, search: true },
|
|
1772
1791
|
access_group: { type: 'string', required: true },
|
|
1792
|
+
workspace_id: { type: 'string', required: true },
|
|
1773
1793
|
project_id: {
|
|
1774
1794
|
type: 'number',
|
|
1775
1795
|
nullable: true,
|
|
@@ -1787,6 +1807,7 @@ export async function createRowPolicyApi (knex, pluginOptions = {}) {
|
|
|
1787
1807
|
otherKey: 'project_id'
|
|
1788
1808
|
}
|
|
1789
1809
|
},
|
|
1810
|
+
autofilter: 'workspace',
|
|
1790
1811
|
rowPolicy: 'groupVisibility',
|
|
1791
1812
|
tableName: 'row_policy_tasks'
|
|
1792
1813
|
})
|
package/tests/row-policy.test.js
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from './helpers/test-utils.js'
|
|
12
12
|
import { storageMode } from './helpers/storage-mode.js'
|
|
13
13
|
import { createRowPolicyApi } from './fixtures/api-configs.js'
|
|
14
|
+
import { createStorageAdapterUtilities } from '../plugins/core/lib/querying/storage-adapter-utils.js'
|
|
14
15
|
|
|
15
16
|
const knex = knexLib({
|
|
16
17
|
client: 'better-sqlite3',
|
|
@@ -23,17 +24,23 @@ const knex = knexLib({
|
|
|
23
24
|
let api
|
|
24
25
|
const policyEvents = []
|
|
25
26
|
|
|
26
|
-
const adminContext = () => ({
|
|
27
|
-
|
|
27
|
+
const adminContext = (workspaceId = 'workspace-a') => ({
|
|
28
|
+
visibility: { all: true },
|
|
29
|
+
scopeValues: { workspaceId }
|
|
30
|
+
})
|
|
31
|
+
const groupContext = (...groups) => ({
|
|
32
|
+
visibility: { groups },
|
|
33
|
+
scopeValues: { workspaceId: 'workspace-a' }
|
|
34
|
+
})
|
|
28
35
|
|
|
29
|
-
const postProject = async (name, accessGroup) => {
|
|
36
|
+
const postProject = async (name, accessGroup, context = adminContext()) => {
|
|
30
37
|
return api.resources.policy_projects.post({
|
|
31
38
|
inputRecord: createJsonApiDocument('policy_projects', {
|
|
32
39
|
name,
|
|
33
40
|
access_group: accessGroup
|
|
34
41
|
}),
|
|
35
42
|
simplified: false
|
|
36
|
-
},
|
|
43
|
+
}, context)
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
const postTask = async ({ title, accessGroup, projectId, context = adminContext() }) => {
|
|
@@ -53,6 +60,39 @@ const postTask = async ({ title, accessGroup, projectId, context = adminContext(
|
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
describe('RowPolicy Plugin', () => {
|
|
63
|
+
it('keeps active query translation stable after the hook context is restored', () => {
|
|
64
|
+
const queryStorageAdapter = {
|
|
65
|
+
translateColumn: (field) => field === 'id' ? 'record_id' : field
|
|
66
|
+
}
|
|
67
|
+
const ambientStorageAdapter = {
|
|
68
|
+
translateColumn: (field) => field === 'id' ? 'wrong_id' : field
|
|
69
|
+
}
|
|
70
|
+
const context = {
|
|
71
|
+
knexQuery: {
|
|
72
|
+
scopeName: 'organisationUnits',
|
|
73
|
+
tableName: 'organisation_units',
|
|
74
|
+
storageAdapter: queryStorageAdapter
|
|
75
|
+
},
|
|
76
|
+
storageAdapter: ambientStorageAdapter
|
|
77
|
+
}
|
|
78
|
+
const utilities = createStorageAdapterUtilities({ context })
|
|
79
|
+
|
|
80
|
+
delete context.knexQuery
|
|
81
|
+
delete context.storageAdapter
|
|
82
|
+
|
|
83
|
+
assert.equal(
|
|
84
|
+
utilities.translateColumn('organisationUnits', 'id'),
|
|
85
|
+
'organisation_units.record_id'
|
|
86
|
+
)
|
|
87
|
+
assert.equal(
|
|
88
|
+
utilities.translateColumn('organisationUnits', 'id', 'visible_units'),
|
|
89
|
+
'visible_units.record_id'
|
|
90
|
+
)
|
|
91
|
+
assert.equal(utilities.defaultAliasForScope('users'), 'users')
|
|
92
|
+
assert.equal(Object.hasOwn(context, 'knexQuery'), false)
|
|
93
|
+
assert.equal(Object.hasOwn(context, 'storageAdapter'), false)
|
|
94
|
+
})
|
|
95
|
+
|
|
56
96
|
before(async () => {
|
|
57
97
|
api = await createRowPolicyApi(knex, {
|
|
58
98
|
onPolicy: (event) => policyEvents.push(event)
|
|
@@ -73,7 +113,7 @@ describe('RowPolicy Plugin', () => {
|
|
|
73
113
|
])
|
|
74
114
|
})
|
|
75
115
|
|
|
76
|
-
it('applies visibility before offset pagination and counts the same dataset', async () => {
|
|
116
|
+
it('applies deferred grouped visibility before offset pagination and counts the same dataset', async () => {
|
|
77
117
|
await postProject('Allowed 1', 'group-a')
|
|
78
118
|
await postProject('Hidden 1', 'group-b')
|
|
79
119
|
await postProject('Allowed 2', 'group-a')
|
|
@@ -112,6 +152,22 @@ describe('RowPolicy Plugin', () => {
|
|
|
112
152
|
)
|
|
113
153
|
})
|
|
114
154
|
|
|
155
|
+
it('combines row policy visibility with the resource autofilter', async () => {
|
|
156
|
+
await postProject('Visible', 'group-a')
|
|
157
|
+
await postProject('Wrong group', 'group-b')
|
|
158
|
+
await postProject('Wrong workspace', 'group-a', adminContext('workspace-b'))
|
|
159
|
+
|
|
160
|
+
const result = await api.resources.policy_projects.query({
|
|
161
|
+
queryParams: { sort: ['id'] },
|
|
162
|
+
simplified: false
|
|
163
|
+
}, groupContext('group-a'))
|
|
164
|
+
|
|
165
|
+
assert.deepEqual(
|
|
166
|
+
result.data.map((record) => record.attributes.name),
|
|
167
|
+
['Visible']
|
|
168
|
+
)
|
|
169
|
+
})
|
|
170
|
+
|
|
115
171
|
it('keeps cursor pagination inside the visible dataset', async () => {
|
|
116
172
|
await postProject('Allowed 1', 'group-a')
|
|
117
173
|
await postProject('Hidden 1', 'group-b')
|
|
@@ -404,7 +460,7 @@ describe('RowPolicy Plugin', () => {
|
|
|
404
460
|
const result = await api.resources.policy_projects.query({
|
|
405
461
|
queryParams: { page: { number: 1, size: 10 } },
|
|
406
462
|
simplified: false
|
|
407
|
-
})
|
|
463
|
+
}, { scopeValues: { workspaceId: 'workspace-a' } })
|
|
408
464
|
|
|
409
465
|
assert.deepEqual(result.data, [])
|
|
410
466
|
assert.equal(result.meta.pagination.total, 0)
|