Files
speckle-server/packages/server/modules/core/index.ts
T
Gergő Jedlicska 968d2f2520 auth/lib (#4242)
* wip

* wip

* feat(authz): wip policy shape

* wip

* fix(authz): canReadProject with latest pattern

* wip

* feat(shared): simplify authz checks and policies

* feat(shared): port role weights into shared

* test(shared): some more tests for authz

* test(shared): more query project tests

* typo!

* feat(shared): ff loading refinements

* feat(shared): example authorization policy integration

* authz loaders init

* chore(authz): naming etc

* wip

* fix(authz): authz error objects

Co-authored-by: Kristaps Fabians Geikins <fabis94@users.noreply.github.com>
Co-authored-by: Gergő Jedlicska <gjedlicska@users.noreply.github.com>

* fix(authz): use correct role weights

* chore(authz): use codes from errors in tests

* chore(authz): wow

* chore(authz): fix more tests, add more tests

* chore(authz): fix some tests, add some tests (again)

* fix(authz): fix tests again

* fix(server): you need to await !!!! otherwise it crashes the server.

---------

Co-authored-by: Charles Driesler <chuck@speckle.systems>
Co-authored-by: Kristaps Fabians Geikins <fabis94@users.noreply.github.com>
Co-authored-by: Gergő Jedlicska <gjedlicska@users.noreply.github.com>
2025-03-21 16:37:36 +01:00

102 lines
3.3 KiB
TypeScript

import { moduleLogger } from '@/observability/logging'
import {
setupResultListener,
shutdownResultListener
} from '@/modules/core/utils/dbNotificationListener'
import * as mp from '@/modules/shared/utils/mixpanel'
import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
import staticRest from '@/modules/core/rest/static'
import uploadRest from '@/modules/core/rest/upload'
import downloadRest from '@/modules/core/rest/download'
import diffUpload from '@/modules/core/rest/diffUpload'
import diffDownload from '@/modules/core/rest/diffDownload'
import scopes from '@/modules/core/scopes'
import roles from '@/modules/core/roles'
import { getGenericRedis } from '@/modules/shared/redis/redis'
import { registerOrUpdateScopeFactory } from '@/modules/shared/repositories/scopes'
import db from '@/db/knex'
import { registerOrUpdateRole } from '@/modules/shared/repositories/roles'
import { isTestEnv } from '@/modules/shared/helpers/envHelper'
import { HooksConfig, Hook, ExecuteHooks } from '@/modules/core/hooks'
import { reportSubscriptionEventsFactory } from '@/modules/core/events/subscriptionListeners'
import { getEventBus } from '@/modules/shared/services/eventBus'
import { publish } from '@/modules/shared/utils/subscriptions'
import { getStreamCollaboratorsFactory } from '@/modules/core/repositories/streams'
import { defineModuleLoaders } from '@/modules/core/authz'
let stopTestSubs: (() => void) | undefined = undefined
const coreModule: SpeckleModule<{
hooks: HooksConfig
addHook: (key: keyof HooksConfig, hook: Hook) => void
executeHooks: ExecuteHooks
}> = {
hooks: {
onCreateObjectRequest: [],
onCreateVersionRequest: []
},
addHook(key: keyof HooksConfig, callback: Hook) {
this.hooks[key].push(callback)
},
async executeHooks(key: keyof HooksConfig, { projectId }: { projectId: string }) {
return await Promise.all(this.hooks[key].map(async (cb) => await cb({ projectId })))
},
async init({ app, isInitial }) {
moduleLogger.info('💥 Init core module')
// Initialize the static route
staticRest(app)
// Initialises the two main bulk upload/download endpoints
uploadRest(app, { executeHooks: this.executeHooks.bind(this) })
downloadRest(app)
// Initialises the two diff-based upload/download endpoints
diffUpload(app)
diffDownload(app)
const scopeRegisterFunc = registerOrUpdateScopeFactory({ db })
// Register core-based scoeps
for (const scope of scopes) {
await scopeRegisterFunc({ scope })
}
const roleRegisterFunc = registerOrUpdateRole({ db })
// Register core-based roles
for (const role of roles) {
await roleRegisterFunc({ role })
}
if (isInitial) {
// Setup global pg notification listener
setupResultListener()
// Init mp
mp.initialize()
// Setup test subs
if (isTestEnv()) {
const { startEmittingTestSubs } = await import('@/test/graphqlHelper')
stopTestSubs = await startEmittingTestSubs()
}
// Setup GQL sub emits
reportSubscriptionEventsFactory({
eventListen: getEventBus().listen,
publish,
getStreamCollaborators: getStreamCollaboratorsFactory({ db })
})()
}
defineModuleLoaders()
},
async shutdown() {
await shutdownResultListener()
await getGenericRedis().quit()
stopTestSubs?.()
}
}
export = coreModule