Programmatically check permissions for a sharepoint group
How can I check whether a specified group has a certain permission for an item? I know that there are methods like DoesUserHavePermissi开发者_StackOverflow中文版ons() on item, but what about groups?
Untested code, but something like this.
This works for both groups and users and also for everything with permissions (items, lists, webs etc.)
public static bool DoesPrincipalHavePermission(ISecurableObject @object, SPPrincipal principal, SPRoleDefinition role)
{
var assignment = @object.RoleAssignments.GetAssignmentByPrincipal(principal);
if (assignment == null || assignment.RoleDefinitionBindings.Count < 1)
return false;
foreach (SPRoleDefinition r in assignment.RoleDefinitionBindings)
{
if (r.BasePermissions == role.BasePermissions)
return true;
}
return false;
}
精彩评论