From 3f35e44e6b4e35db6c4f24495ff2706d029dc06a Mon Sep 17 00:00:00 2001 From: hanzlamateen Date: Wed, 25 Oct 2023 14:56:13 +0500 Subject: [PATCH 1/3] Changes to modify user hooks to allow various scenarios --- .../src/hooks/execute-service-hooks.ts | 46 +++++++++++++++++++ packages/server-core/src/hooks/is-path.ts | 33 +++++++++++++ .../src/hooks/is-skip-service-hooks.ts | 35 ++++++++++++++ .../server-core/src/user/user/user.hooks.ts | 13 ++++-- 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 packages/server-core/src/hooks/execute-service-hooks.ts create mode 100644 packages/server-core/src/hooks/is-path.ts create mode 100644 packages/server-core/src/hooks/is-skip-service-hooks.ts diff --git a/packages/server-core/src/hooks/execute-service-hooks.ts b/packages/server-core/src/hooks/execute-service-hooks.ts new file mode 100644 index 0000000000..ef6d34464e --- /dev/null +++ b/packages/server-core/src/hooks/execute-service-hooks.ts @@ -0,0 +1,46 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { Application, HookContext } from '../../declarations' + +/** + * A hook used to execute service hooks + */ +export default (hooks: any, servicePath?: string | string[]) => { + return async (context: HookContext) => { + if (servicePath) { + servicePath = Array.isArray(servicePath) ? servicePath : [servicePath] + } + + if (!servicePath || servicePath.includes(context.path)) { + // First we need to call before hook so that + for (const hook of hooks[context.type][context.method]) { + context = await hook(context) + } + + context.params.skipServiceHooks = true + } + } +} diff --git a/packages/server-core/src/hooks/is-path.ts b/packages/server-core/src/hooks/is-path.ts new file mode 100644 index 0000000000..e8a86b46c1 --- /dev/null +++ b/packages/server-core/src/hooks/is-path.ts @@ -0,0 +1,33 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { HookContext } from '../../declarations' + +export default (...params: string[]) => { + const args = Array.from(params) + return (context: HookContext): boolean => { + return args.includes(context.path) + } +} diff --git a/packages/server-core/src/hooks/is-skip-service-hooks.ts b/packages/server-core/src/hooks/is-skip-service-hooks.ts new file mode 100644 index 0000000000..2a064acdd1 --- /dev/null +++ b/packages/server-core/src/hooks/is-skip-service-hooks.ts @@ -0,0 +1,35 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { Application, HookContext } from '../../declarations' + +/** + * A hook used to check if service hooks should be executed + */ +export default () => { + return async (context: HookContext) => { + return context.params.skipServiceHooks === true + } +} diff --git a/packages/server-core/src/user/user/user.hooks.ts b/packages/server-core/src/user/user/user.hooks.ts index a77b9df8bb..d0d6562a2e 100755 --- a/packages/server-core/src/user/user/user.hooks.ts +++ b/packages/server-core/src/user/user/user.hooks.ts @@ -33,7 +33,7 @@ import { } from '@etherealengine/engine/src/schemas/user/user.schema' import { hooks as schemaHooks } from '@feathersjs/schema' -import { discard, discardQuery, iff, isProvider } from 'feathers-hooks-common' +import { discard, discardQuery, iff, iffElse, isProvider } from 'feathers-hooks-common' import { scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema' import { @@ -44,6 +44,7 @@ import { userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api import { userSettingPath } from '@etherealengine/engine/src/schemas/user/user-setting.schema' import { HookContext } from '../../../declarations' import disallowNonId from '../../hooks/disallow-non-id' +import isSkipServiceHooks from '../../hooks/is-skip-service-hooks' import persistData from '../../hooks/persist-data' import verifyScope from '../../hooks/verify-scope' import getFreeInviteCode from '../../util/get-free-invite-code' @@ -264,8 +265,14 @@ export default { before: { all: [() => schemaHooks.validateQuery(userQueryValidator), schemaHooks.resolveQuery(userQueryResolver)], find: [ - iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch), - iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier')) + iffElse( + isSkipServiceHooks(), + [], + [ + iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch), + iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier') as any) + ] + ) ], get: [], create: [ From 6e45610d1bafdf8c60e31b603192060f716c323e Mon Sep 17 00:00:00 2001 From: hanzlamateen Date: Wed, 25 Oct 2023 15:14:14 +0500 Subject: [PATCH 2/3] Added hook to make query joinable --- .../src/hooks/make-query-joinable.ts | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 packages/server-core/src/hooks/make-query-joinable.ts diff --git a/packages/server-core/src/hooks/make-query-joinable.ts b/packages/server-core/src/hooks/make-query-joinable.ts new file mode 100644 index 0000000000..b721e66192 --- /dev/null +++ b/packages/server-core/src/hooks/make-query-joinable.ts @@ -0,0 +1,69 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { Application, HookContext } from '../../declarations' + +/** + * A hook to make context.params.query joinable, such that if + * a column is specified without table name, then table name will + * be appended to it. + */ +export default (tableName: string) => { + return async (context: HookContext) => { + const allowedDollarProps = ['$and', '$or', '$select', '$sort'] + + for (const queryItem in context.params.query) { + if (queryItem.startsWith('$') && !allowedDollarProps.includes(queryItem)) { + continue + } + + // If property name already contains a dot, then it contains table name. + if (queryItem.includes('.')) { + return + } + + if (allowedDollarProps.includes(queryItem)) { + if (Array.isArray(context.params.query[queryItem])) { + for (const index in context.params.query[queryItem]) { + for (const subQueryItem in context.params.query[queryItem][index]) { + context.params.query[queryItem][index][`${tableName}.${subQueryItem}`] = + context.params.query[queryItem][index][subQueryItem] + delete context.params.query[queryItem][index][subQueryItem] + } + } + } else { + for (const subQueryItem in context.params.query[queryItem]) { + context.params.query[queryItem][`${tableName}.${subQueryItem}`] = + context.params.query[queryItem][subQueryItem] + delete context.params.query[queryItem][subQueryItem] + } + } + } else { + context.params.query[`${tableName}.${queryItem}`] = context.params.query[queryItem] + delete context.params.query[queryItem] + } + } + } +} From c99220365a083f677587e34d91749d16b23a6778 Mon Sep 17 00:00:00 2001 From: hanzlamateen Date: Thu, 26 Oct 2023 15:24:28 +0500 Subject: [PATCH 3/3] Added createSkippableHooks method and make find and remove method of user hooks skippable --- .../src/hooks/createSkippableHooks.ts | 54 ++++++++ .../src/hooks/is-skip-service-hooks.ts | 35 ------ .../server-core/src/user/user/user.hooks.ts | 115 +++++++++--------- 3 files changed, 110 insertions(+), 94 deletions(-) create mode 100644 packages/server-core/src/hooks/createSkippableHooks.ts delete mode 100644 packages/server-core/src/hooks/is-skip-service-hooks.ts diff --git a/packages/server-core/src/hooks/createSkippableHooks.ts b/packages/server-core/src/hooks/createSkippableHooks.ts new file mode 100644 index 0000000000..61e9cd037c --- /dev/null +++ b/packages/server-core/src/hooks/createSkippableHooks.ts @@ -0,0 +1,54 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { iff } from 'feathers-hooks-common' + +/** + * This can create a skippable hook that will not be executed if `context.params.skipServiceHooks` is set to true. + * @param hooks Service hook object + * @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 + */ +export const createSkippableHooks = (hooks: any, typesMethods?: string | string[]) => { + if (typesMethods) { + typesMethods = Array.isArray(typesMethods) ? typesMethods : [typesMethods] + } + + for (const hookType in hooks) { + for (const serviceMethod in hooks[hookType]) { + if ( + !typesMethods || // apply for all if nothing is specified in typesMethods + typesMethods.includes(hookType) || // apply if hook type is specified in typesMethods + typesMethods.includes(serviceMethod) || // apply if service method is specified in typesMethods + typesMethods.includes(`${hookType}.${serviceMethod}`) // apply if `[Hook_Type].[Service_Method]` is specified in typesMethods + ) { + hooks[hookType][serviceMethod] = [ + iff((context) => context.params.skipServiceHooks !== true, ...hooks[hookType][serviceMethod]) + ] + } + } + } + + return hooks +} diff --git a/packages/server-core/src/hooks/is-skip-service-hooks.ts b/packages/server-core/src/hooks/is-skip-service-hooks.ts deleted file mode 100644 index 2a064acdd1..0000000000 --- a/packages/server-core/src/hooks/is-skip-service-hooks.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* -CPAL-1.0 License - -The contents of this file are subject to the Common Public Attribution License -Version 1.0. (the "License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at -https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. -The License is based on the Mozilla Public License Version 1.1, but Sections 14 -and 15 have been added to cover use of software over a computer network and -provide for limited attribution for the Original Developer. In addition, -Exhibit A has been modified to be consistent with Exhibit B. - -Software distributed under the License is distributed on an "AS IS" basis, -WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the -specific language governing rights and limitations under the License. - -The Original Code is Ethereal Engine. - -The Original Developer is the Initial Developer. The Initial Developer of the -Original Code is the Ethereal Engine team. - -All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 -Ethereal Engine. All Rights Reserved. -*/ - -import { Application, HookContext } from '../../declarations' - -/** - * A hook used to check if service hooks should be executed - */ -export default () => { - return async (context: HookContext) => { - return context.params.skipServiceHooks === true - } -} diff --git a/packages/server-core/src/user/user/user.hooks.ts b/packages/server-core/src/user/user/user.hooks.ts index d0d6562a2e..b2d1ec2098 100755 --- a/packages/server-core/src/user/user/user.hooks.ts +++ b/packages/server-core/src/user/user/user.hooks.ts @@ -33,7 +33,7 @@ import { } from '@etherealengine/engine/src/schemas/user/user.schema' import { hooks as schemaHooks } from '@feathersjs/schema' -import { discard, discardQuery, iff, iffElse, isProvider } from 'feathers-hooks-common' +import { discard, discardQuery, iff, isProvider } from 'feathers-hooks-common' import { scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema' import { @@ -43,8 +43,8 @@ import { import { userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api-key.schema' import { userSettingPath } from '@etherealengine/engine/src/schemas/user/user-setting.schema' import { HookContext } from '../../../declarations' +import { createSkippableHooks } from '../../hooks/createSkippableHooks' import disallowNonId from '../../hooks/disallow-non-id' -import isSkipServiceHooks from '../../hooks/is-skip-service-hooks' import persistData from '../../hooks/persist-data' import verifyScope from '../../hooks/verify-scope' import getFreeInviteCode from '../../util/get-free-invite-code' @@ -257,61 +257,58 @@ const handleUserSearch = async (context: HookContext) => { } } -export default { - around: { - all: [schemaHooks.resolveExternal(userExternalResolver), schemaHooks.resolveResult(userResolver)] - }, - - before: { - all: [() => schemaHooks.validateQuery(userQueryValidator), schemaHooks.resolveQuery(userQueryResolver)], - find: [ - iffElse( - isSkipServiceHooks(), - [], - [ - iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch), - iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier') as any) - ] - ) - ], - get: [], - create: [ - iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write')), - () => schemaHooks.validateData(userDataValidator), - schemaHooks.resolveData(userDataResolver), - persistData, - discard('scopes') - ], - update: [iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write'))], - patch: [ - iff(isProvider('external'), restrictUserPatch), - () => schemaHooks.validateData(userPatchValidator), - schemaHooks.resolveData(userPatchResolver), - disallowNonId, - removeUserScopes, - addUserScopes(false), - discard('scopes') - ], - remove: [iff(isProvider('external'), disallowNonId, restrictUserRemove), removeApiKey] - }, - - after: { - all: [], - find: [], - get: [], - create: [addUserSettings, addUserScopes(true), addApiKey, updateInviteCode], - update: [], - patch: [updateInviteCode], - remove: [] +export default createSkippableHooks( + { + around: { + all: [schemaHooks.resolveExternal(userExternalResolver), schemaHooks.resolveResult(userResolver)] + }, + + before: { + all: [() => schemaHooks.validateQuery(userQueryValidator), schemaHooks.resolveQuery(userQueryResolver)], + find: [ + iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch), + iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier') as any) + ], + get: [], + create: [ + iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write')), + () => schemaHooks.validateData(userDataValidator), + schemaHooks.resolveData(userDataResolver), + persistData, + discard('scopes') + ], + update: [iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'write'))], + patch: [ + iff(isProvider('external'), restrictUserPatch), + () => schemaHooks.validateData(userPatchValidator), + schemaHooks.resolveData(userPatchResolver), + disallowNonId, + removeUserScopes, + addUserScopes(false), + discard('scopes') + ], + remove: [iff(isProvider('external'), disallowNonId, restrictUserRemove), removeApiKey] + }, + + after: { + all: [], + find: [], + get: [], + create: [addUserSettings, addUserScopes(true), addApiKey, updateInviteCode], + update: [], + patch: [updateInviteCode], + remove: [] + }, + + error: { + all: [], + find: [], + get: [], + create: [], + update: [], + patch: [], + remove: [] + } }, - - error: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [] - } -} as any + ['find', 'remove'] +)