This commit is contained in:
2026-09-02 09:19:43 +03:00
parent 74cb5b6176
commit 962f489142
34 changed files with 1190 additions and 124 deletions
@@ -0,0 +1,43 @@
import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
import TenantAccessService, {
type ResolvedTenant,
} from '#base_modules/Tenant/services/tenant_access_service'
type TenantContext = ResolvedTenant
export default class ResolveTenantMiddleware {
async handle(ctx: HttpContext, next: NextFn) {
const header = ctx.request.header('x-tenant-id')
if (!header)
return ctx.response.badRequest({
code: 'E_TENANT_REQUIRED',
message: 'The X-Tenant-Id header is required.',
})
const tenantId = Number(header)
if (!Number.isSafeInteger(tenantId) || tenantId < 1) {
return ctx.response.badRequest({
code: 'E_INVALID_TENANT_ID',
message: 'The X-Tenant-Id header must be a positive integer.',
})
}
const tenant = await new TenantAccessService().resolveForAdmin(ctx.auth.user!.id, tenantId)
if (!tenant)
return ctx.response.forbidden({
code: 'E_TENANT_ACCESS_DENIED',
message: 'You do not have access to this tenant.',
})
ctx.tenant = tenant
return next()
}
}
declare module '@adonisjs/core/http' {
export interface HttpContext {
tenant: TenantContext
}
}