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

ScopeType enforced #9271

Merged
merged 18 commits into from
Nov 22, 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
4 changes: 2 additions & 2 deletions packages/engine/src/common/functions/checkScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ethereal Engine. All Rights Reserved.
*/

import { Engine } from '../../ecs/classes/Engine'
import { ScopeType, scopePath } from '../../schemas/scope/scope.schema'
import { ScopeTypeInterface, scopePath } from '../../schemas/scope/scope.schema'
import { UserType } from '../../schemas/user/user.schema'

export const checkScope = async (user: UserType, currentType: string, scopeToVerify: string) => {
Expand All @@ -33,7 +33,7 @@ export const checkScope = async (user: UserType, currentType: string, scopeToVer
userId: user.id,
paginate: false
}
})) as any as ScopeType[]
})) as any as ScopeTypeInterface[]

if (!scopes || scopes.length === 0) {
return false
Expand Down
6 changes: 5 additions & 1 deletion packages/engine/src/schemas/scope/scope-type.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Ethereal Engine. All Rights Reserved.
// For more information about this file see https://dove.feathersjs.com/guides/cli/service.schemas.html
import type { Static } from '@feathersjs/typebox'
import { getValidator, querySyntax, Type } from '@feathersjs/typebox'
import { TypedString } from '../../common/types/TypeboxUtils'
import { dataValidator, queryValidator } from '../validators'
import { ScopeType } from './scope.schema'

export const scopeTypePath = 'scope-type'

Expand All @@ -35,7 +37,9 @@ export const scopeTypeMethods = ['find', 'get'] as const
// Main data model schema
export const scopeTypeSchema = Type.Object(
{
type: Type.String(),
type: TypedString<ScopeType>({
format: 'uuid'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HexaField this shouldnt be uuid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, perhaps. Does that affect the database?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't but maybe at some time in future it will. I have made the fix in #9331

}),
createdAt: Type.String({ format: 'date-time' }),
updatedAt: Type.String({ format: 'date-time' })
},
Expand Down
7 changes: 5 additions & 2 deletions packages/engine/src/schemas/scope/scope.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { dataValidator, queryValidator } from '../validators'
export const scopePath = 'scope'

export const scopeMethods = ['create', 'find', 'remove'] as const
export type ScopeType = OpaqueType<'ScopeType'> & string

export type ScopeID = OpaqueType<'ScopeID'> & string

Expand All @@ -43,7 +44,9 @@ export const scopeSchema = Type.Object(
id: TypedString<ScopeID>({
format: 'uuid'
}),
type: Type.String(),
type: TypedString<ScopeType>({
format: 'uuid'
}),
userId: TypedString<UserID>({
format: 'uuid'
}),
Expand All @@ -52,7 +55,7 @@ export const scopeSchema = Type.Object(
},
{ $id: 'Scope', additionalProperties: false }
)
export interface ScopeType extends Static<typeof scopeSchema> {}
export interface ScopeTypeInterface extends Static<typeof scopeSchema> {}

// Schema for creating new entries
export const scopeDataSchema = Type.Pick(scopeSchema, ['type', 'userId'], {
Expand Down
5 changes: 4 additions & 1 deletion packages/engine/src/schemas/user/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { Static } from '@feathersjs/typebox'
import { getValidator, querySyntax, Type } from '@feathersjs/typebox'
import { TypedString } from '../../common/types/TypeboxUtils'
import { instanceAttendanceSchema } from '../networking/instance-attendance.schema'
import { ScopeType } from '../scope/scope.schema'
import { locationAdminSchema } from '../social/location-admin.schema'
import { locationBanSchema } from '../social/location-ban.schema'
import { userSettingSchema } from '../user/user-setting.schema'
Expand All @@ -43,7 +44,9 @@ export const userMethods = ['get', 'find', 'create', 'patch', 'remove'] as const

export const userScopeSchema = Type.Object(
{
type: Type.String()
type: TypedString<ScopeType>({
format: 'uuid'
})
},
{ $id: 'UserScope', additionalProperties: false }
)
Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/is-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { HookContext } from '@feathersjs/feathers'

import { UserType } from '@etherealengine/engine/src/schemas/user/user.schema'

import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { ScopeTypeInterface, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { Application } from '../../declarations'

export default (currentType: string, scopeToVerify: string) => {
Expand All @@ -40,7 +40,7 @@ export default (currentType: string, scopeToVerify: string) => {
userId: loggedInUser.id
},
paginate: false
})) as ScopeType[]
})) as ScopeTypeInterface[]
if (!scopes || scopes.length === 0) return false

