检查用户是否具有文件夹的查看权限

时间:2013-10-08 22:01:26

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

我需要检查活动用户(非有效用户)是否有权查看文件夹。权限将通过组的用户成员资格声明。我预计DriveApp.getAccess(用户)将返回VIEW或EDIT但它似乎返回NONE。我该怎么办?

如果此测试脚本由我拥有并与另一个共享的文件夹和文件从getAccess()返回NONE。

    function myFunction() {
  var folder = DriveApp.getFolderById('0B90FGJizRd-gYTU3ZdsfdsafdsaZTgtNmRiNmQ1NafdsfszE5MWYx');
  Logger.log('owner:'+folder.getOwner().getEmail());
  Logger.log('User:'+Session.getActiveUser().getEmail());
  Logger.log('Sharing access:'+folder.getSharingAccess());
  var access = folder.getAccess('Lijgfjhg@ngs.co.nz');
   Logger.log('active user access:'+access)
  var editors = folder.getEditors();
  Logger.log('editors #'+editors.length)
  for (i=0;i<editors.length;i++){ Logger.log(editors[i].getEmail())};
 var file =  DriveApp.getFileById('1y-m-mfdsafdsfafbPK_ZVQlPi4');
 Logger.log('file owner:'+file.getOwner().getEmail());
   Logger.log('Sharing access:'+file.getSharingAccess());
   access = file.getAccess('Linda.fGF@dgf.co.nz');
     Logger.log('linda user access:'+access)
        access = file.getAccess(Session.getActiveUser().getEmail());
        Logger.log('active user access:'+access)
        Logger.log('editors:'+file.getEditors())
}

[13-10-09 10:45:52:562 NZDT]老板:dadfrqwer@dings.co.nz

[13-10-09 10:45:52:622 NZDT]用户:dafgrfdewjhf@dings.co.nz

[13-10-09 10:45:52:740 NZDT]分享访问权限:私人

[13-10-09 10:45:52:933 NZDT]活跃用户访问:无

[13-10-09 10:45:53:154 NZDT]编辑#0

[13-10-09 10:45:53:444 NZDT]文件所有者:dafrykgsdtfwt@dings.co.nz

[13-10-09 10:45:53:600 NZDT]分享访问权限:私人

[13-10-09 10:45:53:899 NZDT] linda用户访问:无

[13-10-09 10:45:54:118 NZDT]活跃用户访问:无

[13-10-09 10:45:54:244 NZDT]编辑:

1 个答案:

答案 0 :(得分:0)

getEditors()

根据文件:

如果执行脚本的用户没有对文件夹的编辑权限,则此方法返回一个空数组。

示例:

function saveLog(textToSave) {
  var logFolderId = "0B1kNObnC3POnR2d2ntubk";
  var logFolder = DriveApp.getFolderById(logFolderId);
  if (logFolder.getEditors() === []) {
    return ("User " + Session.getActiveUser().getEmail() + " does not have permission to save files to the log folder https://drive.google.com/drive/folders/" + logFolderId );
  } else {
    var fileName = "log.txt";
    var logFile = logFolder.createFile(fileName, textToSave);
    return logFile.getUrl();
  }
}