How do I reset permissions for Google Drive documents in all subfolders?

OK, I’ve looked everywhere for the answer for this and don’t seem to be able to find it.

We are trying to get our Google Drive organised so that the right people get access to the right things without a lot of explicit sharing. We have a top-level folder called “Company” with sub-folders for “Admin”, “Finance”, “HR”, “Engineering” and so on. The sub-folders have more restrictive permissions than the top-level one.

We’ve now got more or less everything in the right place, and mostly this works. The problem is where individual files or folders have their own permissions – they then don’t seem to inherit the permissions from their folders. How do we reset the permissions for everything in each part of the hierarchy so they are the same as the parent folder?

On Windows Server or Mac OS X there is a clear distinction between “inherited” and “explicit” permissions for an item, and it is easy to remove the “explicit” permissions for all the items in a hierarchy. Where is this for GD?

Answer

I installed Vidar’s script and works very well for files, but wouldn’t reset permissions on folders. So, I changed a little bit the code to make the folders also inherite the permissions from their upper folder.

Two important disclaimers:

  1. I don’t know if the original intention was to leave folders unchanged. Or even if it worked well for everybody but me, and this solutions was unnecesary. It worked for me.
  2. This is the first script I do, so it is very likely that it is not very efficient and may content some errors. Please feel free to correct it and improve it.

I only changed the doResetPermissions function, but I paste all the code for simplicity reasons.

var selectedFolder;
var dialog;

function doGet() {
  var app = UiApp.createApplication();
  var heading = app.createHTML("<h1>Folders for</h1>");
  app.add(heading);
  var allFoldersIterator = DriveApp.getFolders();
  var text = app.createTextArea().setName("text").setId("output");
  app.add(text);
  var tree = app.createTree();
  var rootFolder = DriveApp.getRootFolder();
  var treeRoot = app.createTreeItem(rootFolder.getName()).setId(rootFolder.getId());
  var handler = app.createServerHandler("folderSelected");
  tree.addItem(treeRoot);
  tree.addSelectionHandler(handler);
  treeRoot.setState(true);
  createBranch(rootFolder, treeRoot, app);
  app.add(tree);
  var selectedFolderIdBox = app.createTextBox().setName("selectedFolderIdBox").setId("selectedFolderIdBox");
  app.add(selectedFolderIdBox);
  return app;
}

function createBranch(folder, branch, app) {
  var children = folder.getFolders();
  while (children.hasNext()) {
    child = children.next();
    Logger.log("Folder id: " + child.getId());
    var subBranch = app.createTreeItem(child.getName()).setId(child.getId());
    branch.addItem(subBranch);
    createBranch(child, subBranch, app);
  }
}

function folderSelected(eventInfo) {
  var app = UiApp.getActiveApplication();
  var parameter = eventInfo.parameter;
  Logger.log("Selected " + eventInfo.parameter.selected);
  selectedFolder = DriveApp.getFolderById(eventInfo.parameter.selected);
  var selectedFolderIdBox = app.getElementById("selectedFolderIdBox");
  selectedFolderIdBox.setValue(eventInfo.parameter.selected);
  var okHandler = app.createServerHandler("okResetPermissions");
  okHandler.addCallbackElement(selectedFolderIdBox);
  dialog = app.createDialogBox(true, true);
  var panel = app.createFlowPanel();
  dialog.add(panel);
  dialog.setId("confirmationDialog");
  dialog.setPopupPosition(100, 100).setSize(500, 500);
  panel.add(app.createLabel("Do you want to reset permissions for all files found in folder " + selectedFolder.getName() + " and its subfolders?\n (Click outside of the dialog box to cancel)"));
  panel.add(app.createButton("Do it!", okHandler));
  dialog.show();
  return app;
}

function okResetPermissions(eventInfo) {
  var selectedFolderId = eventInfo.parameter.selectedFolderIdBox;
  var selectedFolder = DriveApp.getFolderById(selectedFolderId);
  Logger.log("Resetting permissions on " + selectedFolder.getName());
  var app = UiApp.getActiveApplication();
  app.getElementById("confirmationDialog").hide();
  Logger.log("Confirmation dialog hidden");
  var progressIndicator = app.createDialogBox(false, true);
  progressIndicator.setId("progressIndicator");
  progressIndicator.setPopupPosition(100, 100).setSize(500, 500);
  var label = app.createLabel("Working ...");
  progressIndicator.add(label);
  progressIndicator.show();
  doResetPermissions(selectedFolder, function() {
    var app = UiApp.getActiveApplication();
    var progressIndicator = app.getElementById("progressIndicator");
    progressIndicator.hide();
    return app;
  });
  return app;
}

function doResetPermissions(selectedFolder, callback) {
  Logger.log("Resetting permissions on folder " + selectedFolder.getName());

  var sharingAccess = selectedFolder.getSharingAccess();
  var sharingPermission = selectedFolder.getSharingPermission();

  var viewersfolder = selectedFolder.getViewers();
  Logger.log("Number of Viewers in folder: " + selectedFolder.getName() + " = " + viewersfolder.length);
  for (var i = 0; i < viewersfolder.length; i++) {
    Logger.log("      Removing viewer " + viewersfolder[i].getEmail());
    selectedFolder.removeViewer(viewersfolder[i]);
  }

  var editorsfolder = selectedFolder.getEditors();

  Logger.log("Number of Editors in folder: " + selectedFolder.getName() + " = " + editorsfolder.length);

  for (var i = 0; i < editorsfolder.length; i++) {
    Logger.log("      Removing editor " + editorsfolder[i].getEmail());
    selectedFolder.removeEditor(editorsfolder[i]);
  }

  var files = selectedFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    Logger.log("   Resetting permissions on file " + file.getName());
    file.setSharing(sharingAccess, sharingPermission);

    var viewers = file.getViewers();
    Logger.log("Number of Viewers in file: " + file.getName() + " = " + viewers.length);
    for (var i = 0; i < viewers.length; i++) {
      Logger.log("      Removing viewer " + viewers[i].getEmail());
      file.removeViewer(viewers[i]);
    }

    var editors = file.getEditors();

    Logger.log("Number of editors in file: " + file.getName() + " = " + editors.length);

    for (var i = 0; i < editors.length; i++) {
      Logger.log("      Removing editor " + editors[i].getEmail());
      file.removeEditor(editors[i]);
    }
  }

  var children = selectedFolder.getFolders();
  while (children.hasNext()) {
    var child = children.next();
    doResetPermissions(child);
  }

  if (callback) {
    callback();
  }
}

Attribution
Source : Link , Question Author : Bids , Answer Author : Alex

Leave a Comment