如何获取 SPList 中的所有文件夹,然后检查权限“Contribute”对于当前用户

发布于 2024-11-28 19:26:54 字数 417 浏览 4 评论 0原文

我有一个这样的共享点列表:

List
---------Folder 1
-----------------Item 1
-----------------Item 2
---------Folder 2
-----------------Item 1
-----------------Item 2
---------Folder 3
-----------------Item 1
-----------------Item 2
  1. 如何获取 List 中的所有文件夹?

  2. 之后检查当前用户是否对文件夹 1文件夹 2文件夹 3 具有贡献权限?

I have a sharepoint list like that:

List
---------Folder 1
-----------------Item 1
-----------------Item 2
---------Folder 2
-----------------Item 1
-----------------Item 2
---------Folder 3
-----------------Item 1
-----------------Item 2
  1. How can I get all Folders in List?

  2. After that checking if current user has Contribute permission on Folder 1, Folder 2, Folder 3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤城病女 2024-12-05 19:26:54

要获取列表中的文件夹列表,您可以使用 SPList 对象的 Folders 属性:

private SPFolderCollection GetListFolders(SPList list) {
  return list.Folders; 
  // you can also do:
  // return list.Folders.Cast<SPFolder>().ToList();
  // to return a List<SPFolder> instead of a SPFolderCollection
}

要检查给定用户是否对您需要获取的文件夹具有贡献权限与 SPFolder 关联的 SPListItem 检查给定用户的 RoleAssignment 并检查其 Contribute 的 RoleDefinitionBindings角色定义:

private bool HasContributePermissionOnFolder(SPFolder folder, SPPrincipal user) {
  var contributePermission = folder.ParentWeb.RoleDefinitions["Contribute"];

  var roleAssignementsOfUser = folder.Item.RoleAssignments.Cast<SPRoleAssignment>()
    .Where(ra => ra.Member == user);

  var hasContributePermission = roleAssignementsOfUser
    .Where(ra => ra.RoleDefinitionBindings.Contains(contributePermission)).Count() > 0;

  return hasContributePermission;
}

使用示例

//remember to add using System.Linq; for the above code to work
//SPList list = <your list>;
//SPWeb web = <your web>;

var folders = GetAllFoldersOfList(list);

foreach (SPFolder folder in folders) {
  if (HasContributePermissionOnFolder(folder, spWeb.CurrentUser)) {
  // do stuff
}

To get the list of folders of a list you can use the Folders property of the SPList object:

private SPFolderCollection GetListFolders(SPList list) {
  return list.Folders; 
  // you can also do:
  // return list.Folders.Cast<SPFolder>().ToList();
  // to return a List<SPFolder> instead of a SPFolderCollection
}

To check if a given user has Contribute permission on a folder you need to get the SPListItem associated with the SPFolder, check for a RoleAssignment of the given user and check its RoleDefinitionBindings for the Contribute Role Definition:

private bool HasContributePermissionOnFolder(SPFolder folder, SPPrincipal user) {
  var contributePermission = folder.ParentWeb.RoleDefinitions["Contribute"];

  var roleAssignementsOfUser = folder.Item.RoleAssignments.Cast<SPRoleAssignment>()
    .Where(ra => ra.Member == user);

  var hasContributePermission = roleAssignementsOfUser
    .Where(ra => ra.RoleDefinitionBindings.Contains(contributePermission)).Count() > 0;

  return hasContributePermission;
}

Usage example

//remember to add using System.Linq; for the above code to work
//SPList list = <your list>;
//SPWeb web = <your web>;

var folders = GetAllFoldersOfList(list);

foreach (SPFolder folder in folders) {
  if (HasContributePermissionOnFolder(folder, spWeb.CurrentUser)) {
  // do stuff
}
迷荒 2024-12-05 19:26:54
private IEnumerable<SPFolder> GetListFolders(SPList list)
{
    return list.Folders.OfType<SPListItem>().Select(item => item.Folder);
}
private IEnumerable<SPFolder> GetListFolders(SPList list)
{
    return list.Folders.OfType<SPListItem>().Select(item => item.Folder);
}
陌伤ぢ 2024-12-05 19:26:54

通过检查角色定义的成员资格来检查用户权限有点冒险。谁敢说角色定义不会被重命名,或者角色定义中包含的基本权限不会被修改。

如果目标主要是检查当前用户对安全对象的权限,那么我认为更好的方法是使用所需的权限掩码调用 SPSecurableObject(SPListItem、SPList、SPWeb 或 SPSite)的重载的DoesUserHavePermissions 方法之一。

Checking user permissions by checking their membership of role definitions is a bit risky. Who's to say that the role definition won't be renamed or that the base permissions included in the role definition won't have been modified.

If the goal is primarily to check the current user's permissions on a securable object then I think a better way is simply to call one of the overloaded DoesUserHavePermissions methods of the SPSecurableObject (SPListItem, SPList, SPWeb or SPSite) with the desired permission mask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文