Open Closed

Tenant Name matching rules in Domain Tenant Resolver #2638


User avatar
0
dboutwell created
  • ABP Framework version: v5.1.3
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: N/A
  • Steps to reproduce the issue:"

Not a bug, per se, but I was wondering what would be the easiest way to extend the Domain Tenant Resolver functionality with custom string rules for matching on the tenant name side?

For example, I have a tenant with the name "No Rules" in the database, and I want to match on "norules.localhost", but the system keeps kicking back that it can't find a tenant with that name.

So, what I am looking to do is one of two things (whichever is easiest):

  • tell the system to match on the name string with all of the whitespace removed
  • update the "new tenant" form to not allow whitespace characters in the name field

Let me know if you need any code from my side. Thus far the project is very close to the commercial startup template.


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can custom the DomainTenantResolveContributor.

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#custom-tenant-resolvers

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/DomainTenantResolveContributor.cs

  • User Avatar
    0
    dboutwell created

    hi

    You can custom the DomainTenantResolveContributor.

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#custom-tenant-resolvers

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/DomainTenantResolveContributor.cs

    That is where I started, and the domain tenant resolver does successfully match the domain value (in this case norules) from the URL but I am then pushed to a screen that simply states:

    Tenant not found!

    There is no tenant with the tenant id or name: norules

    So what I am looking for is to modify whatever routine looks at the SaasTenants table AFTER the domain resolver pulls the deal and tell it to match on something like:

    Regex.Replace(Tenant.Name, @"\s", "");

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can override the TenantManager class,

  • User Avatar
    0
    dboutwell created

    Not sure if this will be made public, but the solution I landed on was overriding the FindByNameAsync method in the EfCoreTenantRepository class so that the tenant creation process and the the domain resolver will both be operating out of the same playbook.

    It's probably not the most efficient code, but this is what that override class looks like now:

    [Dependency(ReplaceServices = true)]
        public class CustomTenantRepository : EfCoreTenantRepository
        {
            public CustomTenantRepository(IDbContextProvider<ISaasDbContext> dbContextProvider)
                : base(dbContextProvider)
            {
    
            }
    
            public override async Task<Tenant> FindByNameAsync(string name, bool includeDetails = true, CancellationToken cancellationToken = default)
            {
                var tenants = await (await GetDbSetAsync())
                    .IncludeDetails(includeDetails)
                    .OrderBy(t => t.Id).ToListAsync();
    
    
                return tenants
                    .FirstOrDefault(t => (t.Name == name || TestUrlNamingConvention(t.Name,name)));
            }
    
            private bool TestUrlNamingConvention(string tenantName, string inputName)
            {
                var i = string.Concat(inputName.Where(c => !char.IsWhiteSpace(c))).ToLowerInvariant();
                var t = string.Concat(tenantName.Where(c => !char.IsWhiteSpace(c))).ToLowerInvariant();
    
                return i == t;              
            }
        }
    

    And because I often find myself asking where I should put these, I have mine in the Project.EntityFrameworkCore project.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11