Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit b6813f9

Browse files
authored
Changes to modify user hooks to allow various scenarios (#9143)
* Changes to modify user hooks to allow various scenarios * Added hook to make query joinable * Added createSkippableHooks method and make find and remove method of user hooks skippable
1 parent 3d7924a commit b6813f9

File tree

5 files changed

+224
-51
lines changed

5 files changed

+224
-51
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
CPAL-1.0 License
3+
4+
The contents of this file are subject to the Common Public Attribution License
5+
Version 1.0. (the "License"); you may not use this file except in compliance
6+
with the License. You may obtain a copy of the License at
7+
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
8+
The License is based on the Mozilla Public License Version 1.1, but Sections 14
9+
and 15 have been added to cover use of software over a computer network and
10+
provide for limited attribution for the Original Developer. In addition,
11+
Exhibit A has been modified to be consistent with Exhibit B.
12+
13+
Software distributed under the License is distributed on an "AS IS" basis,
14+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
15+
specific language governing rights and limitations under the License.
16+
17+
The Original Code is Ethereal Engine.
18+
19+
The Original Developer is the Initial Developer. The Initial Developer of the
20+
Original Code is the Ethereal Engine team.
21+
22+
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
23+
Ethereal Engine. All Rights Reserved.
24+
*/
25+
26+
import { iff } from 'feathers-hooks-common'
27+
28+
/**
29+
* This can create a skippable hook that will not be executed if `context.params.skipServiceHooks` is set to true.
30+
* @param hooks Service hook object
31+
* @param typesMethods If its undefined then the check will be performs for all hook types and methods, for string or array, it can be for hook type, service method type or a [Hook_Type].[Service_Method] notation
32+
*/
33+
export const createSkippableHooks = (hooks: any, typesMethods?: string | string[]) => {
34+
if (typesMethods) {
35+
typesMethods = Array.isArray(typesMethods) ? typesMethods : [typesMethods]
36+
}
37+
38+
for (const hookType in hooks) {
39+
for (const serviceMethod in hooks[hookType]) {
40+
if (
41+
!typesMethods || // apply for all if nothing is specified in typesMethods
42+
typesMethods.includes(hookType) || // apply if hook type is specified in typesMethods
43+
typesMethods.includes(serviceMethod) || // apply if service method is specified in typesMethods
44+
typesMethods.includes(`${hookType}.${serviceMethod}`) // apply if `[Hook_Type].[Service_Method]` is specified in typesMethods
45+
) {
46+
hooks[hookType][serviceMethod] = [
47+
iff((context) => context.params.skipServiceHooks !== true, ...hooks[hookType][serviceMethod])
48+
]
49+
}
50+
}
51+
}
52+
53+
return hooks
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
CPAL-1.0 License
3+
4+
The contents of this file are subject to the Common Public Attribution License
5+
Version 1.0. (the "License"); you may not use this file except in compliance
6+
with the License. You may obtain a copy of the License at
7+
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
8+
The License is based on the Mozilla Public License Version 1.1, but Sections 14
9+
and 15 have been added to cover use of software over a computer network and
10+
provide for limited attribution for the Original Developer. In addition,
11+
Exhibit A has been modified to be consistent with Exhibit B.
12+
13+
Software distributed under the License is distributed on an "AS IS" basis,
14+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
15+
specific language governing rights and limitations under the License.
16+
17+
The Original Code is Ethereal Engine.
18+
19+
The Original Developer is the Initial Developer. The Initial Developer of the
20+
Original Code is the Ethereal Engine team.
21+
22+
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
23+
Ethereal Engine. All Rights Reserved.
24+
*/
25+
26+
import { Application, HookContext } from '../../declarations'
27+
28+
/**
29+
* A hook used to execute service hooks
30+
*/
31+
export default (hooks: any, servicePath?: string | string[]) => {
32+
return async (context: HookContext<Application>) => {
33+
if (servicePath) {
34+
servicePath = Array.isArray(servicePath) ? servicePath : [servicePath]
35+
}
36+
37+
if (!servicePath || servicePath.includes(context.path)) {
38+
// First we need to call before hook so that
39+
for (const hook of hooks[context.type][context.method]) {
40+
context = await hook(context)
41+
}
42+
43+
context.params.skipServiceHooks = true
44+
}
45+
}
46+
}

packages/server-core/src/hooks/is-path.ts

100755100644
File mode changed.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
CPAL-1.0 License
3+
4+
The contents of this file are subject to the Common Public Attribution License
5+
Version 1.0. (the "License"); you may not use this file except in compliance
6+
with the License. You may obtain a copy of the License at
7+
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
8+
The License is based on the Mozilla Public License Version 1.1, but Sections 14
9+
and 15 have been added to cover use of software over a computer network and
10+
provide for limited attribution for the Original Developer. In addition,
11+
Exhibit A has been modified to be consistent with Exhibit B.
12+
13+
Software distributed under the License is distributed on an "AS IS" basis,
14+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
15+
specific language governing rights and limitations under the License.
16+
17+
The Original Code is Ethereal Engine.
18+
19+
The Original Developer is the Initial Developer. The Initial Developer of the
20+
Original Code is the Ethereal Engine team.
21+
22+
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
23+
Ethereal Engine. All Rights Reserved.
24+
*/
25+
26+
import { Application, HookContext } from '../../declarations'
27+
28+
/**
29+
* A hook to make context.params.query joinable, such that if
30+
* a column is specified without table name, then table name will
31+
* be appended to it.
32+
*/
33+
export default (tableName: string) => {
34+
return async (context: HookContext<Application>) => {
35+
const allowedDollarProps = ['$and', '$or', '$select', '$sort']
36+
37+
for (const queryItem in context.params.query) {
38+
if (queryItem.startsWith('$') && !allowedDollarProps.includes(queryItem)) {
39+
continue
40+
}
41+
42+
// If property name already contains a dot, then it contains table name.
43+
if (queryItem.includes('.')) {
44+
return
45+
}
46+
47+
if (allowedDollarProps.includes(queryItem)) {
48+
if (Array.isArray(context.params.query[queryItem])) {
49+
for (const index in context.params.query[queryItem]) {
50+
for (const subQueryItem in context.params.query[queryItem][index]) {
51+
context.params.query[queryItem][index][`${tableName}.${subQueryItem}`] =
52+
context.params.query[queryItem][index][subQueryItem]
53+
delete context.params.query[queryItem][index][subQueryItem]
54+
}
55+
}
56+
} else {
57+
for (const subQueryItem in context.params.query[queryItem]) {
58+
context.params.query[queryItem][`${tableName}.${subQueryItem}`] =
59+
context.params.query[queryItem][subQueryItem]
60+
delete context.params.query[queryItem][subQueryItem]
61+
}
62+
}
63+
} else {
64+
context.params.query[`${tableName}.${queryItem}`] = context.params.query[queryItem]
65+
delete context.params.query[queryItem]
66+
}
67+
}
68+
}
69+
}

packages/server-core/src/user/user/user.hooks.ts

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
import { userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api-key.schema'
4444
import { userSettingPath } from '@etherealengine/engine/src/schemas/user/user-setting.schema'
4545
import { HookContext } from '../../../declarations'
46+
import { createSkippableHooks } from '../../hooks/createSkippableHooks'
4647
import disallowNonId from '../../hooks/disallow-non-id'
4748
import persistData from '../../hooks/persist-data'
4849
import verifyScope from '../../hooks/verify-scope'
@@ -256,55 +257,58 @@ const handleUserSearch = async (context: HookContext<UserService>) => {
256257
}
257258
}
258259

259-
export default {
260-
around: {
261-
all: [schemaHooks.resolveExternal(userExternalResolver), schemaHooks.resolveResult(userResolver)]
262-
},
263-
264-
before: {
265-
all: [() => schemaHooks.validateQuery(userQueryValidator), schemaHooks.resolveQuery(userQueryResolver)],
266-
find: [
267-
iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch),
268-
iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier'))
269-
],
270-
get: [],
271-
create: [
272-
iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write')),
273-
() => schemaHooks.validateData(userDataValidator),
274-
schemaHooks.resolveData(userDataResolver),
275-
persistData,
276-
discard('scopes')
277-
],
278-
update: [iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write'))],
279-
patch: [
280-
iff(isProvider('external'), restrictUserPatch),
281-
() => schemaHooks.validateData(userPatchValidator),
282-
schemaHooks.resolveData(userPatchResolver),
283-
disallowNonId,
284-
removeUserScopes,
285-
addUserScopes(false),
286-
discard('scopes')
287-
],
288-
remove: [iff(isProvider('external'), disallowNonId, restrictUserRemove), removeApiKey]
289-
},
290-
291-
after: {
292-
all: [],
293-
find: [],
294-
get: [],
295-
create: [addUserSettings, addUserScopes(true), addApiKey, updateInviteCode],
296-
update: [],
297-
patch: [updateInviteCode],
298-
remove: []
260+
export default createSkippableHooks(
261+
{
262+
around: {
263+
all: [schemaHooks.resolveExternal(userExternalResolver), schemaHooks.resolveResult(userResolver)]
264+
},
265+
266+
before: {
267+
all: [() => schemaHooks.validateQuery(userQueryValidator), schemaHooks.resolveQuery(userQueryResolver)],
268+
find: [
269+
iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch),
270+
iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier') as any)
271+
],
272+
get: [],
273+
create: [
274+
iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write')),
275+
() => schemaHooks.validateData(userDataValidator),
276+
schemaHooks.resolveData(userDataResolver),
277+
persistData,
278+
discard('scopes')
279+
],
280+
update: [iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write'))],
281+
patch: [
282+
iff(isProvider('external'), restrictUserPatch),
283+
() => schemaHooks.validateData(userPatchValidator),
284+
schemaHooks.resolveData(userPatchResolver),
285+
disallowNonId,
286+
removeUserScopes,
287+
addUserScopes(false),
288+
discard('scopes')
289+
],
290+
remove: [iff(isProvider('external'), disallowNonId, restrictUserRemove), removeApiKey]
291+
},
292+
293+
after: {
294+
all: [],
295+
find: [],
296+
get: [],
297+
create: [addUserSettings, addUserScopes(true), addApiKey, updateInviteCode],
298+
update: [],
299+
patch: [updateInviteCode],
300+
remove: []
301+
},
302+
303+
error: {
304+
all: [],
305+
find: [],
306+
get: [],
307+
create: [],
308+
update: [],
309+
patch: [],
310+
remove: []
311+
}
299312
},
300-
301-
error: {
302-
all: [],
303-
find: [],
304-
get: [],
305-
create: [],
306-
update: [],
307-
patch: [],
308-
remove: []
309-
}
310-
} as any
313+
['find', 'remove']
314+
)

0 commit comments

Comments
 (0)