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

Changes to modify user hooks to allow various scenarios #9143

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/server-core/src/hooks/createSkippableHooks.ts
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions packages/server-core/src/hooks/execute-service-hooks.ts
Original file line number Diff line number Diff line change
@@ -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<Application>) => {
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
}
}
}
Empty file modified packages/server-core/src/hooks/is-path.ts
100755 → 100644
Empty file.
69 changes: 69 additions & 0 deletions packages/server-core/src/hooks/make-query-joinable.ts
Original file line number Diff line number Diff line change
@@ -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<Application>) => {
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]
}
}
}
}
106 changes: 55 additions & 51 deletions packages/server-core/src/user/user/user.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -256,55 +257,58 @@ const handleUserSearch = async (context: HookContext<UserService>) => {
}
}

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']
)
Loading