Open Closed

RequireFeature dit not work. #2774


User avatar
0
zhongfang created
  • ABP Framework version: v5.1.4
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: The tenant without the feature will see the menu item.
  • Steps to reproduce the issue:" I have successfully create the feature definition. And the value of the feature is false. I want that the tenant without the feature can not see the menuitem...

    public class RmsMenuContributor : IMenuContributor
    {

        public RmsMenuContributor()
        {
        }

        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            bool isGranted = await context.AuthorizationService.IsGrantedAsync(Yee.Change.Common.Permissions.AdminPermissions.Admin.Default);

            if (!isGranted)
            {
                return;
            }

            this._L = context.GetLocalizer<RmsResource>();

            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }

            await SetAiRulesMenu(context);

            await SetDic(context);

            await SetZipingBazi(context);

            await SetLianshanMenus(context);

            await SetLiuyaoMenus(context);

            await SetKanyuMenus(context);

            await SetBasicMenu(context);

            await SetTestResultMenu(context);

            await SetOperationMenu(context);

            await SetProductsMenu(context);
        }

        [RequiresFeature(Yee.Change.ZipingBazi.Features.ZipingBaziFeature.IsEnabled)]
        public virtual async Task SetOperationMenu(MenuConfigurationContext context)
        {
            var operateMenu = AddOperateMenuItem(context);

            AddMenuItemPictures(context, operateMenu);

            AddMenuItemUploadPicture(context, operateMenu);

5 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi @zhongfang

    As I see you decorated your method with RequiresFeature attribute you called it directly in code. There is no interceptor is executed because you made a regular method call.

    That attribute works on AppServices or Controllers well, it won't work your manual method calls.

    Try injecting IFeatureChecker and check the feature with it.

    public class RmsMenuContributor : IMenuContributor
        {
            private readonly IFeatureChecker _featureChecker;
            public RmsMenuContributor(IFeatureChecker featureChecker)
            {
                _featureChecker = featureChecker;
            }
    
            public async Task ConfigureMenuAsync(MenuConfigurationContext context)
            {
    
                // ...
                
                if (await _featureChecker.IsEnabledAsync(Yee.Change.ZipingBazi.Features.ZipingBaziFeature.IsEnabled))
                {
                    await SetOperationMenu(context);
                }
    
                await SetProductsMenu(context);
            }
    
            [RequiresFeature(Yee.Change.ZipingBazi.Features.ZipingBaziFeature.IsEnabled)]
            public virtual async Task SetOperationMenu(MenuConfigurationContext context)
            {
                var operateMenu = AddOperateMenuItem(context);
    
                AddMenuItemPictures(context, operateMenu);
    
                AddMenuItemUploadPicture(context, operateMenu);
    
    
  • User Avatar
    0
    zhongfang created

    OK. I see. I will try later.

    another question, I have define features. But after I saved for tenant version. I can not change the value for a single tenant.

  • User Avatar
    0
    zhongfang created

    Now, I can control the menuitem with IFetureChecker. But another question ... can not change the feature value for a single tenant ater save the value for version.

  • User Avatar
    0
    zhongfang created

    My question can be change to:

    If I save feature value for tenant version. Then I can't change the feature value for a single tenant?

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Feature system was originally designed to control the tenant features in a multi-tenant application. However, it is extensible and capable of determining the features by any condition.

    You can see the documentation from here: https://docs.abp.io/en/abp/latest/Features it's already designed for multi-tenant applications

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19