-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroups.js
78 lines (73 loc) · 3.31 KB
/
Groups.js
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
function createGroupForm (groups, form) {
if (!form) {
form = FormApp.create("Google Groups").setTitle("Google Groups")
.setCollectEmail(true);
form.addTextItem()
.setTitle("Username")
.setHelpText("Name of User Who Will be Added to Groups");
Logger.log('createGroupForm=>'+form.getPublishedUrl());
}
form.addSectionHeaderItem()
.setTitle("Groups");
var cb = form.addCheckboxItem()
.setTitle('Add to Google Groups')
.setChoiceValues(groups);
return form;
}
function addToGroupFromForm (results, groupSettings) {
if (! checkAuthorization(results,groupSettings)) {
Logger.log('Unauthorized use attempted.')
return false;
}
var fields = lookupFields(groupSettings, results);
Logger.log('addToGroupFromForm got fields=>%s',shortStringify(fields));
var groupsAdded = addToGroups(fields.username,fields.groups);
return {'user':fields.username,'groups':groupsAdded,'settings':groupSettings}
}
function addToGroups (username, groupEmails) {
var added = [];
groupEmails.forEach(function (groupEmail) {
try {AdminDirectory.Members.insert({email:username,role:'MEMBER'},groupEmail);
logNormal('Inserted '+username+' into group '+groupEmail);
added.push(groupEmail)
}
catch (err) {
logVerbose('Error adding user - already exists? '+err);
if (err!="Exception: Member already exists.") {
logAlways('No, some other error: %s',err);
emailError('Error adding '+username+' to group %s'+groupEmail,err,{'subject':'Script error adding user to group'});
//throw err;
}
else {
added.push(groupEmail); // we record it if it already existed!
}
}
});
return added
}
function testEmailAndAddToGroups () {
createAccount(
{username:'[email protected]',
first:'Fake',
last:'Faculty',
informList:['[email protected],[email protected]'],
fields:{'orgUnitPath':'/Staff',},
});
addToGroups('[email protected]',
]
);
}
function testAddBunchOfGroups () {
var result = addToGroups('[email protected]',
]
);
Logger.log('addToGroups=>%s',result);
}
function testCreateGroupForm () {
createGroupForm(['[email protected]',
'[email protected]']);
}