获取TeamDrive文件夹的权限

时间:2018-09-05 08:39:40

标签: google-apps-script google-api google-drive-api google-drive-team-drive

我正在尝试获取对TeamDrive根文件夹(而非TeamDrive文件)具有访问权限(编辑,评论,查看...)的用户列表。

第一次尝试:

var folders = Drive.Teamdrives.list({
  pageSize:100,
  pageToken:folderPT
});

for (var i = 0; i < folders.items.length; i++)
    {
      var folder = folders.items[i];

      var teamDrive = Drive.Teamdrives.get(folder.id);
      var test = DriveApp.getFolderById(folder.id);
      Logger.log(test.getEditors());
    }

我必须使用哪种方法?是否有可能做到这一点 ?

我的代码的响应仅为空结果:

[18-09-05 14:18:36:282 CEST] []
[18-09-05 14:18:36:771 CEST] []
[18-09-05 14:18:37:143 CEST] []
[18-09-05 14:18:37:625 CEST] []
[18-09-05 14:18:38:306 CEST] []
[18-09-05 14:18:38:690 CEST] []

编辑: 实际上上面的代码可以正常工作,我的测试文件夹为空。 但是下面的答案更好。

1 个答案:

答案 0 :(得分:2)

您可以使用文件来确定在文件本身上设置的权限或从Team Drive继承的权限。

您可以通过Permissions中的Drive名称空间来完成此操作。

function getFilePermissions(fileId) {

  var permissions = Drive.Permissions.list(fileId,  {supportsTeamDrives:true,
                                                     corpora: 'teamDrive',
                                                     includeTeamDriveItems: true})
  .items
  .reduce(function(all, i){

    var role = i.teamDrivePermissionDetails.map(function(permission){

      // commenters are flagged as having an additional role as readers
      var activeRole = (i.additionalRoles) ?  i.additionalRoles[0] : permission.role;

      return {emailAddress: i.emailAddress,
              teamDrivePermissionType: permission.teamDrivePermissionType,
              role: activeRole};
    });

    return all.concat(role);
  }, []);

  return permissions;
}

此函数减少了Permissions.list调用返回的信息量,该调用返回一个对象数组,您可以从该对象数组中确定一整堆信息。单个用户可以是Team Drive的读取器,但可以是文件本身的Writer(编辑器),此函数将同时返回两个权限。

例如

 [
    {
        "emailAddress": "geoff@example.com",
        "teamDrivePermissionType": "member",     <-- a Team Drive permission
        "role": "organizer"                      <-- an Team Drive domain member
    },
    {
        "emailAddress": "jonathon@example.com", 
        "teamDrivePermissionType": "member",
        "role": "writer"                         <-- a non-Domain member
    },
    {
        "emailAddress": "jonathon@example.com",
        "teamDrivePermissionType": "file",
        "role": "writer"                         <-- editor set on the file share
    },
    {
        "emailAddress": "svea@example.com",
        "teamDrivePermissionType": "file",
        "role": "reader"                         <-- viewing rights set at file level
    },
    {
        "emailAddress": "info@example.com",
        "teamDrivePermissionType": "member",
        "role": "organizer"
    },
    {
        "emailAddress": "rebekkah@example.com",
        "teamDrivePermissionType": "file",
        "role": "commenter"                      <-- this is a reader with comment rights
    }
]

通过memberfile(非成员)或角色(读者,作家等)对其进行过滤。

有关权限对象架构的更多信息,请在此处检查:Permissions Resource