获取 TeamDrive 文件夹的权限

Get permissions for a TeamDrive folder

我正在尝试获取有权访问(编辑、评论、查看...)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] []

编辑: 事实上上面的代码是有效的,我的测试文件夹是空的。 但是下面的答案更好。

您可以使用文件来确定在文件本身上设置的权限或从团队云端硬盘继承的权限。

您可以通过 Drive 中的 Permissions 命名空间执行此操作。

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;
}

此函数减少了 return 由 Permissions.list 调用 return 对象数组编辑的信息量,您可以从中确定一大堆信息。单个用户可以是 Team Drive 的 reader 但文件本身的编写者(编辑者),此功能 return 具有两种权限。

例如

 [
    {
        "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(非会员)或角色过滤:reader、作家等。

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