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/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 old mode 100755 new mode 100644 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] + } + } + } +} diff --git a/packages/server-core/src/user/user/user.hooks.ts b/packages/server-core/src/user/user/user.hooks.ts index 8ab6a9456c..2b6b8113ae 100755 --- a/packages/server-core/src/user/user/user.hooks.ts +++ b/packages/server-core/src/user/user/user.hooks.ts @@ -43,6 +43,7 @@ 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 persistData from '../../hooks/persist-data' import verifyScope from '../../hooks/verify-scope' @@ -256,55 +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: [ - iff(isProvider('external'), verifyScope('admin', 'admin'), verifyScope('user', 'read'), handleUserSearch), - iff(isProvider('external'), discardQuery('search', '$sort.accountIdentifier')) - ], - 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'] +)