Open Closed

Dataseeding a New Tenant - How to get Edition/Feature #5821


User avatar
0
SuperBeastX3 created
  • ABP Framework version: v7.3.3
  • UI Type: MVC
  • Database System: EF Core
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

When I seed data for a new tenant, how can I get the edition? I tried using a feature of the edition with the feature checker, but that value is only set to the default value and isn't pulling in the correct editions feature. What is the proper way when data seeding to do this?

In my data seeder I am doing this: var featureName = await _featureChecker.GetOrNullAsync("FeatureName");

But it is always just the default value.

Thanks!


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

    hi

    Please share more code. Thanks

  • User Avatar
    0
    SuperBeastX3 created

    First, I created a new feature using this:

    var myGroup = context.AddGroup("Version");
    
    
    myGroup.AddFeature(
        "Version",
        defaultValue: "V1",
        displayName: LocalizableString
        .Create<MaestroCoreResource>("My.Version"),
        valueType: new SelectionStringValueType
        {
            ItemSource = new StaticSelectionStringValueItemSource(
               new LocalizableSelectionStringValueItem
               {
                   Value = "V1",
                   DisplayText = new LocalizableStringInfo("MyResource", "My.Version.V1")
               },
               new LocalizableSelectionStringValueItem
               {
                   Value = "V2",
                   DisplayText = new LocalizableStringInfo("MyResource", "My.Version.V2")
               }),
            Validator = new AlwaysValidValueValidator()
        }
    );
    

    Then, when I create a Tenant, I am choosing "V2" for my Version feature.

    When it seeds the data, I want to do specific seeding based on that version:

    var version = await _featureChecker.GetOrNullAsync("Version");
    
    if(version == "V2") 
    {
        //do something for V2
    
    }
    else
    {
        //do something for V1
    }
    

    I would expect the code of: await _featureChecker.GetOrNullAsync("Version"); Would return the correct version, but no matter what it always returns the default value set in the feature.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Does CurrentTenant set the correct tenant?

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#change-the-current-tenant

  • User Avatar
    0
    SuperBeastX3 created

    Im not sure I understand what you are asking. The tenant id that gets passed into the data seeding is the correct Id, and all the seeded items have the correct tenant id. I do not use currenttenant in the data seeding.

    Can you be more specific as to what you are asking?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Ok, Can you share the full code so I can understand your case? Thanks

  • User Avatar
    0
    SuperBeastX3 created
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Principal;
    using System.Text;
    using System.Threading.Tasks;
    using Volo.Abp.Authorization.Permissions;
    using Volo.Abp.Data;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.Features;
    using Volo.Abp.Identity;
    using Volo.Abp.MultiTenancy;
    using Volo.Abp.PermissionManagement;
    using Volo.Abp.Security.Claims;
    using Volo.Saas.Tenants;
    
    namespace MyCore.DataSeedContributors
    {
        public class AddAbpRoleDataSeedContributor : IDataSeedContributor, ITransientDependency
        {
            private readonly IdentityUserManager _identityUserManager;
            private readonly IdentityRoleManager _identityRoleManager;
            private readonly IPermissionDataSeeder _permissionDataSeeder;
            private readonly IFeatureChecker _featureChecker;
            private readonly IPermissionDefinitionManager _permissionDefinitionManager;
    
    
            public AddAbpRoleDataSeedContributor(IdentityUserManager identityUserManager, IdentityRoleManager identityRoleManager, IPermissionDataSeeder permissionDataSeeder, IFeatureChecker featureChecker, IPermissionDefinitionManager permissionDefinitionManager)
            {
                _identityUserManager = identityUserManager;
                _identityRoleManager = identityRoleManager;
                _permissionDataSeeder = permissionDataSeeder;
                _featureChecker = featureChecker;
                _permissionDefinitionManager = permissionDefinitionManager;
            }
    
            public async Task SeedAsync(DataSeedContext context)
            {
                var version = await _featureChecker.GetOrNullAsync("Version");
                
                if(version == "V2") 
                {
                    //dosomething for V2 
    
                }
                else
                {
                    //dosomthing for V1
                }
            }
        }
    }
    

    In addition to this, I really want to know what Edition the tenant I just created is using, but I can't seem to find a way to it.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to get a feature value of a tenant by GetOrNullForTenantAsync method of IFeatureManager.

    https://github.com/abpframework/abp/blob/dev/modules/feature-management/src/Volo.Abp.FeatureManagement.Domain/Volo/Abp/FeatureManagement/TenantFeatureManagerExtensions.cs#L11

  • User Avatar
    0
    SuperBeastX3 created

    Same result, just returns the default value of the feature.

  • User Avatar
    0
    SuperBeastX3 created

    Maybe part of the problem is the tenant record doesnt exist in the database yet when the dataseeder runs?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    public class AddAbpRoleDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IdentityUserManager _identityUserManager;
        private readonly IdentityRoleManager _identityRoleManager;
        private readonly IPermissionDataSeeder _permissionDataSeeder;
        private readonly IFeatureChecker _featureChecker;
        private readonly IFeatureManager _featureManager;
        private readonly IPermissionDefinitionManager _permissionDefinitionManager;
        private readonly ICurrentPrincipalAccessor _currentPrincipalAccessor;
        private readonly ITenantRepository _tenantRepository;
    
        public AddAbpRoleDataSeedContributor(IdentityUserManager identityUserManager, IdentityRoleManager identityRoleManager, IPermissionDataSeeder permissionDataSeeder, IFeatureChecker featureChecker, IPermissionDefinitionManager permissionDefinitionManager, IFeatureManager featureManager, ICurrentPrincipalAccessor currentPrincipalAccessor, ITenantRepository tenantRepository)
        {
            _identityUserManager = identityUserManager;
            _identityRoleManager = identityRoleManager;
            _permissionDataSeeder = permissionDataSeeder;
            _featureChecker = featureChecker;
            _permissionDefinitionManager = permissionDefinitionManager;
            _featureManager = featureManager;
            _currentPrincipalAccessor = currentPrincipalAccessor;
            _tenantRepository = tenantRepository;
        }
    
        public async Task SeedAsync(DataSeedContext context)
        {
            if (context.TenantId.HasValue)
            {
                var tenant = await _tenantRepository.FindByIdAsync(context.TenantId.Value);
                var myPrincipal = _currentPrincipalAccessor.Principal.Clone();
                myPrincipal.Identities.First().AddClaim(new Claim(AbpClaimTypes.TenantId, tenant.Id.ToString()));
                if (tenant.EditionId.HasValue)
                {
                    myPrincipal.Identities.First().AddClaim(new Claim(AbpClaimTypes.EditionId, tenant.EditionId.ToString()));
                }
                using (_currentPrincipalAccessor.Change(_currentPrincipalAccessor.Principal))
                {
                    var version = await _featureChecker.GetOrNullAsync(IdentityProFeature.EnableLdapLogin);
                    var version2 = await _featureManager.GetOrNullForTenantAsync(IdentityProFeature.EnableLdapLogin, context.TenantId.Value);
    
                    if(version == "V2")
                    {
                        //dosomething for V2
    
                    }
                    else
                    {
                        //dosomthing for V1
                    }
                }
            }
    
        }
    }
    
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I will add code to the abp framework to fix this case.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    featureManager.GetOrNullForTenantAsync will works.

  • User Avatar
    0
    SuperBeastX3 created

    I took a different approach and modified the MigrateAndSeedForTenantAsync method. Using: var version = await _featureManager.GetOrNullForTenantAsync("Version", tenantId); seems to have the correct value, so I added a property: ` // Seed data using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true)) { await _dataSeeder.SeedAsync( new DataSeedContext(tenantId) .WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, adminEmail) .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, adminPassword) .WithProperty("Version", version) );

     await uow.CompleteAsync();
    

    } `

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    It looks fine.

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