Last active
December 4, 2022 07:57
-
-
Save ohsawa0515/ef5736e7baa849975aaf6d000ed16ef9 to your computer and use it in GitHub Desktop.
Cloud Identity APIでGoogleグループのメンバーリストの取得、メンバー追加・削除をするGoogle Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ユーザーリストを返す | |
* @param {string} groupEmail - グループのEmail | |
* @return {string[]} - ユーザーEmailのリスト | |
*/ | |
function listMembers(groupEmail) { | |
let memberships = listMemberships(groupEmail); | |
var members = []; | |
for (const membership of memberships) { | |
// ロールがオブジェクトかつ配列になっているので「,」でジョインした文字列にする | |
var roles = []; | |
membership['roles'].forEach(function (value) { | |
var property = Object.entries(value); | |
property.forEach(function (v) { | |
roles.push(v[1]); | |
}); | |
}); | |
members.push([membership['preferredMemberKey']['id'], roles.join()]); | |
} | |
return members; | |
} | |
/** | |
* ユーザーをグループに追加 | |
* @param {string} userEmail - ユーザーのEmail | |
* @param {string} groupEmail - グループのEmail | |
* @return {string} - 実行結果 | |
*/ | |
function insertMember(userEmail, groupEmail) { | |
const url = `https://cloudidentity.googleapis.com/v1/${getGroupId(groupEmail)}/memberships`; | |
let body = { | |
"preferredMemberKey": { "id": userEmail }, | |
"roles": { | |
"name": "MEMBER" | |
} | |
} | |
let response = UrlFetchApp.fetch(url, { | |
headers: { | |
Authorization: `Bearer ${getToken()}` | |
}, | |
method: "POST", | |
contentType: "application/json", | |
payload: JSON.stringify(body), | |
muteHttpExceptions: true | |
}); | |
console.log(response.getContentText()); | |
return response.getContentText(); | |
} | |
/** | |
* ユーザーをグループから削除 | |
* @param {string} userEmail - ユーザーのEmail | |
* @param {string} groupEmail - グループのEmail | |
* @return {string} - 実行結果 | |
*/ | |
function deleteMember(userEmail, groupEmail) { | |
const url = `https://cloudidentity.googleapis.com/v1/${getMembershipId(userEmail, groupEmail)}` | |
let response = UrlFetchApp.fetch(url, { | |
headers: { | |
Authorization: `Bearer ${getToken()}` | |
}, | |
method: "DELETE", | |
muteHttpExceptions: true | |
}); | |
console.log(response.getContentText()); | |
return response.getContentText(); | |
} | |
function listMemberships(groupEmail) { | |
let memberships = []; | |
let response; | |
let nextPageToken; | |
do { | |
response = listMembershipByPageToken(groupEmail, nextPageToken); | |
memberships = memberships.concat(response["memberships"]); | |
// debug | |
console.log(memberships); | |
nextPageToken = response["nextPageToken"]; | |
if (typeof nextPageToken === 'undefined') { | |
return memberships; | |
} | |
} while (true); | |
} | |
function listMembershipByPageToken(groupEmail, pageToken) { | |
let url = `https://cloudidentity.googleapis.com/v1/${getGroupId(groupEmail)}/memberships`; | |
if (typeof pageToken !== 'undefined') { | |
url += `?pageToken=${pageToken}` | |
} | |
// debug | |
// console.log(url); | |
let response = UrlFetchApp.fetch(url, { | |
headers: { | |
Authorization: `Bearer ${getToken()}` | |
} | |
}); | |
return JSON.parse(response.getContentText()); | |
} | |
function getToken() { | |
return ScriptApp.getOAuthToken(); | |
} | |
function getGroupId(groupEmail) { | |
const url = "https://cloudidentity.googleapis.com/v1/groups:lookup"; | |
let response = UrlFetchApp.fetch(url + "?groupKey.id=" + groupEmail, { | |
headers: { | |
Authorization: `Bearer ${getToken()}` | |
} | |
}); | |
let res_json = JSON.parse(response.getContentText()); | |
return res_json["name"]; | |
} | |
function getMembershipId(userEmail, groupEmail) { | |
const memberships = listMemberships(groupEmail); | |
for (const membership of memberships) { | |
if (membership['preferredMemberKey']['id'] == userEmail) { | |
return membership['name']; | |
} | |
} | |
return null; | |
} |
Author
ohsawa0515
commented
Sep 18, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment