Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear security role in Sitecore?

Tags:

c#

roles

sitecore

I am trying to remove a specific role from an item. Right now I have 3 role assigned. "Everyone", "sitecore\author", "sitecore\developer". How do I remove "Everyone" and "sitecore\author". So far I have tried this but it is not working.

public static void ClearSecurityRoles(this Item item)
{
    var accessrules = item.Security.GetAccessRules();
    var role = Sitecore.Security.Accounts.Role.FromName("Everyone");
    accessrules.Helper.RemoveExactMatches(role,AccessRight.Any,PropagationType.Any);
    item.SaveSecurityAccessRules(accessrules);
}

public static void SaveSecurityAccessRules(this Item item, AccessRuleCollection accessRules)
{
    item.Editing.BeginEdit();
    item.Security.SetAccessRules(accessRules);
    item.Editing.EndEdit();
}

When I use accessrules.clear() it removes all the roles, but I want to be able to remove just two. How do I do that?

like image 409
siddhipur Avatar asked Nov 09 '22 15:11

siddhipur


1 Answers

You could do something like:

Role role = Role.FromName("Everyone");
AccessRuleCollection accessRules = item.Security.GetAccessRules();

foreach (var accessRight in _accessRights)
{
   AccessRight right = AccessRight.FromName(accessRight.Value);
   accessRules.Helper.RemoveExactMatches(role, right, PropagationType.Any);
}
item.Security.SetAccessRules(accessRules);
like image 194
Santiago Morla Avatar answered Nov 14 '22 22:11

Santiago Morla