Base solution for your next web application
Open Closed

Use a variable with IsGranted(AppPermissions. #5082


User avatar
0
strategy9 created

In a View I have a foreach loop. I have the string name of a set of AppPermissions in a table. I want to iterate through the data (to fill a select list) by evaluating permissions.

How can I use a variable in the IsGranted statement like:

if (IsGranted(AppPermissions.variablename))...

I've tried a bunch of different options using razor formatting but haven't figured it out yet. Is it possible?


3 Answer(s)
  • User Avatar
    0
    hitaspdotnet created

    Hi,

    I think isn't possible for dynamical items. If you have static items list then you can use hard codes in your razor

    <select class="form-control" name="IsApprovedFilter" id="IsApprovedFilterId">
                               @if (IsGranted(Pages_Blogs_Comments))
                                {
                                          <option value="-1">@L("All")</option>
                                          <option value="1">@L("True")</option>
                                }
                                    <option value="0">@L("False")</option>
                                </select>
    
  • User Avatar
    0
    hitaspdotnet created

    OR You can add new string array property to your entity

    string[] RequiredPermissions {get; set;}
    

    Usage:

    @if (IsGranted(AppPermissions.Pages_SomePermission))
     {
        @foreach (var object in Model.YourObject.Where(e =>  e.RequiredPermissions.Contains("yourSomePermission")))
        {
            @Html.Partial("_ObjectPartialView", object)
        }
    }
        @foreach (var object in Model.YourObject.Where(e =>  e.RequiredPermissions.Contains("yourOtherRequiredPermission")))
        {
            @Html.Partial("_ObjectPartialView", object)
        }
    
  • User Avatar
    0
    alper created
    Support Team

    hi,

    you can filter the data in your controller/application service then give it to your view. and as @CsBeginner says you can use

    @IsGranted("myPermission")