Copied to clipboard

Flag this post as spam?

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


  • lori ryan 239 posts 573 karma points
    Feb 25, 2021 @ 09:49
    lori ryan
    0

    Programmatically set permissions on node

    Is there a way to Programmatically set permissions on node for a group? i.e. this group can only browse home, this group can browse, edit and create.

    Is it possible to do this by pageAlias?

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Feb 25, 2021 @ 10:48
    Kevin Jump
    1

    Hi Lori,

    You can do this with the PublicAccessService (docs)

    you can get the rule for a current content item, GetEntryForContent:

    var rule = publicAccessService.GetEntryForContent(contentItem);
    

    or you can add rules first by creating a rule,

    var rule = new PublicAccessEntry(contentItem, loginNode, noAccessNode, new List<PublicAccessRule>());
    

    (you need to have a content node for the login page (loginNode), and one for the error/no access page (noAccessNode)).

    then you can add rules to it.

    rule.AddRule(ruleValue, ruleType);
    

    Rule value will be the user/group and rule type is the type of rule. rule type is either MemberUsername or MemberRole these are defined in Umbraco (so you can use these to be sure)

    Umbraco.Core.Constants.Conventions.PublicAccess.MemberRoleRuleType
    Umbraco.Core.Constants.Conventions.PublicAccess.MemberUsernameRuleType
    

    then you can save it

    publicAccessService.Save(item);
    

    this should create a rule against a bit of content, rules inherit down, so if you do this on for example the homepage all rules beneath that will be protected.

Please Sign in or register to post replies

Write your reply to:

Draft