Created
October 20, 2018 03:05
-
-
Save ad-m/873e9eec3becbdb0ddb75fd8f4ed9347 to your computer and use it in GitHub Desktop.
GSuite Signature sync - Unifies signatures of all users by copying the signature of the selected user.
This file contains hidden or 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
| const {google} = require('googleapis'); | |
| const {JWT} = require('google-auth-library'); | |
| const MasterUser = "adam.dobrawy@siecobywatelska.pl"; | |
| const OrganizationDomain = 'siecobywatelska.pl'; | |
| function getAuthClient(options) { | |
| const authClient = new JWT(Object.assign({}, { | |
| email: 'vacationreplymanager@vacationreplymanager.iam.gserviceaccount.com', | |
| keyFile: 'keys/vacationreplymanager-7dbf49809e60.json', | |
| }, options)); | |
| authClient.authorize(); | |
| return authClient; | |
| } | |
| function getGmailClient(user) { | |
| const authClient = getAuthClient({ | |
| subject: user, | |
| scopes: [ | |
| 'https://mail.google.com/', | |
| 'https://www.googleapis.com/auth/gmail.settings.sharing', | |
| 'https://www.googleapis.com/auth/gmail.settings.basic' | |
| ] | |
| }); | |
| return google.gmail({ | |
| version: 'v1', | |
| auth: authClient | |
| }); | |
| } | |
| const getDirectoryClient = user => { | |
| const authClient = getAuthClient({ | |
| subject: user, | |
| scopes: [ | |
| 'https://www.googleapis.com/auth/admin.directory.user.readonly', | |
| ] | |
| }); | |
| return google.admin({version: 'directory_v1', auth: authClient}) | |
| }; | |
| const getUsers = async () => { | |
| const directory = getDirectoryClient(MasterUser); | |
| return directory.users.list({ | |
| domain: OrganizationDomain | |
| }).then(resp => resp.data.users.map(user => user.primaryEmail)) | |
| }; | |
| const getSignature = async () => { | |
| const gmail = getGmailClient(MasterUser); | |
| const resp = await gmail.users.settings.sendAs.list({ | |
| userId: 'me' | |
| }); | |
| return resp.data.sendAs.find(x => x.signature).signature; | |
| }; | |
| const updateSignature = (gmail, signature, user) => async sendAs => gmail.users.settings.sendAs.patch({ | |
| userId: 'me', | |
| sendAsEmail: sendAs.sendAsEmail, | |
| requestBody: { | |
| signature: signature | |
| } | |
| }).then(() => console.log(`Updated signature at ${sendAs.sendAsEmail} of ${user}.`)); | |
| const updateUser = signature => async user => { | |
| const gmail = getGmailClient(user); | |
| const resp_list = await gmail.users.settings.sendAs.list({ | |
| userId: 'me' | |
| }); | |
| const needUpdate = resp_list.data.sendAs | |
| .filter(sendAs => sendAs.signature !== signature); | |
| console.log(`${needUpdate.length} emails of ${user} need update`); | |
| await Promise.all(needUpdate.map(updateSignature(gmail, signature, user))); | |
| }; | |
| const main = async () => { | |
| const signature = await getSignature(); | |
| console.log(`Signature length ${signature.length} available.`); | |
| const users = await getUsers(); | |
| console.log(`${users.length} users founds.`); | |
| await Promise.all(users.map(updateUser(signature))); | |
| }; | |
| main().catch(err => { | |
| console.log("Something happened", err); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment