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

Commit d49f5a1

Browse files
ScopeType enforced (#9271)
* Name change: - Changed name of `interface ScopeType` to `interface ScopeTypeInterface` * ScopeType enforced * Merge fix --------- Co-authored-by: Josh Field <[email protected]>
1 parent ee5e6e2 commit d49f5a1

File tree

23 files changed

+120
-99
lines changed

23 files changed

+120
-99
lines changed

β€Žpackages/engine/src/common/functions/checkScope.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Ethereal Engine. All Rights Reserved.
2424
*/
2525

2626
import { Engine } from '../../ecs/classes/Engine'
27-
import { ScopeType, scopePath } from '../../schemas/scope/scope.schema'
27+
import { ScopeTypeInterface, scopePath } from '../../schemas/scope/scope.schema'
2828
import { UserType } from '../../schemas/user/user.schema'
2929

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

3838
if (!scopes || scopes.length === 0) {
3939
return false

β€Žpackages/engine/src/schemas/scope/scope-type.schema.tsβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ Ethereal Engine. All Rights Reserved.
2626
// For more information about this file see https://dove.feathersjs.com/guides/cli/service.schemas.html
2727
import type { Static } from '@feathersjs/typebox'
2828
import { getValidator, querySyntax, Type } from '@feathersjs/typebox'
29+
import { TypedString } from '../../common/types/TypeboxUtils'
2930
import { dataValidator, queryValidator } from '../validators'
31+
import { ScopeType } from './scope.schema'
3032

3133
export const scopeTypePath = 'scope-type'
3234

@@ -35,7 +37,9 @@ export const scopeTypeMethods = ['find', 'get'] as const
3537
// Main data model schema
3638
export const scopeTypeSchema = Type.Object(
3739
{
38-
type: Type.String(),
40+
type: TypedString<ScopeType>({
41+
format: 'uuid'
42+
}),
3943
createdAt: Type.String({ format: 'date-time' }),
4044
updatedAt: Type.String({ format: 'date-time' })
4145
},

β€Žpackages/engine/src/schemas/scope/scope.schema.tsβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { dataValidator, queryValidator } from '../validators'
3434
export const scopePath = 'scope'
3535

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

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

@@ -43,7 +44,9 @@ export const scopeSchema = Type.Object(
4344
id: TypedString<ScopeID>({
4445
format: 'uuid'
4546
}),
46-
type: Type.String(),
47+
type: TypedString<ScopeType>({
48+
format: 'uuid'
49+
}),
4750
userId: TypedString<UserID>({
4851
format: 'uuid'
4952
}),
@@ -52,7 +55,7 @@ export const scopeSchema = Type.Object(
5255
},
5356
{ $id: 'Scope', additionalProperties: false }
5457
)
55-
export interface ScopeType extends Static<typeof scopeSchema> {}
58+
export interface ScopeTypeInterface extends Static<typeof scopeSchema> {}
5659

5760
// Schema for creating new entries
5861
export const scopeDataSchema = Type.Pick(scopeSchema, ['type', 'userId'], {

β€Žpackages/engine/src/schemas/user/user.schema.tsβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type { Static } from '@feathersjs/typebox'
2929
import { getValidator, querySyntax, Type } from '@feathersjs/typebox'
3030
import { TypedString } from '../../common/types/TypeboxUtils'
3131
import { instanceAttendanceSchema } from '../networking/instance-attendance.schema'
32+
import { ScopeType } from '../scope/scope.schema'
3233
import { locationAdminSchema } from '../social/location-admin.schema'
3334
import { locationBanSchema } from '../social/location-ban.schema'
3435
import { userSettingSchema } from '../user/user-setting.schema'
@@ -43,7 +44,9 @@ export const userMethods = ['get', 'find', 'create', 'patch', 'remove'] as const
4344

4445
export const userScopeSchema = Type.Object(
4546
{
46-
type: Type.String()
47+
type: TypedString<ScopeType>({
48+
format: 'uuid'
49+
})
4750
},
4851
{ $id: 'UserScope', additionalProperties: false }
4952
)

β€Žpackages/server-core/src/hooks/is-scope.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { HookContext } from '@feathersjs/feathers'
2727

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

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

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

4646
const currentScopes = scopes.reduce<string[]>((result, sc) => {

β€Žpackages/server-core/src/hooks/verify-scope.test.tsβ€Ž

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import assert from 'assert'
2828

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

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

103103
await app.service(scopePath).create({
104-
type: 'location:read',
104+
type: 'location:read' as ScopeType,
105105
userId: user.id
106106
})
107107

@@ -129,7 +129,7 @@ describe('verify-scope', () => {
129129
})
130130

131131
await app.service(scopePath).create({
132-
type: 'location:read',
132+
type: 'location:read' as ScopeType,
133133
userId: user.id
134134
})
135135

@@ -165,12 +165,12 @@ describe('verify-scope', () => {
165165
})
166166

167167
await app.service(scopePath).create({
168-
type: 'location:read',
168+
type: 'location:read' as ScopeType,
169169
userId: user.id
170170
})
171171

172172
await app.service(scopePath).create({
173-
type: 'admin:admin',
173+
type: 'admin:admin' as ScopeType,
174174
userId: user.id
175175
})
176176

β€Žpackages/server-core/src/hooks/verify-scope.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { HookContext } from '@feathersjs/feathers'
2727

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

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

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

4747
const currentScopes = scopes.reduce<string[]>((result, sc) => {

β€Žpackages/server-core/src/networking/instance/instance.tsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
instancePath,
3131
InstanceType
3232
} from '@etherealengine/engine/src/schemas/networking/instance.schema'
33-
import { scopePath, ScopeType } from '@etherealengine/engine/src/schemas/scope/scope.schema'
33+
import { scopePath, ScopeType, ScopeTypeInterface } from '@etherealengine/engine/src/schemas/scope/scope.schema'
3434
import { channelPath, ChannelType } from '@etherealengine/engine/src/schemas/social/channel.schema'
3535
import { UserID } from '@etherealengine/engine/src/schemas/user/user.schema'
3636
import { Paginated } from '@feathersjs/feathers'
@@ -74,10 +74,10 @@ export default (app: Application): void => {
7474
try {
7575
const adminScopes = (await app.service(scopePath).find({
7676
query: {
77-
type: 'admin:admin'
77+
type: 'admin:admin' as ScopeType
7878
},
7979
paginate: false
80-
})) as ScopeType[]
80+
})) as ScopeTypeInterface[]
8181

8282
const targetIds = adminScopes.map((admin) => admin.userId)
8383
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions

β€Žpackages/server-core/src/projects/project-permission/project-permission.test.tsβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
projectPermissionPath
3636
} from '@etherealengine/engine/src/schemas/projects/project-permission.schema'
3737
import { projectPath } from '@etherealengine/engine/src/schemas/projects/project.schema'
38-
import { scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
38+
import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
3939
import { AvatarID } from '@etherealengine/engine/src/schemas/user/avatar.schema'
4040
import { UserApiKeyType, userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api-key.schema'
4141
import { InviteCode, UserID, UserName, UserType, userPath } from '@etherealengine/engine/src/schemas/user/user.schema'
@@ -129,27 +129,27 @@ describe('project-permission.test', () => {
129129
})) as Paginated<UserApiKeyType>
130130
user4.apiKey = user4ApiKeys.data.length > 0 ? user4ApiKeys.data[0] : user4.apiKey
131131
await app.service(scopePath).create({
132-
type: 'editor:write',
132+
type: 'editor:write' as ScopeType,
133133
userId: user1.id
134134
})
135135
await app.service(scopePath).create({
136-
type: 'editor:write',
136+
type: 'editor:write' as ScopeType,
137137
userId: user2.id
138138
})
139139
await app.service(scopePath).create({
140-
type: 'editor:write',
140+
type: 'editor:write' as ScopeType,
141141
userId: user3.id
142142
})
143143
await app.service(scopePath).create({
144-
type: 'editor:write',
144+
type: 'editor:write' as ScopeType,
145145
userId: user4.id
146146
})
147147
await app.service(scopePath).create({
148-
type: 'projects:read',
148+
type: 'projects:read' as ScopeType,
149149
userId: user4.id
150150
})
151151
await app.service(scopePath).create({
152-
type: 'projects:write',
152+
type: 'projects:write' as ScopeType,
153153
userId: user4.id
154154
})
155155
})

β€Žpackages/server-core/src/projects/project/project.tsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
projectPermissionPath
3333
} from '@etherealengine/engine/src/schemas/projects/project-permission.schema'
3434
import { ProjectType, projectMethods, projectPath } from '@etherealengine/engine/src/schemas/projects/project.schema'
35-
import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
35+
import { ScopeType, ScopeTypeInterface, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
3636
import { UserID } from '@etherealengine/engine/src/schemas/user/user.schema'
3737
import { Application } from '../../../declarations'
3838
import { ProjectService } from './project.class'
@@ -78,10 +78,10 @@ export default (app: Application): void => {
7878

7979
const projectReadScopes = (await app.service(scopePath).find({
8080
query: {
81-
type: 'projects:read'
81+
type: 'projects:read' as ScopeType
8282
},
8383
paginate: false
84-
})) as ScopeType[]
84+
})) as ScopeTypeInterface[]
8585

8686
targetIds = targetIds.concat(projectReadScopes.map((admin) => admin.userId!))
8787
targetIds = _.uniq(targetIds)

0 commit comments

Comments
Β (0)