Base solution for your next web application
Open Closed

Check feature by permission #1813


User avatar
0
andmattia created

I try to check if a user has one permission depending on a feature. I try to explain;
feature
-- UseApplication1 -> featureDependency: new SimpleFeatureDependency("UseApplication1")
-- UseApplication2 -> featureDependency: new SimpleFeatureDependency("UseApplication2")

Permission all permission depend on
-- Document -> featureDependency: new SimpleFeatureDependency("UseApplication2")
---| Add -> featureDependency: new SimpleFeatureDependency("UseApplication2")
---| Update -> featureDependency: new SimpleFeatureDependency("UseApplication2")
...
-- Application-> featureDependency: new SimpleFeatureDependency("UseApplication1")
---| Add -> featureDependency: new SimpleFeatureDependency("UseApplication1")
---| Update -> featureDependency: new SimpleFeatureDependency("UseApplication1")

1 user
-- Has permission Document,Add,Update
2 user
-- has no permission

If i get all feature and I check if is enable in tenant
if(await FeatureChecker.IsEnabledAsync(feature.Name))

now I need to check if user has one(or more) permission enable depending on this feature
...

var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
var userPermission = await UserManager.GetGrantedPermissionsAsync(user);
userPermission.Where(t=>t.FeatureDependency())

... But How can I check if exists 1 (or more) check permission depending on my feature
mattia


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I suspect that you are using it a bit wrong :)

    Say that you have a "PermissionX" depends on "FeatureA". You should not have such a code

    if(tenant has FeatureA)
    {
    if(user has PermissionX)
    {
    //...do it
    }
    }

    Since PermissionX depends on FeatureA, this will be enough:

    if(user has PermissionX)
    {
    //...do it
    }

    So, most of times, no need to explictly check features.

    I haven't understood why you need to check "if exists 1 (or more) check permission depending on my feature", but to do it, use such a where condition:

    userPermission.Where(t=>
    (t.FeatureDependency is SimpleFeatureDependency) &&
    t.FeatureDependency.As<SimpleFeatureDependency>().Features.Contains("MyFeatureName")
    );

    If that's not you want, please explain your real case for a better answer.