Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Problem creating Parent and Child nodes in Permissions #3827


User avatar
0
bilalhaidar created

Hello,

In the AppServiceModule project I am adding 2 AuthorizationProviders. The first creates a root node under "Pages" called "Pages.Common" and the second create new permissions under "Pages.Common". The code is as follows:

Configuration.Authorization.Providers.Add<SharedAuthorizationProvider>();
Configuration.Authorization.Providers.Add<AgencyAuthorizationProvider>();

In the SharedAuthorizationProvider:

public static class SharedPermissions
    {
        public const string Pages = "Pages";

        public const string Common= "Pages.Common";
    }

public class SharedAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        public SharedAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public SharedAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var pages = context.GetPermissionOrNull(SharedPermissions.Pages);
            pages.CreateChildPermission(SharedPermissions.Common, L("Common.Permission"));
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, DrcAppConsts.LocalizationSourceName);
        }
    }

In the AgenciesAuthorizationProvider:

public static class AgencyPermissions
    {
        public const string Common = "Pages.Common";

        public const string Agency = "Agency";
...

   public class AgencyAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        public AgencyAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public AgencyAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
           **var common = context.GetPermissionOrNull(AgencyPermissions.Common);**

            var agency = common.CreateChildPermission(AgencyPermissions.Agency, L("Agency.Permission"));

In the line of code above that is bold: "common" is being null.

Is there something wrong in the code written or the library doesn't allow creating parent and child on the fly?

Thanks


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I thin you should first get permission with name Pages in AgencyAuthorizationProvider, then get Pages.Common from it's children. Can you try it like that ?

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Thank you Ismail for your quick response. That was it, and here is the code I used and it works in case someone needs it:

    var pages = context.GetPermissionOrNull(AgencyPermissions.Pages);
                var common = pages.Children.Where( permission => permission.Name.Equals(AgencyPermissions.Common, System.StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                
                if (common == null)
                {
                    return;
                }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @bilalhaidar :).