Merge pull request #832 from bigcapitalhq/validate-tenant-existance-in-guards

fix: validate request org id existance in guards
This commit is contained in:
Ahmed Bouhuolia
2025-10-25 15:20:42 +02:00
committed by GitHub
3 changed files with 16 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ export class EnsureTenantIsInitializedGuard implements CanActivate {
constructor(
private readonly tenancyContext: TenancyContext,
private reflector: Reflector,
) {}
) { }
/**
* Validate the tenant of the current request is initialized..
@@ -41,6 +41,12 @@ export class EnsureTenantIsInitializedGuard implements CanActivate {
}
const tenant = await this.tenancyContext.getTenant();
if (!tenant) {
throw new UnauthorizedException({
message: 'Tenant not found.',
errors: [{ type: 'TENANT.NOT.FOUND' }],
});
}
if (!tenant?.initializedAt) {
throw new UnauthorizedException({
statusCode: 400,

View File

@@ -19,7 +19,7 @@ export class EnsureTenantIsSeededGuard implements CanActivate {
constructor(
private readonly tenancyContext: TenancyContext,
private reflector: Reflector,
) {}
) { }
/**
* Validate the tenant of the current request is seeded.
@@ -41,6 +41,12 @@ export class EnsureTenantIsSeededGuard implements CanActivate {
}
const tenant = await this.tenancyContext.getTenant();
if (!tenant) {
throw new UnauthorizedException({
message: 'Tenant not found.',
errors: [{ type: 'TENANT.NOT.FOUND' }],
});
}
if (!tenant.seededAt) {
throw new UnauthorizedException({
message: 'Tenant database is not seeded with initial data yet.',

View File

@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
import { ClsService } from 'nestjs-cls';
import { SystemUser } from '../System/models/SystemUser';
import { TenantModel } from '../System/models/TenantModel';
import { ServiceError } from '../Items/ServiceError';
@Injectable()
export class TenancyContext {
@@ -13,7 +14,7 @@ export class TenancyContext {
@Inject(TenantModel.name)
private readonly systemTenantModel: typeof TenantModel,
) {}
) { }
/**
* Get the current tenant.