4
votes

I want to check if a sitecore item has any access rights applied or not so I am thinking of checking if the security field has a value i.e

item.Fields["__Security"].Value

Is this is the correct way of checking if an item has access rights or Is there another way of doing this?

2

2 Answers

5
votes

Yes, in the __Security Field are the rights stored.

You can use: item.Security.GetAccessRules();

var accessRules = item.Security.GetAccessRules();
if (accessRules != null)
{
    foreach (var rule in accessRules)
    {
        var name = rule.Account.Name;
        var comment = rule.AccessRight.Comment;
        var permiss = rule.SecurityPermission;
    }
}
2
votes

This sort of thing feels more more API-ish

foreach(Role role in RolesInRolesManager.GetAllRoles())
{
    bool hasReadAccess= itemUnderTest.Security.CanRead(role);
}

In fact, take a look at all the methods of Item.Security