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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With