fix: prefer primary Tenant.domain in get_user/get_version matching
When multiple tenants claim the same host via domains aliases, resolve the tenant whose primary domain matches first so branding edits apply to the expected version. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,7 +17,7 @@ export class GetUserService {
|
||||
where: { isActive: true },
|
||||
});
|
||||
|
||||
const tenant = tenants.find((item) => this.matchesHost(item, host));
|
||||
const tenant = this.findTenantForHost(tenants, host);
|
||||
if (!tenant) {
|
||||
throw new NotFoundException('tenant not found for domain');
|
||||
}
|
||||
@@ -181,6 +181,46 @@ export class GetUserService {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* اول دامنهٔ اصلی (Tenant.domain)، بعد aliases داخل domains[].
|
||||
* اگر چند نسخه یک دامنه را داشته باشند، نسخهٔ primary برنده است.
|
||||
*/
|
||||
private findTenantForHost<
|
||||
T extends {
|
||||
domain: string;
|
||||
domains: unknown;
|
||||
},
|
||||
>(tenants: T[], host: string): T | undefined {
|
||||
const normalizedHost = this.normalizeHost(host);
|
||||
const hostNoWww = normalizedHost.replace(/^www\./, '');
|
||||
|
||||
const primary = tenants.find((item) => {
|
||||
const d = this.normalizeHost(item.domain);
|
||||
return d === normalizedHost || d.replace(/^www\./, '') === hostNoWww;
|
||||
});
|
||||
if (primary) return primary;
|
||||
|
||||
return tenants.find((item) => this.matchesAliasHost(item, normalizedHost));
|
||||
}
|
||||
|
||||
private matchesAliasHost(
|
||||
tenant: {
|
||||
domain: string;
|
||||
domains: unknown;
|
||||
},
|
||||
host: string,
|
||||
) {
|
||||
const hostNoWww = host.replace(/^www\./, '');
|
||||
for (const value of this.readDomains(tenant.domains)) {
|
||||
const normalized = this.normalizeHost(value);
|
||||
if (!normalized) continue;
|
||||
if (normalized === host || normalized.replace(/^www\./, '') === hostNoWww) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private matchesHost(
|
||||
tenant: {
|
||||
domain: string;
|
||||
@@ -188,15 +228,7 @@ export class GetUserService {
|
||||
},
|
||||
host: string,
|
||||
) {
|
||||
const candidates = new Set<string>();
|
||||
for (const value of [tenant.domain, ...this.readDomains(tenant.domains)]) {
|
||||
const normalized = this.normalizeHost(value);
|
||||
if (!normalized) continue;
|
||||
candidates.add(normalized);
|
||||
candidates.add(normalized.replace(/^www\./, ''));
|
||||
candidates.add(`www.${normalized.replace(/^www\./, '')}`);
|
||||
}
|
||||
return candidates.has(host) || candidates.has(host.replace(/^www\./, ''));
|
||||
return Boolean(this.findTenantForHost([tenant], host));
|
||||
}
|
||||
|
||||
private readDomains(raw: unknown): string[] {
|
||||
|
||||
Reference in New Issue
Block a user