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

Commit

Permalink
Remove unnecessary param helper function (#5552)
Browse files Browse the repository at this point in the history
* remove extractLoggedInUserFromParams

* format
  • Loading branch information
HexaField authored Mar 24, 2022
1 parent 1c1d3d1 commit e6a9a66
Show file tree
Hide file tree
Showing 34 changed files with 75 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { BadRequest, Forbidden } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'
import { Application } from './../../declarations.d'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext<Application>): Promise<HookContext> => {
const { params, app } = context
console.log(params.query)
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
const userId = loggedInUser.id
if (!params.query!.channelId) {
throw new BadRequest('Must provide a channel ID')
Expand Down
6 changes: 3 additions & 3 deletions packages/server-core/src/hooks/create-group-owner.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
// Getting logged in user and attaching owner of user
const { result } = context
const loggedInUser = extractLoggedInUserFromParams(context.params)
const { result, params } = context
const loggedInUser = params.user as UserDataType
await context.app.service('group-user').create({
groupId: result.id,
groupUserRank: 'owner',
Expand Down
6 changes: 2 additions & 4 deletions packages/server-core/src/hooks/create-party-owner.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
// Getting logged in user and attaching owner of user
const { result } = context
const { result, params } = context

const user = extractLoggedInUserFromParams(context.params)
const user = params.user // TODO: add location_admins type // as User
let ownerUser = false
if (user.location_admins) {
ownerUser =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BadRequest, Forbidden } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
let fetchedGroupId
const { id, method, params, app, path } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
if (path === 'group-user' && method === 'remove') {
const groupUser = await app.service('group-user').get(id!, null!)
fetchedGroupId = groupUser.groupId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { BadRequest } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'
import { Params } from '@feathersjs/feathers'
import _ from 'lodash'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
const { params, app } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
const groupId = params.query!.groupId
const userId = params.query!.userId || loggedInUser.id
const paramsClone = _.cloneDeep(context.params)
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/invite-remove-authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { HookContext } from '@feathersjs/feathers'
import { IdentityProviderInterface } from '@xrengine/common/src/dbmodels/IdentityProvider'

import Paginated from '../types/PageObject'
import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
let inviteIdentityProviderUser
// Getting logged in user and attaching owner of user
const { id, params, app } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
const invite = await app.service('invite').get(id!)
if (invite == null) {
throw new BadRequest('Invalid invite ID')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BadRequest } from '@feathersjs/errors'
import { Hook, HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

/**
* prevent user to join new search game more then once at time
*/
export default (): Hook => {
return async (context: HookContext): Promise<HookContext> => {
const { app } = context
const loggedInUser = extractLoggedInUserFromParams(context.params)
const { app, params } = context
const loggedInUser = params.user as UserDataType
const matchUserResult = await app.service('match-user').find({
query: {
userId: loggedInUser.id,
Expand Down
7 changes: 3 additions & 4 deletions packages/server-core/src/hooks/matchmaking-save-ticket.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Hook, HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

export default (): Hook => {
return async (context: HookContext): Promise<HookContext> => {
const { app, result, data } = context

const loggedInUser = extractLoggedInUserFromParams(context.params)
const { app, result, data, params } = context
const loggedInUser = params.user as UserDataType
await app.service('match-user').create({
ticketId: result.id,
gamemode: data.gamemode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BadRequest } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'
import { Application } from './../../declarations.d'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext<Application>): Promise<HookContext> => {
const { id, method, data, params, app } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
if (method === 'remove' || method === 'patch') {
const match = await app.service('message').Model.findOne({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { BadRequest, Forbidden } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'
import _ from 'lodash'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
try {
let fetchedPartyId
const { id, data, method, params, app, path } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
if (path === 'party-user' && method === 'remove') {
const partyUser = await app.service('party-user').get(id!)
fetchedPartyId = partyUser.partyId
Expand Down Expand Up @@ -38,7 +38,7 @@ export default () => {
if (isAdmin != null) {
data.isOwner = 1
}
await app.service('user').patch(loggedInUser.id, {
await app.service('user').patch(loggedInUser.id!, {
partyId: data.partyId
})
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { BadRequest } from '@feathersjs/errors'
import { HookContext } from '@feathersjs/feathers'
import _ from 'lodash'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
const { params, app } = context
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params.user as UserDataType
const partyId = params.query!.partyId
const userId = params.query!.userId || loggedInUser.id
const paramsClone = _.cloneDeep(context.params)
Expand Down
9 changes: 2 additions & 7 deletions packages/server-core/src/hooks/restrict-user-role.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { HookContext } from '@feathersjs/feathers'

import config from '../appconfig'
import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'

// Get the logged in user entity
const loggedInUserEntity: string = config.authentication.entity
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default (userRole: string) => {
return async (context: HookContext): Promise<HookContext> => {
// console.log('restrict user role', context.params)
if (context.params.isInternal) return context
// Getting logged in user and attaching owner of user
const loggedInUser = extractLoggedInUserFromParams(context.params)
const loggedInUser = context.params.user as UserDataType
if (loggedInUser.userRole !== userRole) {
throw new Error(`Must be ${userRole} to access this function`)
}

return context
}
}
10 changes: 3 additions & 7 deletions packages/server-core/src/hooks/send-invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import { UserId } from '@xrengine/common/src/interfaces/UserId'
import config from '../appconfig'
import logger from '../logger'
import Page from '../types/PageObject'
import {
extractLoggedInUserFromParams,
getInviteLink,
sendEmail,
sendSms
} from '../user/auth-management/auth-management.utils'
import { getInviteLink, sendEmail, sendSms } from '../user/auth-management/auth-management.utils'
import { UserRelationshipDataType } from '../user/user-relationship/user-relationship.class'
import { UserDataType } from '../user/user/user.class'
import { Application } from './../../declarations.d'

export type InviteDataType = InviteType & { targetObjectId: UserId; passcode: string }
Expand Down Expand Up @@ -108,7 +104,7 @@ export default () => {
const inviteType = result.inviteType
const targetObjectId = result.targetObjectId

const authUser = extractLoggedInUserFromParams(params)
const authUser = params.user as UserDataType

if (result.identityProviderType === 'email') {
await generateEmail(app, result, token, inviteType, authUser.name, targetObjectId)
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/set-loggedin-user-in-body.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default (propertyName: string) => {
return (context: HookContext): HookContext => {
// console.log('\n\n\n', context)
// Getting logged in user and attaching owner of user
const loggedInUser = extractLoggedInUserFromParams(context.params)
const loggedInUser = context.params.user as UserDataType
if (Array.isArray(context.data)) {
context.data = context.data.map((item) => {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/set-loggedin-user-in-query.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// TODO: Make one hook by combine this with "set-loggedin-user-in-body"
// This will attach the loggedIn user id in the query property
export default (propertyName: string) => {
return (context: HookContext): HookContext => {
// Getting logged in user and attaching owner of user
const loggedInUser = extractLoggedInUserFromParams(context.params)
const loggedInUser = context.params.user as UserDataType
context.params.query = {
...context.params.query,
[propertyName]: loggedInUser?.id || null
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/unset-self-party-owner.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'

// This will attach the owner ID in the contact while creating/updating list item
export default () => {
return async (context: HookContext): Promise<HookContext> => {
// Getting logged in user and attaching owner of user
const { result } = context
const loggedInUser = extractLoggedInUserFromParams(context.params)
const loggedInUser = context.params.user as UserDataType
if (loggedInUser?.id != null) {
const user = await context.app.service('user').get(loggedInUser.id)
if (user.partyId) {
Expand Down
6 changes: 3 additions & 3 deletions packages/server-core/src/hooks/verify-scope.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { HookContext } from '@feathersjs/feathers'

import { extractLoggedInUserFromParams } from '../user/auth-management/auth-management.utils'
import { UserDataType } from '../user/user/user.class'
import { NotFoundException, UnauthenticatedException, UnauthorizedException } from '../util/exceptions/exception'
import { Application } from './../../declarations.d'

export default (currentType: string, scopeToVerify: string) => {
return async (context: HookContext<Application>) => {
if (context.params.isInternal) return context
const loggedInUser = extractLoggedInUserFromParams(context.params)
const loggedInUser = context.params.user as UserDataType
if (!loggedInUser) throw new UnauthenticatedException('No logged in user')
const user = await context.app.service('user').get(loggedInUser.id)
const user = await context.app.service('user').get(loggedInUser.id!)
if (user.userRole === 'admin') return context
const scopes = await context.app.service('scope').Model.findAll({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Instance<T = InstanceDataType> extends Service<T> {

if (action === 'admin') {
//TODO: uncomment here
// const loggedInUser = extractLoggedInUserFromParams(params)
// const loggedInUser = params.user as UserDataType
// const user = await super.get(loggedInUser.userId);
// console.log(user);
// if (user.userRole !== 'admin') throw new Forbidden ('Must be system admin to execute this action');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AdminAuthSetting as AdminAuthSettingInterface } from '@xrengine/common/

import { Application } from '../../../declarations'
import config from '../../appconfig'
import { extractLoggedInUserFromParams } from '../../user/auth-management/auth-management.utils'
import { UserDataType } from '../../user/user/user.class'

export type AdminAuthSettingDataType = AdminAuthSettingInterface

Expand All @@ -19,7 +19,7 @@ export class Authentication<T = AdminAuthSettingDataType> extends Service<T> {

async find(params?: Params): Promise<T[] | Paginated<T>> {
const auth = (await super.find()) as any
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params!.user as UserDataType
const data = auth.data.map((el) => {
let oauth = JSON.parse(el.oauth)
let authStrategies = JSON.parse(el.authStrategies)
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/social/channel/channel.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Channel as ChannelInterface } from '@xrengine/common/src/interfaces/Cha

import { Application } from '../../../declarations'
import logger from '../../logger'
import { extractLoggedInUserFromParams } from '../../user/auth-management/auth-management.utils'
import { UserDataType } from '../../user/user/user.class'

export type ChannelDataType = ChannelInterface

Expand All @@ -32,7 +32,7 @@ export class Channel<T = ChannelDataType> extends Service<T> {
const query = params.query!
const skip = query?.skip || 0
const limit = query?.limit || 10
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params!.user as UserDataType
const userId = loggedInUser.id
const Model = (this.app.service('channel') as any).Model
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/social/group/group.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Op } from 'sequelize'
import { Group as GroupInterface } from '@xrengine/common/src/interfaces/Group'

import { Application } from '../../../declarations'
import { extractLoggedInUserFromParams } from '../../user/auth-management/auth-management.utils'
import { UserDataType } from '../../user/user/user.class'

export type GroupDataType = GroupInterface
/**
Expand All @@ -30,7 +30,7 @@ export class Group<T = GroupDataType> extends Service<T> {
*/

async find(params?: Params): Promise<Paginated<T>> {
const loggedInUser = extractLoggedInUserFromParams(params)
const loggedInUser = params!.user as UserDataType
const skip = params?.query?.$skip ? params.query.$skip : 0
const limit = params?.query?.$limit ? params.query.$limit : 10
const search = params?.query?.search
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/social/invite/invite.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Invite as InviteType } from '@xrengine/common/src/interfaces/Invite'
import { UserId } from '@xrengine/common/src/interfaces/UserId'

import { Application } from '../../../declarations'
import { extractLoggedInUserFromParams } from '../../user/auth-management/auth-management.utils'
import { UserDataType } from '../../user/user/user.class'

export type InviteDataType = InviteType & { targetObjectId: UserId; passcode: string }

Expand Down Expand Up @@ -100,7 +100,7 @@ export class Invite<T = InviteDataType> extends Service<T> {
async remove(id: string, params?: Params): Promise<T> {
const invite = await this.app.service('invite').get(id)
if (invite.inviteType === 'friend' && invite.inviteeId != null && !params?.preventUserRelationshipRemoval) {
const selfUser = extractLoggedInUserFromParams(params)
const selfUser = params!.user as UserDataType
const relatedUserId = invite.userId === selfUser.id ? invite.inviteeId : invite.userId
await this.app.service('user-relationship').remove(relatedUserId, params)
}
Expand Down
Loading

0 comments on commit e6a9a66

Please sign in to comment.