Box: コラボレーション削除 (Box: Delete Collaboration)
この工程は、Box の指定ファイルもしくはフォルダから、指定ユーザとのコラボレーションを削除します。ユーザはメールアドレスで指定します。全ての情報が一致しない場合は削除できません。
Configs:共通設定
  • 工程名
  • メモ
Configs
  • C1: OAuth2 設定 *
  • C2: コラボレーションを削除する Box ユーザのメールアドレス *
  • C3: ロール *
  • C4: 項目の種類(ファイルまたはフォルダ) *
  • C5: コラボレーションを削除するファイル/フォルダの ID *

Notes

  1. メールアドレス、ロール、項目の種別の全てが一致しないとコラボレーションは削除できません
  2. Box のリフレッシュトークンには、期限があります。期限を超えないよう、定期的に利用する必要があります

Capture

See also

Script (click to open)
  • 下記のスクリプトを記述した XML ファイルをダウンロードできます
    • box-collaboration-delete.xml (C) Questetra, Inc. (MIT License)
    • Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます

main();
function main(){

  const oauth2 = configs.get("conf_OAuth2");
  const itemType = configs.get("conf_ItemType");
  const fileOrFolderId = decideFileOrFolderId(itemType);
  const emailAddress = decideEmailAddress();
  const role = configs.get("conf_Role");
  
  // コラボレーションのリストを取得
  const collaborationList = getCollaborationList(oauth2, itemType, fileOrFolderId);
  // コラボレーションIDを探す
  const collaborationId = findCollaborationId(collaborationList, emailAddress, role);
  // コラボレーション削除
  deleteCollaboration(oauth2, collaborationId);
}

/**
  * 指定するメールアドレスをconfigから読み出して出力する
  * @return {String}  emailAddress メールアドレス
  */
function decideEmailAddress(){
  let emailAddress = "";
  const emailAddressDef = configs.getObject("conf_EmailAddress");
  if(emailAddressDef === null){
    emailAddress = configs.get("conf_EmailAddress");
  }else{
    emailAddress = engine.findData(emailAddressDef);
  }
  
  if(emailAddress === "" || emailAddress === null) {
    throw "Email address is blank";
  }
  return emailAddress;
}

/**
  * コラボレーションを削除するファイルもしくはフォルダのIDをconfigから読み出して出力する
  * @param {String}  itemType アクセス権限が付与される項目の種類 ファイル/フォルダ
  * @return {String} id ファイル もしくは フォルダの ID
  */
function decideFileOrFolderId(itemType){
  let id = "";
  const idDef = configs.getObject("conf_ItemId");
  if(idDef === null){
    id = configs.get("conf_ItemId");
  }else{
    id = engine.findData(idDef);
  }
  
  if(id === "" || id === null) {
    throw "File ID or Folder ID is blank";
  }
  
  if(itemType === "folders" && id === "0") {
    throw "Collaborations not available on root folder";
  }
  return id;
}

/**
  * ファイルまたはフォルダのコラボレーションのリストを取得する
  * @param {String} oauth OAuth2 設定
  * @param {String} itemType  項目の種類 ファイル/フォルダ
  * @param {String} fileOrFolderId ファイル/フォルダの ID
  * @return {Object} collaborationList コラボレーションのリストを格納した JSON オブジェクト
  */
function getCollaborationList(oauth2, itemType, fileOrFolderId) {
  const url = `https://api.box.com/2.0/${itemType}/${fileOrFolderId}/collaborations/`;
  const response = httpClient.begin()
    .authSetting(oauth2)
    .queryParam("fields", "id,accessible_by,invite_email,role")
    .get(url);
  
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  
  if (status !== 200) {
    engine.log(responseTxt);
    throw `Failed to get information. status: ${status}`;
  }
  
  const jsonRes = JSON.parse(responseTxt);
  if (jsonRes.entries.length === 0) {
    throw `No collaborations in ID: ${fileOrFolderId}`;
  }
  return jsonRes.entries;
}

/**
  * コラボレーションのリストからコラボレーション ID を探す
  * @param {Object} collaborationList コラボレーションのリストを格納した JSON オブジェクト
  * @param {String} emailAddress メールアドレス
  * @param {String} role ロール
  * @return {String} collaborationId コラボレーション ID
  */
function findCollaborationId(collaborationList, emailAddress, role) {
  for (let i = 0; i < collaborationList.length; i++){
    if(collaborationList[i].role !== role){
      continue;
    }
    if(collaborationList[i].accessible_by === null){
      // 未承認ユーザの場合、招待メールアドレスをチェック
      if(collaborationList[i].invite_email === emailAddress){
        return collaborationList[i].id;
      }
      continue;
    }
    if(collaborationList[i].accessible_by.login === emailAddress){
      return collaborationList[i].id;
    }
  }
  
  throw `Could not find the collaboration`;
}

/**
  * コラボレーションを削除する
  * @param {String} oauth2 Oauth2設定
  * @param {String} collaborationId コラボレーション ID
  */
function deleteCollaboration(oauth2, collaborationId) {
  const url = `https://api.box.com/2.0/collaborations/${collaborationId}`;
  const response = httpClient.begin()
    .authSetting(oauth2)
    .delete(url);
  
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  
  if (status !== 204) {
    engine.log(responseTxt);
    throw `Failed to delete the collaboration. status: ${status}`;
  }
  
}

%d人のブロガーが「いいね」をつけました。