Open Closed

Seed tenant database after being created using override of CreateAsync #217


User avatar
0
edirkzwager created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

  • ABP Framework version: v2.8.0
  • UI type: ~~Angular~~ / MVC
  • Tiered (MVC) or Identity Server Seperated (Angular): yes / ~~no~~
  • Exception message and stack trace: n.a.
  • Steps to reproduce the issue: n.a.
  • After succesfully creating the tenant database using an override of the function CreateAsync, we now want to seed the new database with new permissions. I thought of using a Seeder but the docs on the Seeder subject contain : 'ToDo'

Could you explain to me hwo I should add the permissons we have defined in the PermissionDefinitionProvider in the Application.Contracts ? The code we use now to create the tenant database looks like this :

[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(ITenantAppService), typeof(KantanTenantAppService))]
public class KantanTenantAppService : TenantAppService
{
	private readonly ITenantRepository _tenantRepository;
	private readonly IEditionRepository _editionRepository;
	private readonly ITenantManager _tenantManager;
	private readonly IDataSeeder _dataSeeder;
	private readonly ICurrentTenant _currentTenant;
	private readonly IConfiguration _config;
	public KantanTenantAppService(ITenantRepository tenantRepository,
																IEditionRepository editionRepository,
																ITenantManager tenantManager,
																IDataSeeder dataSeeder,
																ICurrentTenant currentTenant,
																IConfiguration config) : base(
																tenantRepository,
																editionRepository,
																tenantManager,
																dataSeeder)
	{
		_tenantRepository = tenantRepository;
		_editionRepository = editionRepository;
		_tenantManager = tenantManager;
		_dataSeeder = dataSeeder;
		_currentTenant = currentTenant;
		_config = config;
	}
	[Authorize("Saas.Tenants.Create")]
	public override async Task<SaasTenantDto> CreateAsync(SaasTenantCreateDto input)
	{
		SaasTenantDto result;
		string configConnectionString = _config.GetConnectionString("NewTenantConnectionString");
		string newConnectionString = string.Empty;
		Tenant tenant;
		string dbPrefix = _config.GetSection(KantanConsts.TenantConfigSection)["Default"];

		result = await base.CreateAsync(input);
		tenant = _tenantRepository.FindByName(input.Name);
		if (tenant != null)
		{
			newConnectionString = 
				string.Format(configConnectionString
											,dbPrefix
											,tenant.Id.ToString().Replace("-","")
											);
			//Create default connection string for this new tenant
			await base.UpdateDefaultConnectionStringAsync(tenant.Id, newConnectionString);

			using (_currentTenant.Change(tenant.Id))
			{ 
				await ServiceProvider.
					GetRequiredService<KantanDbMigrationService>()
					.MigrateTenantDatabasesAsync(tenant);

				// Add the additional Permissions
				//addpermissions

				//Add other data
				//DoSomething
				
			}
		}
		return result;
	}
}

The definition provider for the permissions looks like this (in the application.contracts)

public class KantanPermissionDefinitionProvider : PermissionDefinitionProvider { public override void Define(IPermissionDefinitionContext context) { PermissionGroupDefinition KantanGroup = context.AddGroup(KantanPermissions.GroupName); PermissionDefinition pd;

  KantanGroup.AddPermission(KantanPermissions.Dashboard.Host, L("Permission:Dashboard"), MultiTenancySides.Host);
  KantanGroup.AddPermission(KantanPermissions.Dashboard.Tenant, L("Permission:Dashboard"), MultiTenancySides.Tenant);
  pd = KantanGroup.AddPermission(KantanPermissions.FinancialStatement.FinancialStatementGroup, L("Permission:FinancialStatementGroupName"), MultiTenancySides.Host);
  pd.AddChild(KantanPermissions.FinancialStatement.FinancialStatement_Add, L("Permission:Add"), MultiTenancySides.Host);
  pd.AddChild(KantanPermissions.FinancialStatement.FinancialStatement_Delete, L("Permission:Delete"), MultiTenancySides.Host);
  pd.AddChild(KantanPermissions.FinancialStatement.FinancialStatement_Edit, L("Permission:Edit"), MultiTenancySides.Host);
  pd.AddChild(KantanPermissions.FinancialStatement.FinancialStatement_Read, L("Permission:Read"), MultiTenancySides.Host);

}

private static LocalizableString L(string name)
{
  return LocalizableString.Create<KantanResource>(name);
}

}

Basically we have two questions : 1 : How to add the permissions ? 2 : How to add other data to the tenant database ?

Do we need to use a seeder ? Thanks for your help.


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

    Hi,

    Try :

    using (_currentTenant.Change(tenant.Id))
    { 
        await ServiceProvider.
            GetRequiredService<KantanDbMigrationService>()
            .MigrateTenantDatabasesAsync(tenant);
    
       // init seed data
       await _dataSeeder.SeedAsync(tenant.Id);
    
    }
    

    You can custom DataSeedContributor to add more seed data, Like this:

    public class CustomDataSeedContributor : IDataSeedContributor, ITransientDependency
    {   
            private readonly IIdentityUserRepository _identityUserRepository;
            
            public CustomDataSeedContributor(IIdentityUserRepository identityUserRepository)
            {
                _identityUserRepository = identityUserRepository;
            }
    
            [UnitOfWork]
            public virtual async Task SeedAsync(DataSeedContext context)
            {
                _identityUserRepository.InsertAsync(...);
            }
    }
    
  • User Avatar
    0
    edirkzwager created

    Hi liangshiwei,

    Your first suggestion does not give the desired result. The SeedAsync is already called in the MigrateTenantDatabaseAsync. I think I need to take the second step and create a custom seeder.

    But is there a way to use the KantanPermissionDefinitionProvider defined in the Application.Contracts ?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Permission definitions are not persisted to the database. You can use DataSeedContributor add anything seed data.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 26, 2024, 06:07