如何使用sharepoint api找出另一个用户对每个树元素(文件夹或文件)的权限?

时间:2018-08-16 15:47:54

标签: rest sharepoint permissions sharepoint-api

我想回答问题:

  

用户A 可以读取文件夹/树吗?

应用程序使用另一个用户通过sharepoint API与sharpoint通信。

我已经搜索了以下请求:

http://aissp2013/sites/Team/_api/web/lists/getbytitle('L2')/EffectiveBasePermissions

但是它不能解决我的问题,因为它以当前用户的权限响应!

  1. 有没有办法为其他用户获得具体元素(文件夹,文件或库)的权限
  2. 是否可以为其他用户获取所有元素和子元素的权限?

1 个答案:

答案 0 :(得分:2)

要获取每个用户的权限 SP.ListItem.getUserEffectivePermissions method的目的是

SP.Folder

Url /_api/web/getFolderByServerRelativeUrl('<folder-rel-url>')/ListItemAllFields/getusereffectivepermissions(@u)?@u='<account>'
Method: Get

SP.File

Url /_api/web/getFileByServerRelativeUrl('<file-rel-url>')/ListItemAllFields/getusereffectivepermissions(@u)?@u='<account>'
Method: Get
  

注意:claims format中需要提供account参数

关于这个问题:

  

userA可以读取文件夹/树吗?

以下示例演示了如何检索文件夹的权限

 const accountName = "i:0#.f|membership|<name>@<tenant>.onmicrosoft.com"; 

  let endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getFolderByServerRelativeUrl('/Shared Documents/Achive')/ListItemAllFields/getusereffectivepermissions(@u)?@u='" + encodeURIComponent(accountName) + "'";
  const content = await executeJson(endpointUrl);

  let roles = parseBasePermissions(content.d.GetUserEffectivePermissions);
  if(roles.viewListItems)
     console.log(`${accountName} has been granted permissions.`);

其中

function parseBasePermissions(value)
{      
    let permissions = new SP.BasePermissions();
    permissions.initPropertiesFromJson(value);
    let result = {};
    for(var levelName in SP.PermissionKind.prototype) {
        if (SP.PermissionKind.hasOwnProperty(levelName)) {
            var permLevel = SP.PermissionKind.parse(levelName);
            if(permissions.has(permLevel))
                result[levelName] = true;
            else
                result[levelName] = false;
        }     
    }
    return result; 
}

用于将权限掩码解析为角色

async function executeJson(url,options) {
    options = options || {};
    options.method = options.method || 'GET';
    options.headers = options.headers || {};
    options.headers["Accept"] = "application/json;odata=verbose";
    options.headers["Content-Type"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        options.headers["X-RequestDigest"] = document.getElementById("__REQUESTDIGEST").value;
    }    
    if (options.body) {
       options.body = JSON.stringify(options.body);
    }  
    const rawResponse = await fetch(url,options);
    const content = await rawResponse.json();
    return content;
}

执行REST请求