const currentScopes = scopes.reduce<string[]>((result, sc) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/server-core/src/hooks/verify-scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import assert from 'assert'

import { destroyEngine } from '@etherealengine/engine/src/ecs/classes/Engine'

import { scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { scopePath, ScopeType } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { AvatarID } from '@etherealengine/engine/src/schemas/user/avatar.schema'
import { userApiKeyPath, UserApiKeyType } from '@etherealengine/engine/src/schemas/user/user-api-key.schema'
import { InviteCode, UserName, userPath, UserType } from '@etherealengine/engine/src/schemas/user/user.schema'
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('verify-scope', () => {
})

await app.service(scopePath).create({
type: 'location:read',
type: 'location:read' as ScopeType,
userId: user.id
})

Expand Down Expand Up @@ -129,7 +129,7 @@ describe('verify-scope', () => {
})

await app.service(scopePath).create({
type: 'location:read',
type: 'location:read' as ScopeType,
userId: user.id
})

Expand Down Expand Up @@ -165,12 +165,12 @@ describe('verify-scope', () => {
})

await app.service(scopePath).create({
type: 'location:read',
type: 'location:read' as ScopeType,
userId: user.id
})

await app.service(scopePath).create({
type: 'admin:admin',
type: 'admin:admin' as ScopeType,
userId: user.id
})

Expand Down
4 changes: 2 additions & 2 deletions packages/server-core/src/hooks/verify-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { HookContext } from '@feathersjs/feathers'

import { UserType } from '@etherealengine/engine/src/schemas/user/user.schema'

import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { ScopeTypeInterface, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { Forbidden, NotAuthenticated, NotFound } from '@feathersjs/errors'
import { Application } from '../../declarations'

Expand All @@ -41,7 +41,7 @@ export default (currentType: string, scopeToVerify: string) => {
userId: loggedInUser.id
},
paginate: false
})) as ScopeType[]
})) as ScopeTypeInterface[]
if (!scopes || scopes.length === 0) throw new NotFound('No scope available for the current user.')

const currentScopes = scopes.reduce<string[]>((result, sc) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/server-core/src/networking/instance/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
instancePath,
InstanceType
} from '@etherealengine/engine/src/schemas/networking/instance.schema'
import { scopePath, ScopeType } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { scopePath, ScopeType, ScopeTypeInterface } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { channelPath, ChannelType } from '@etherealengine/engine/src/schemas/social/channel.schema'
import { UserID } from '@etherealengine/engine/src/schemas/user/user.schema'
import { Paginated } from '@feathersjs/feathers'
Expand Down Expand Up @@ -74,10 +74,10 @@ export default (app: Application): void => {
try {
const adminScopes = (await app.service(scopePath).find({
query: {
type: 'admin:admin'
type: 'admin:admin' as ScopeType
},
paginate: false
})) as ScopeType[]
})) as ScopeTypeInterface[]

const targetIds = adminScopes.map((admin) => admin.userId)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
projectPermissionPath
} from '@etherealengine/engine/src/schemas/projects/project-permission.schema'
import { projectPath } from '@etherealengine/engine/src/schemas/projects/project.schema'
import { scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { AvatarID } from '@etherealengine/engine/src/schemas/user/avatar.schema'
import { UserApiKeyType, userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api-key.schema'
import { InviteCode, UserID, UserName, UserType, userPath } from '@etherealengine/engine/src/schemas/user/user.schema'
Expand Down Expand Up @@ -129,27 +129,27 @@ describe('project-permission.test', () => {
})) as Paginated<UserApiKeyType>
user4.apiKey = user4ApiKeys.data.length > 0 ? user4ApiKeys.data[0] : user4.apiKey
await app.service(scopePath).create({
type: 'editor:write',
type: 'editor:write' as ScopeType,
userId: user1.id
})
await app.service(scopePath).create({
type: 'editor:write',
type: 'editor:write' as ScopeType,
userId: user2.id
})
await app.service(scopePath).create({
type: 'editor:write',
type: 'editor:write' as ScopeType,
userId: user3.id
})
await app.service(scopePath).create({
type: 'editor:write',
type: 'editor:write' as ScopeType,
userId: user4.id
})
await app.service(scopePath).create({
type: 'projects:read',
type: 'projects:read' as ScopeType,
userId: user4.id
})
await app.service(scopePath).create({
type: 'projects:write',
type: 'projects:write' as ScopeType,
userId: user4.id
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/server-core/src/projects/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
projectPermissionPath
} from '@etherealengine/engine/src/schemas/projects/project-permission.schema'
import { ProjectType, projectMethods, projectPath } from '@etherealengine/engine/src/schemas/projects/project.schema'
import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { ScopeType, ScopeTypeInterface, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { UserID } from '@etherealengine/engine/src/schemas/user/user.schema'
import { Application } from '../../../declarations'
import { ProjectService } from './project.class'
Expand Down Expand Up @@ -78,10 +78,10 @@ export default (app: Application): void => {

const projectReadScopes = (await app.service(scopePath).find({
query: {
type: 'projects:read'
type: 'projects:read' as ScopeType
},
paginate: false
})) as ScopeType[]
})) as ScopeTypeInterface[]

targetIds = targetIds.concat(projectReadScopes.map((admin) => admin.userId!))
targetIds = _.uniq(targetIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Ethereal Engine. All Rights Reserved.
*/

import { ScopeTypeType, scopeTypePath } from '@etherealengine/engine/src/schemas/scope/scope-type.schema'
import { ScopeType } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { clientSettingPath } from '@etherealengine/engine/src/schemas/setting/client-setting.schema'
import type { Knex } from 'knex'
import { getDateTimeSql } from '../../../util/datetime-sql'
Expand All @@ -41,12 +42,12 @@ export async function up(knex: Knex): Promise<void> {
if (tableExists === true) {
const scopeTypeData: ScopeTypeType[] = [
{
type: `${clientSettingPath}:read`,
type: `${clientSettingPath}:read` as ScopeType,
createdAt: await getDateTimeSql(),
updatedAt: await getDateTimeSql()
},
{
type: `${clientSettingPath}:write`,
type: `${clientSettingPath}:write` as ScopeType,
createdAt: await getDateTimeSql(),
updatedAt: await getDateTimeSql()
}
Expand Down
Loading