Open Closed

Change default admin name to the provided USER NAME when creating a new tenant #8116


User avatar
0
abhisheksreesaila created
  • ABP Framework version: v8.2
  • UI Type: Blazor Server
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): Tiered
  • Exception message and full stack trace:
  • Steps to reproduce the issue:
  • I have implemented a custom register logic to create tenant (without any permissions). In other words anonymous user can create tenant. (self registration)
  • Each tenant gets a dedicated database
  • A user with ADMIN role gets created. no issues,
    • The user email -> is the email that was used in the REGISTRATION page
    • The user name is defaulted to ADMIN.
    • After digging some code, it always takes the "admin" as the default user name. I want to the default name to be the name passed from REGISTRATION page.

See the code below for CustomRegisterModel How should i modify to set the DEFAULT NAME as** Input.UserName** `

    public override async Task<IActionResult> OnPostAsync()
    {
        try
        {
            var tenantName = $"{Input.UserName}-dedicated-tenant";
            var defaultConnectionString = _configuration.GetConnectionString("Default");

            if (string.IsNullOrEmpty(defaultConnectionString))
            {
                throw new Exception("Default connection string is not configured in app settings.");
            }

            // Create the database for the new tenant
            var tenantConnectionString = CreateTenantSpecificConnectionString(defaultConnectionString, tenantName);
            //await CreateTenantDatabaseAsync(tenantConnectionString, tenantName);

            SaasTenantDto tenant;
            using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
            {
                var tenantCreateDto = new SaasTenantCreateDto
                {
                    Name = tenantName,
                    AdminEmailAddress = Input.EmailAddress,
                    AdminPassword = Input.Password,
                    ConnectionStrings = new SaasTenantConnectionStringsDto
                    {
                        Default = tenantConnectionString
                    }
                };

                tenant = await _tenantAppService.CreateAsync(tenantCreateDto);

                // Migrate and seed the tenant database
                await MigrateAndSeedTenantDatabaseAsync(tenant.Id);

                await uow.CompleteAsync();
            }
            
                return RedirectToPage("/Login");  // or wherever you want to redirect after successful registration
            
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An error occurred during registration");
            
       
            
            // Redirect to the login page
            return RedirectToPage("./Login");
        }
    }

    private string CreateTenantSpecificConnectionString(string defaultConnectionString, string tenantName)
    {
        var builder = new NpgsqlConnectionStringBuilder(defaultConnectionString);
        builder.Database = tenantName;
        return builder.ConnectionString;
    }
    
    private async Task MigrateAndSeedTenantDatabaseAsync(Guid tenantId)
    {
        using (_currentTenant.Change(tenantId))
        {
            await _dbSchemaMigrator.MigrateAsync();
        }
    }
}

`


5 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    you can try

     private async Task MigrateAndSeedTenantDatabaseAsync(Guid tenantId, string userName)
    {
        using (_currentTenant.Change(tenantId))
        {
            await _dbSchemaMigrator.MigrateAsync();
            
            await _dataSeeder.SeedAsync(new DataSeedContext(tenant?.Id)
                .WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName,
                    userName)
                .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName,
                    MyProjectNameConsts.AdminPasswordDefaultValue)
            );
        }
    }
    
  • User Avatar
    0
    abhisheksreesaila created

    I changed the above code slightly since the email property cannot take USER NAME.

    private async Task MigrateAndSeedTenantDatabaseAsync(Guid tenantId, string userName, string password)
            {
                using (_currentTenant.Change(tenantId))
                {
                    await _dbSchemaMigrator.MigrateAsync();
                       await _dataSeeder.SeedAsync(new DataSeedContext(tenantId)
                        .WithProperty("AdminUserName",
                            userName)
                        .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName,
                            password)
                    );
                }
            }
    

    it made it worse. It defaulted everything.

  • User Avatar
    0
    abhisheksreesaila created

    actually it looks like ABP 8.3 solves the problem. just saw the migration guide. Let me upgrade and report back

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    okay

  • User Avatar
    0
    abhisheksreesaila created

    to close the loop on this, Release 8.3 fixed the issue.

Made with ❤️ on ABP v9.1.0-preview. Updated on October 22, 2024, 09:35