Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • BillyTheKid 3 posts 105 karma points
    Aug 11, 2015 @ 13:53
    BillyTheKid
    0

    Settings permission on Content with EntityPermissionSet

    Im trying to set Permissions on Content from code, but I can't find where this is documented and Bing doesn't return anything usefull either.

    What I eventually want to do is: Set the public access option of the Content to Role Based Set permissions on a Content item based on the logged in member's member group(s)

    What I found so far: ContentService.ReplaceContentPermissions() This method takes an EntityPermissionSet

    An EntityPermissionSet consists of:

    int enitityId // is this the Id of the Content i would like to set permissions on?
    IEnumerable<EntityPermissionSet.UserPermission> // I have no idea what this should contain
    

    Some info about this would really be appreciated, since the documentation lacks on this subject.

    Also another question: If, in the razor template, I do a for example: rootnode.Children this returns all published children. However I only want the Children which the current logged in Member has permissions for. Is this possible with the built in security/permission features in Umbraco?

  • Rahul Sekhar 9 posts 85 karma points
    Mar 22, 2017 @ 14:34
    Rahul Sekhar
    1

    Very late, but I've been working this out for myself and couldn't find any resources, so maybe this will help someone else.

    Yes, the entityId is the ID of the content you'd like to set permissions for. The second parameter is a list of user-permissions combinations. Each item in this list contains a user ID and a permissions string.

    Here's an example of setting permissions:

    int totalUsers;
    // Find all editor users (assuming we don't have more than a 100 users here)
    var editorUsers = ApplicationContext.Current.Services.UserService.GetAll(0, 100, out totalUsers).Where(x => x.UserType.Alias == "editor");
    var userPermissionsSet = new List<EntityPermissionSet.UserPermission>();
    
    // For each user, add our set of permissions
    foreach (var user in editorUsers)
    {
      foreach (char c in "FPUSHKA")
      {
        userPermissionsSet.Add(new EntityPermissionSet.UserPermission(user.Id, c.ToString()));
      }
    }
    
    // Apply the permissions to your content entity
    var permissions = new EntityPermissionSet(contentEntityId, userPermissionsSet);
    ApplicationContext.Current.Services.ContentService.ReplaceContentPermissions(permissions);
    

    The permissions string has a character for each permission you want to set. Here's a list of possible permissions, as far as I can see in 7.5.6:

    • I - Culture and hostnames
    • Z - Audit trail
    • F - Browse node
    • 7 - Change document type
    • O - Copy
    • D - Delete
    • M - Move
    • C - Create
    • P - Public access
    • U - Publish
    • R - Permissions
    • K - Rollback
    • 5 - Send to translation
    • S - Sort
    • H - Send to publish
    • 4 - Translate
    • A - Update

    EDIT Just realised that you need to have a UserPermission for each individual permission-user combination, not a full string of combined permissions. Editing the code above, works fine now.

  • Gurumurthy 54 posts 127 karma points
    Mar 19, 2021 @ 11:34
    Gurumurthy
    0

    Hi All,

    can we restrict public access for an node to with specific members programmatically ?

    enter image description here

    Any help on this would be much appreciated. Umbarco v8.11

    Thanks, Gurumurthy J V

  • WTD-leachA 1 post 21 karma points
    Nov 15, 2022 @ 11:37
    WTD-leachA
    0

    Just wanted to say thanks to OP and Rahul Sekhar. Helped me solve my permissions issue, for particular content nodes on a legacy site we manage.

    👍

Please Sign in or register to post replies

Write your reply to:

Draft