2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[GAS] ファイルのアクセス権限を絞るスクリプト(ドメイン内の設定も)

Posted at

会社ではG Suite を使っているのですが、GoogleDriveにあるファイル/フォルダのアクセス権限って大事じゃないですか。困るのは「アクセスしちゃダメなユーザーにアクセス権がついちゃってる場合」です。

もちろん運用ルールとして「こうやって使いましょう」というのはありますが、人もファイルも多くなってきて、「すぐ共有したい」という場面が増えてくると、うまく管理できていないケースがあります。見直しというか、棚卸しというか、何かのタイミングでやりたいですよね。

その際に「やたらといっぱい人が追加されてる」ことがありました。それを一人ひとり手動で「削除」していくのが苦痛だったので、とりあえず「オーナーだけアクセスできるように、アクセス可能者を絞る」ためのスクリプトを書きました。(自分がオーナーのファイルのみ有効)

その後、改めて「正しい」アクセス範囲を指定しよう、と。

function changeAccessControl(fileId) {
  const file = DriveApp.getFileById(fileId);

  const me  = Session.getActiveUser().getEmail();
  if ( me !== file.getOwner().getEmail() ) {
    throw new Error(`あなたは [${file.getName()}] [${file.getUrl()}] のファイルのオーナーでは無いため、処理を中断します`);
  }
  
  const sharingAccess = file.getSharingAccess(); 
  file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);
  
  const editors = file.getEditors();
  for (const editor of editors) {
    file.removeEditor(editor);
  } 
  
  const viewers = file.getViewers();
  for (const viewer of viewers) {
    file.removeViewer(viewer);
  } 
}

今回のポイント

↓ これはSpreadsheetを開いて、右上の「共有」を押したあとの画面ですが、

access.png

(1) の部分について

  const editors = file.getEditors();
  for (const editor of editors) {
    file.removeEditor(editor);
  } 
  
  const viewers = file.getViewers();
  for (const viewer of viewers) {
    file.removeViewer(viewer);
  } 

で、そのファイルの編集者と閲覧者を取得して、削除することができます。

参照元

(2) の部分について

Gsuiteを使っていて、そのドメイン(つまり会社・組織)にいる人全員に対しての設定ができるところですが、

  const sharingAccess = file.getSharingAccess(); 
  file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);

で指定できることを今回知りました。

参照元

その後のネタとして

上記スクリプトで「オーナーだけがアクセスできる状態」にしたあと、
「必要な人にだけ、必要な権限(閲覧or編集)をつける」という処理を追加してもいいですね!

↓ こんな感じで!

access2.png

誰かお願いします!

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?