如何检查共享点组是否在 SPListItem 中具有读取或写入权限

How to check if a sharepoint group has read or write permission in SPListItem

如何检查共享点组是否在 SPListItem 中具有读取或写入权限。

我们可以使用此代码检查 SPUser 权限:

SPBasePermissions perms = SPBasePermissions.EditListItems;
spListItem.DoesUserHavePermissions(spUser, perms);

但我找不到任何地方应该如何检查组的权限。这就是我要找的:

spListItem.DoesUserHavePermissions(spGroup, perms);

检查组的权限时,您可以直接查看 SPListItem 的 RoleAssignments property (which is a collection of SPRoleAssignment 对象)并查看任何角色分配的 Member 属性 是否与您想要的组相对应。

与用户不同,组不能嵌套在 Active Directory 组和 SharePoint 组中,因此您不需要比直接角色分配更深入地查看。

最简单的解决方案是使用 SPRoleAssignmentCollection 对象的 GetAssignmentByPrincipal 方法。

bool hasEdit = false;
SPRoleAssignment ra = spListItem.RoleAssignments.GetAssignmentByPrincipal(spGroup);
SPRoleDefinitionBindingCollection permissions = ra.RoleDefinitionBindings;
foreach(SPRoleDefinition level in permissions)
{
    if(level.BasePermissions & SPBasePermissions.EditListItems == SPBasePermissions.EditListItems 
      || level.BasePermissions & SPBasePermissions.FullMask == SPBasePermissions.FullMask)
    {
         hasEdit = true;
         break;
    }
}

请注意,在上面的代码中,将权限级别的BasePermissions 属性与特定的SPBasePermissions enumeration, I'm using the approach recommended by Microsoft's Guidelines for FlagsAttribute and Enum进行比较时:

  • A convenient way to test whether a flag is set in a numeric value is to perform a bitwise AND operation between the numeric value and the flag enumerated constant, which sets all bits in the numeric value to zero that do not correspond to the flag, then test whether the result of that operation is equal to the flag enumerated constant.