Starts in:
1 DAY
16 HRS
31 MIN
26 SEC
Starts in:
1 D
16 H
31 M
26 S
Open Closed

FeatureChecker is not working when changing Tenant by code #1201


User avatar
0
Leonardo.Willrich created
  • ABP Framework version: v4.3.0-rc.2
  • UI type: Blazor
  • DB provider: EF Core

Hi,

I have a backgroundJob method where I need to check if the Tenant Edition Feature is checked or not. The method does a loop in a table disabling IMultiTenant filter. And then, for each record, it sets the CurrentTenant using the method Change().

The problem is that the Features are not returning properly. For example, if one Editon has the feature Trial checked, it is returning false anyway.

I have tried to create a new instance for FeatureChecker after changing the tenant, but it is still not working.

What should I do to be able to change the tenant and get the features properly?

If I am logged as Tenant and check that in a Blazor page, that works fine. It means that my settings are right.

Another question, it seems that ServiceProvider is obsolete now and it suggests using LazyServiceProvider instead. But, how can I create an instance and release/dispose it properly? It doesn't have CreateScope() method.

I have researched in the documentation, but no success so far for my questions.

Bellow my code for reference:

public async Task RunNotificationCheck()
        {
            List<SupplyNetwork> networks;
            using (_dataFilter.Disable<IMultiTenant>())
            {
                networks = _supplyNetworkRepository.Where(x => x.Active).ToList();
            }
            foreach (var network in networks)
            {
                await CheckSupplyNetworkNotifications(network);
            }
        }

        private async Task CheckSupplyNetworkNotifications(SupplyNetwork supplyNetwork)
        {
            using (CurrentTenant.Change(supplyNetwork.TenantId))
            {
                using (var scope = ServiceProvider.CreateScope())
                { 
                    _featureChecker = ServiceProvider.GetRequiredService<IFeatureChecker>();
                    var trial = await _featureChecker.IsEnabledAsync(AvalancheOCPFeatures.Trial);
                    if (!trial)
                    {
                        var txtAlertEnabled = !await _featureChecker.IsEnabledAsync(AvalancheOCPFeatures.TXTAlerts);
                        var emailAlertEnabled = !await _featureChecker.IsEnabledAsync(AvalancheOCPFeatures.EmailAlerts);
                        var desktopAlertEnabled = !await _featureChecker.IsEnabledAsync(AvalancheOCPFeatures.DesktopAlerts);

                        if (supplyNetwork.EnableEmailNotification && emailAlertEnabled)
                        {
                            await CheckTenantEmailNotifications(supplyNetwork);
                        }

                        if (supplyNetwork.EnableTXTNotification && txtAlertEnabled)
                        {
                            await CheckTenantTXTNotifications(supplyNetwork);
                        }

                        if (supplyNetwork.EnableAlertNotification && desktopAlertEnabled)
                        {
                            await CheckTenantAlertNotifications(supplyNetwork);
                        }
                    }
                }
            }
        }

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

    if one Editon has the feature Trial checked, it is returning false anyway.

    Can you confirm the EditionId is exists?

    
    using (CurrentPrincipalAccessor.Change(new Claim(AbpClaimTypes.EditionId, currentTenant.EditionId)))
    {
       
    }
    
  • User Avatar
    0
    Leonardo.Willrich created

    Hi @maliming,

    Sorry, your answer it not 100% clear for me. I don't have the Volo.Abp.Features source code in my project, only the package reference for it. I haven't found that peace of code as your image. Where exactly do you want me to check if there is EditionId assigned or not? Besides, the instance for CurrentTenant doesn't have the field EditionId.

  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi

    You can inject the ICurrentPrincipalAccessor service. then check its claims. PrincipalAccessor.Principal?.FindEditionId() If there is no EditionId claims, you shuold CurrentPrincipalAccessor.Change to set is as tenant's EditionId.

    using (CurrentPrincipalAccessor.Change(new Claim(AbpClaimTypes.EditionId, currentTenant.EditionId)))
    {
       
    }
    
  • User Avatar
    0
    Leonardo.Willrich created

    That worked, thank you! Do you have the reference for the documentation of that? PrincipalAncessor and Claims?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    These components are not commonly used, and there is no documentation yet.

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