@hz-9/a5-tenant
Version:
Tenant module for the @hz-9/a5-* series of repositories.
86 lines • 3.8 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.A5TenantGuard = void 0;
const common_1 = require("@nestjs/common");
const const_1 = require("../const");
const module_definition_1 = require("./module-definition");
const optionsWithDefaults = (options) => ({
emptyToDefault: true,
defaultTenantId: '-',
...options,
});
/**
* 租户检查 Guard
*
* 从 headers 或 route params 中获取租户 ID,并检查租户是否存在
*
* @public
*/
let A5TenantGuard = class A5TenantGuard {
constructor(options, tenantService) {
this.tenantService = tenantService;
this.options = optionsWithDefaults(options);
}
async canActivate(context) {
const request = context.switchToHttp().getRequest();
// TODO 后续可以基于 metadata 信息,对解析方案进行自定义优化
return this.parseFromHeaders(request);
}
async parseFromHeaders(request) {
// 尝试从 headers 中获取租户 ID
let tenantId = this.getTenantIdFromHeaders(request);
if (!tenantId) {
if (this.options.emptyToDefault) {
tenantId = this.options.defaultTenantId;
const tenant = this.tenantService.getDefaultTenant(tenantId);
// 将租户信息存储到 request 对象中供后续使用
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const requestWithTenant = request;
requestWithTenant.tenantId = tenantId;
requestWithTenant.tenantContext = tenant;
return true;
}
throw new common_1.UnauthorizedException('No tenant ID found');
}
// 检查租户是否存在和有效
const tenant = await this.tenantService.getTenant(tenantId);
if (!tenant) {
throw new common_1.ForbiddenException('tenant ID is not valid');
}
// 将租户信息存储到 request 对象中供后续使用
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const requestWithTenant = request;
requestWithTenant.tenantId = tenantId;
requestWithTenant.tenantContext = tenant;
return true;
}
/**
* 从 headers 中获取租户 ID
*
* @param request - FastifyRequest 对象
* @returns 租户 ID 或 undefined
*/
getTenantIdFromHeaders(request) {
return request.headers[const_1.A5_TENANT_ID_HEADER];
}
};
exports.A5TenantGuard = A5TenantGuard;
exports.A5TenantGuard = A5TenantGuard = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(module_definition_1.A5_TENANT_MODULE_OPTIONS)),
__param(1, (0, common_1.Inject)(const_1.A5_TENANT_SERVICE_TOKEN)),
__metadata("design:paramtypes", [Object, Object])
], A5TenantGuard);
//# sourceMappingURL=a5-tenant.guard.js.map