Open Closed

Module as a Feature #7555


User avatar
0
IbrahimSarigoz created

I have created a tiered main application and added a new module to it using the suite. My goal is to enable or disable this module for specific tenants, and I need to do this at runtime, managed by the admin from the main application. I'm having trouble finding the correct approach to achieve this. I'm torn between using features and permissions. If I use features, do I need to add a RequireFeature attribute at the beginning of each ApplicationService within the module? What is the proper way to handle this?

In my main app i created this :

public class MyFeatureDefinitionProvider : FeatureDefinitionProvider
{
    public override void Define(IFeatureDefinitionContext context)
    {
        var myGroup = context.AddGroup("MyFeatureGroup", "My feature group");
        
        myGroup.AddFeature(
                        "MyModuleFeature",
                        defaultValue: "false",
                        displayName: LocalizableString
                                         .Create<FeaturesDemoResource>("MyModuleFeature"),
                        valueType: new ToggleStringValueType()
                    );

    }
}

In my module i created this :

public class MyModule : AbpModule
{
    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>();
        
        if (!featureChecker.IsEnabledAsync("MyModuleFeature").Result)
        {
            throw new BusinessException("This tenant does not have permission to access this module.");
        }
    }
}
  • ABP Framework version: v8.1.3
  • UI Type: Angular / MVC
  • Database System: EF Core , Oracle
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes tiered

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

    HI,

    you can check ABP how to do it in CMS module

    • https://github.com/abpframework/abp/blob/dev/modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Blogs/BlogAdminAppService.cs#L17
    • https://github.com/abpframework/abp/blob/b9ae57b38bbfe29837079be54bfab31b9949b55a/modules/cms-kit/src/Volo.CmsKit.Admin.Web/Menus/CmsKitAdminMenuContributor.cs#L52
    • https://github.com/abpframework/abp/blob/dev/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Permissions/CmsKitAdminPermissionDefinitionProvider.cs

    do I need to add a RequireFeature attribute at the beginning of each ApplicationService within the module? What is the proper way to handle this?

    Yes, you need.

    In my module i created this : public class MyModule : AbpModule { public override void OnApplicationInitialization(ApplicationInitializationContext context) { var featureChecker = context.ServiceProvider.GetRequiredService();

        if (!featureChecker.IsEnabledAsync("MyModuleFeature").Result)
        {
            throw new BusinessException("This tenant does not have permission to access this module.");
        }
    }
    

    }

    You don't need to this.

  • User Avatar
    0
    IbrahimSarigoz created
    using MerkezYonetim.Localization;
    using MusteriYonetimi.Features;
    using UrunYonetimi.Features;
    using Volo.Abp.Features;
    using Volo.Abp.Localization;
    using Volo.Abp.Validation.StringValues;
    
    namespace MerkezYonetim.Features
    {
        public class MyFeatureDefinitionProvider : FeatureDefinitionProvider
        {
    
            public override void Define(IFeatureDefinitionContext context)
            {
                var urunYonetimiGroup = context.AddGroup("UrunYonetimi", L("UrunYonetimi"));
    
                urunYonetimiGroup.AddFeature(
                    UrunYonetimiFeatures.Enable,
                    defaultValue: "false",
                    L("Enable"),
                    valueType: new ToggleStringValueType());
    
    
                var musteriYonetimiGroup = context.AddGroup("MusteriYonetimi", L("MusteriYonetimi"));
    
                musteriYonetimiGroup.AddFeature(
                    MusteriYonetimiFeatures.Enable,
                    defaultValue: "false",
                    L("Enable"),
                    valueType: new ToggleStringValueType());
            }
    
            private static LocalizableString L(string name)
            {
                return LocalizableString.Create<MerkezYonetimResource>(name);
            }
        }
    }
    

    in one FeatureDefinitionProvider class i couldnt create 2 different group.

    it says :

    when i comment one of them it works fine.

  • User Avatar
    0
    IbrahimSarigoz created

    I found my mistake. 'UrunYonetimiFeatures.Enable' and 'MusteriYonetimiFeatures.Enable' have the same string value, which is causing the error.

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