-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle.gs
More file actions
481 lines (427 loc) · 14.9 KB
/
google.gs
File metadata and controls
481 lines (427 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
For this script to work, you must include the OAuth2 library from here: https://github.com/googleworkspace/apps-script-oauth2
And also, some functions rely on the Admin directory API
*/
/**
* Admin user email to be used for impersonation in service calls.
* Replace with the email of the admin user.
*/
//const USER_EMAIL_TO_IMPERSONATE = ""; // Replace with the email of the admin user
/**
* Adds a mail delegate to a specified user's Gmail account.
* @param {string} userEmail - The email of the user to whom a delegate will be added.
* @param {string} delegateEmail - The email of the delegate to be added.
*/
function addMailDelegate_(userEmail, delegateToEmail) {
const service = getService_(userEmail);
//console.log(service.hasAccess());
if (service.hasAccess()) {
const url = `https://gmail.googleapis.com/gmail/v1/users/${userEmail}/settings/delegates`;
const payload = {
delegateEmail: delegateToEmail,
// Other required delegation parameters
};
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
console.log(`Successfully added delegate ${delegateToEmail} to ${userEmail}\n${response.getContentText()}`);
} else {
console.error(`Adding ${delegateToEmail} as delegate to ${userEmail} failed.\nreason: ${service.getLastError()}`);
}
}
/**
* Creates and returns an OAuth2 service for Google Admin SDK.
* @param USER_EMAIL_TO_IMPERSONATE - user to imporsanate using domain wide delegation {string}
* @return {OAuth2Service} The OAuth2 service for authorization.
*/
function getService_(USER_EMAIL_TO_IMPERSONATE) {
return OAuth2.createService('Google Admin SDK')
.setTokenUrl('https://oauth2.googleapis.com/token')
.setPrivateKey(PropertiesService.getScriptProperties().getProperty("PRIVATE_KEY"))
.setIssuer(PropertiesService.getScriptProperties().getProperty("CLIENT_EMAIL"))
.setPropertyStore(PropertiesService.getScriptProperties())
.setParam('scope', 'https://www.googleapis.com/auth/gmail.settings.sharing '+
'https://www.googleapis.com/auth/admin.directory.user.readonly')
.setSubject(USER_EMAIL_TO_IMPERSONATE); // Admin user email
}
/**
* Lists all the users in a domain sorted by first name.
* @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
* @return {Array} An array of user objects, each containing user details.
*/
function listAllUsers() {
let pageToken;
let page;
var count =0;
var title;
var usersArr = [];
do {
page = AdminDirectory.Users.list({
domain: 'bizzabo.com',
orderBy: 'givenName',
maxResults: 300,
pageToken: pageToken
});
const users = page.users;
if (!users) {
console.log('No users found.');
return;
}
// Print the user's full name and email.
for (const user of users) {
if (!user.suspended)
{
try {// Try to get the users title if there is an error push the error to the array
title = user.organizations[0].title;
} catch (e) {
continue
}
if(user.primaryEmail.indexOf("-tmp") != -1) continue;
var curr = new Object();
curr["fullName"] = user.name.fullName;
curr["primaryEmail"] = user.primaryEmail;
curr["title"] = title;
usersArr.push(curr);
//console.log('%s (%s) (%s)', curr["fullName"] , curr["primaryEmail"] , curr["title"]);
count++;
}
}
pageToken = page.nextPageToken;
} while (pageToken);
return usersArr;
}
/**
* Lists all users in the domain without any restrictions.
* @return {Array} An array of user objects, each containing user details.
*/
function listUsers_No_restrictions_(){
/**
* Lists all the users in a domain sorted by first name.
* @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
*/
let pageToken;
let page;
var list = [];
do {
page = AdminDirectory.Users.list({
domain: 'bizzabo.com',
orderBy: 'givenName',
maxResults: 100,
projection: 'FULL', // Include all data for each user
pageToken: pageToken
});
const users = page.users;
if (!users) {
console.log('No users found.');
return;
}
// Print the user's full name and email.
for (const user of users) {
//console.log('%s (%s)', user.name.fullName, user.primaryEmail);
list.push(user);
}
pageToken = page.nextPageToken;
} while (pageToken);
return list;
}
/**
* Suspends a specified user in Google Workspace.
* @param {string} email - The email of the user to be suspended.
*/
function suspendUser_(email) {
try{
var user = AdminDirectory.Users.get(email);
if (user && user.suspended !== true) {
user.suspended = true;
AdminDirectory.Users.update(user, email);
Logger.log('User suspended: ' + email);
} else {
Logger.log('User not found or already suspended');
}
} catch(e) {
console.error(`Error suspending user ${email} : ${e.message}`);
return false;
}
}
/**
* Retrieves the Organizational Unit (OU) path of a specified user.
* @param {string} userEmail - The email of the user whose OU path is to be retrieved.
* @return {string} The OU path of the user.
*/
function getUserOU(userEmail) {
var user;
try {
user = AdminDirectory.Users.get(userEmail);
} catch (e) {
console.error("Error retrieving user information: " + e.message);
return "User not found or access denied.";
}
if (user && user.orgUnitPath) {
Logger.log("User " + userEmail + " is in the OU: " + user.orgUnitPath);
return user.orgUnitPath;
} else {
Logger.log("Organizational unit information not available for " + userEmail);
return "OU information not available.";
}
}
/**
* Moves a specified user to a different Organizational Unit (OU).
* @param {string} userEmail - The email of the user to be moved.
* @param {string} ouLocation - The destination OU path.
*/
function moveToOU_(userEmail , ouLocation) {
var offboardingOU = ouLocation; // Update this path to match your OU structure
var user = {
orgUnitPath: offboardingOU
};
try {
AdminDirectory.Users.update(user, userEmail);
Logger.log(userEmail + ' moved to '+ouLocation+' OU.');
} catch (e) {
console.error('Error moving ' + userEmail + ' to '+ouLocation + ' OU: ' + e.message);
}
}
/**
* Resets the password for a specified user and sets whether they must change it at next login.
* @param {string} userEmail - The email of the user whose password is to be reset.
* @param {boolean} changeNextLogin - Whether the user should change their password at the next login.
*/
function resetUserPassword_(userEmail , changeNextLogin) {
var newPassword = generateRandomPassword_();
var user = {
password: newPassword,
changePasswordAtNextLogin: changeNextLogin // User must change password at next login
};
try {
AdminDirectory.Users.update(user, userEmail);
Logger.log('Password reset for ' + userEmail + '. New Password: ' + newPassword);
} catch (e) {
console.error('Error resetting password for ' + userEmail + ': ' + e.message);
}
}
/**
* Updates the departure date for a specified user in their custom schema.
* @param {string} userEmail - The email of the user whose departure date is to be updated.
* @param {string} departureDate - The departure date to set.
*/
function updateDepartureDate_(userEmail, departureDate) {
var customSchemas = {
"Basic_information": { // Replace with your actual schema name
"Departure_date": departureDate
}
};
var user = {
customSchemas: customSchemas
};
try {
AdminDirectory.Users.update(user, userEmail);
Logger.log('Departure date updated for ' + userEmail);
} catch (e) {
Logger.log('Error updating departure date for ' + userEmail + ': ' + e.message);
}
}
/**
* Updates the data retention settings for a specified user in their custom schema.
* @param {string} userEmail - The email of the user whose data retention settings are to be updated.
* @param {boolean} gmail - The Gmail data retention setting.
* @param {boolean} gDrive - The Google Drive data retention setting.
* @param {boolean} cal - The Calendar data retention setting.
* @param {string} transferTo - Email to which data should be transferred.
*/
function updateDataRetention_(userEmail, gmail,gDrive,cal , transferTo) {
var transferTo = {
value:transferTo,
type : "work"
};
var customSchemas = {
"IT_data_retention": { // Replace with your actual schema name
"Gmail": gmail,
"Google_drive" : gDrive,
"Calendar" : cal,
"Who_to_transfer_data_to" : [transferTo]
}
};
var user = {
customSchemas: customSchemas
};
try {
AdminDirectory.Users.update(user, userEmail);
console.log('IT data retention updated for ' + userEmail);
} catch (e) {
console.error('Error updating departure date for ' + userEmail + ': ' + e.message);
}
}
/**
* Prints the custom schemas of a specified user.
* @param {string} userEmail - The email of the user whose custom schemas are to be printed.
*/
function printUserCustomSchemas(userEmail) {
try {
// Fetch user details with full projection to get all data including custom schemas
var user = AdminDirectory.Users.get(userEmail, {projection: 'full'});
var customSchemas = user.customSchemas;
// Check if the user has any custom schemas
if (customSchemas) {
Logger.log('Custom schemas for ' + userEmail + ':');
// Iterate over each schema and print the schema name and fields
for (var schemaName in customSchemas) {
Logger.log('Schema: ' + schemaName);
for (var fieldName in customSchemas[schemaName]) {
var fieldValue = customSchemas[schemaName][fieldName];
// Check if the field value is an array (or object, and iterate accordingly)
if (Array.isArray(fieldValue)) {
fieldValue.forEach(function(item) {
Logger.log(' - Field: ' + fieldName + ', Value: ' + item.value);
});
} else {
Logger.log(' - Field: ' + fieldName + ', Value: ' + fieldValue);
}
}
}
} else {
Logger.log('No custom schemas found for ' + userEmail + '.');
}
} catch (e) {
Logger.log('Error retrieving custom schemas for ' + userEmail + ': ' + e.message);
}
}
/**
* Retrieves the Gmail profile of a specified user.
* @param {string} userEmail - The email of the user whose Gmail profile is to be retrieved.
*/
function getUserProfile_(userEmail ) {
const service = getService_();
if (service.hasAccess()) {
const url = `https://gmail.googleapis.com/gmail/v1/users/${userEmail}/profile`;
const options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
console.info(response.getContentText());
} else {
console.error(service.getLastError());
}
}
/**
* Retrieves the manager's email for a specified user.
* @param {string} userEmail - The email of the user whose manager's email is to be retrieved.
* @return {string|null} The manager's email, or null if not found.
*/
function getUsersManagerEmail_(userEmail) {
try {
var user = AdminDirectory.Users.get(userEmail);
if (user.relations) {
for (var i = 0; i < user.relations.length; i++) {
if (user.relations[i].type === 'manager') {
return user.relations[i].value;
}
}
}
console.warn('Manager not found for ' + userEmail);
return null;
} catch (e) {
console.error('Error fetching manager information: ' + e.message);
return null;
}
}
/**
* Removes a specified user from all their Google groups.
* @param {string} userEmail - The email of the user to be removed from all groups.
*/
function removeUserFromAllGroups_(userEmail) {
try {
// List all groups for the user
var groups = AdminDirectory.Groups.list({
userKey: userEmail
});
if (groups.groups && groups.groups.length > 0) {
// Iterate through each group and remove the user
groups.groups.forEach(function(group) {
AdminDirectory.Members.remove(group.id, userEmail);
Logger.log('Removed user ' + userEmail + ' from group: ' + group.email);
});
} else {
console.warn('No groups found for user ' + userEmail);
}
} catch (e) {
console.error('Error removing user from groups: ' + e.message);
}
}
/**
* Lists all Organizational Units (OUs) in the domain and logs their names and IDs.
*/
function listAllOUs() {
var ous = AdminDirectory.Orgunits.list('my_customer');
if (ous.organizationUnits && ous.organizationUnits.length > 0) {
for (var i = 0; i < ous.organizationUnits.length; i++) {
var ou = ous.organizationUnits[i];
console.info('OU Name: %s, OU ID: %s', ou.name, ou.orgUnitId);
}
} else {
console.warn('No organizational units found.');
}
}
/**
* Hides a specified user from the Global Address List (GAL) in Google Workspace.
* This action makes the user's profile invisible in directory searches
* and contacts suggestions within your organization.
*
* @param {string} userEmail - The email address of the user to be hidden from the GAL.
*/
function hideUserFromGlobalAddressList_(userEmail) {
try {
// Retrieve the current user's settings
var user = AdminDirectory.Users.get(userEmail);
// Update the includeInGlobalAddressList setting
user.includeInGlobalAddressList = false;
// Apply the update
AdminDirectory.Users.update(user, userEmail);
Logger.log(userEmail + ' has been hidden from the Global Address List.');
} catch (e) {
Logger.log('Error hiding ' + userEmail + ' from the Global Address List: ' + e.message);
}
}
/**
* Retrieves all users in a specified Organizational Unit (OU) path.
*
* @param {string} ouPath - The OU path for which to retrieve users.
* @return {Array} List of users in the specified OU.
*/
function getUsersInOU_(ouPath) {
var usersInOU = [];
var pageToken;
do {
try {
var response = AdminDirectory.Users.list({
domain: 'bizzabo.com', // Replace with your domain
query: 'orgUnitPath=\'' + ouPath + '\'',
maxResults: 100,
pageToken: pageToken
});
var users = response.users;
if (users) {
usersInOU = usersInOU.concat(users);
}
pageToken = response.nextPageToken;
} catch (e) {
Logger.log('Error fetching users in OU: ' + ouPath + ' - ' + e.message);
return []; // Or handle the error as needed
}
} while (pageToken);
return usersInOU.map(function(user) {
return {
fullName: user.name.fullName,
email: user.primaryEmail,
orgUnitPath: user.orgUnitPath
};
});
}