-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDemo.gs
336 lines (277 loc) · 9.17 KB
/
UserDemo.gs
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
//OAuth 2 lib = 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
const DOMAIN = "-api.toolsquare.io";
const OauthURL = "/o/authorize/";
const OauthTokenURL = "/o/token/";
const USERS_GET_URL = '/open-api/v1/users/user/';
const USERS_CREATE_URL = '/api/user_core/user/bulkcreate/';
const USER_GROUPS_URL = '/open-api/v1/groups/usergroup/';
const USERS_UPDATE_URL = '/open-api/v1/users/user/bulkupdate/';
const USERS_DESTROY_URL = '/open-api/v1/users/user/bulkdestroy/';
let theProps = PropertiesService.getDocumentProperties();
let NrOfNewUsers = 0;
let NrOfCalls = 0;
const PageSize = 100;
//UI
function onOpen() {
//Add functions to menu
let ui = SpreadsheetApp.getUi();
ui.createMenu('Toolsquare')
//.addItem('Create User Template', 'CreateUserRegistrationSheet')
.addItem('Create User Template', 'createUserTemplate')
.addItem('Create Users', 'createUsers')
.addItem('Delete all credentials', 'deleteAll')
.addToUi()
}
function getUrl(slug) {
let tenant = theProps.getProperty('CLIENT_TENANT');
return "https://" + tenant + DOMAIN + slug;
}
function getUsers() {
let pageIndex = 1;
let nextResults = true;
var users = [];
while (nextResults) {
Logger.log("# retrieving users page " + pageIndex);
let results = getTSData(USERS_GET_URL, pageIndex).results;
if (results != null) {
pageIndex++;
for (var n = 0; n < results.length; n++) {
users.push(results[n]);
}
} else {
Logger.log("# users = " + users.length);
nextResults = false;
}
}
return users;
}
function getUserGroups() {
let usergroups = getTSData(USER_GROUPS_URL, 1);
Logger.log(JSON.stringify(usergroups, null, 2));
return usergroups.results;
}
function createUsers() {
let sheet = null;
let users = [];
let usersWithgroups = [];
doc = SpreadsheetApp.getActiveSheet();
sheet = SpreadsheetApp.getActiveSheet();
let lastRow = sheet.getLastRow();
let userRange = "A2:D" + lastRow;
let userData = sheet.getRange(userRange).getValues();
for (let i = 0; i < userData.length; ++i) {
let u_object = {};
let ug_object = {};
let fName = userData[i][0];
let lName = userData[i][1];
let email = userData[i][2];
let userGroup = userData[i][3];
var re = /\S+@\S+\.\S+/;
if (re.test(email)) {
u_object.first_name = fName;
u_object.last_name = lName;
u_object.email = email;
ug_object.first_name = fName;
ug_object.last_name = lName;
ug_object.email = email;
ug_object.usergroup = userGroup;
users.push(u_object);
usersWithgroups.push(ug_object)
}
}
Logger.log(JSON.stringify(users));
let result = apiTSData(USERS_CREATE_URL, users, 'post');
let responsecode = result.getResponseCode().toString();
Logger.log("Users created response: " + responsecode);
let responsetext = JSON.parse(result.getContentText());
NrOfNewUsers = responsetext.data.length;
Logger.log(NrOfNewUsers);
updateUserData(usersWithgroups);
}
function updateUserData(theUpdateList, allUsers = null) {
if (allUsers == null) {
allUsers = getUsers();
}
let usergroups = getUserGroups();
let changelist = [];
for (e in theUpdateList) {
let updateemail = theUpdateList[e].email;
updateemail = updateemail.toLowerCase();
Logger.log(updateemail);
for (i in allUsers) {
if (updateemail == allUsers[i].email) {
let object = {};
object.first_name = theUpdateList[e].first_name;
object.last_name = theUpdateList[e].last_name;
object.id = allUsers[i].id;
object.usergroup = getUserGroupID(usergroups, theUpdateList[e].usergroup);
changelist.push(object);
}
}
}
Logger.log(changelist);
let result = apiTSData(USERS_UPDATE_URL, changelist, "PATCH");
let responsecode = result.getResponseCode().toString();
Logger.log("Users updated response: " + responsecode);
let responsetext = JSON.parse(result.getContentText());
let NrOfUsersUpdated = responsetext.data.length;
Logger.log(NrOfUsersUpdated);
let ui = SpreadsheetApp.getUi();
ui.alert("There are " + NrOfNewUsers + " created and " + NrOfUsersUpdated + " updated");
}
function getUserGroupID(theusergroups, thegroupname) {
for (e in theusergroups) {
if (theusergroups[e].name === thegroupname) {
return (theusergroups[e].id);
}
}
}
function getService() {
//get check credentials
let C_TENANT = theProps.getProperty('CLIENT_TENANT');
if (C_TENANT === null) {
setKey_('CLIENT_TENANT', 'tenant');
C_TENANT = theProps.getProperty('CLIENT_TENANT');
}
let C_ID = theProps.getProperty('CLIENT_KEY');
if (C_ID === null) {
setKey_('CLIENT_KEY', 'client id');
C_ID = theProps.getProperty('CLIENT_KEY');
}
let C_SECRET = theProps.getProperty('CLIENT_SECRET');
if (C_SECRET === null) {
setKey_('CLIENT_SECRET', 'client secret');
C_SECRET = theProps.getProperty('CLIENT_SECRET');
}
let authURL = getUrl(OauthURL);
let tokenURL = getUrl(OauthTokenURL);
return OAuth2.createService('TSUsers')
.setAuthorizationBaseUrl(authURL)
.setTokenUrl(tokenURL)
.setClientId(C_ID)
.setClientSecret(C_SECRET)
.setCallbackFunction('oauthCallback')
.setPropertyStore(theProps)
.setScope('read')
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(C_ID + ':' + C_SECRET),
'Content-Type': 'application/x-www-form-urlencoded'
})
.setCache(CacheService.getUserCache());
}
function getTSData(theURL, pageNr = 1) {
NrOfCalls++;
let service = getService();
let isConnected = checkAccess(service);
//Access granted
if (isConnected) {
let url = getUrl(theURL) + '?page=' + pageNr + '&page_size=' + PageSize;
let response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
muteHttpExceptions: true
});
let result = JSON.parse(response.getContentText());
return result;
}
}
function apiTSData(theURL, theContent, method) {
NrOfCalls++;
let service = getService();
let isConnected = checkAccess(service);
//Access granted
if (isConnected) {
Logger.log(theURL + " " + method);
// let tenant = PropertiesService.getUserProperties('CLIENT_TENANT');
let url = getUrl(theURL);
let response = UrlFetchApp.fetch(url, {
method: method,
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
contentType: 'application/json',
payload: JSON.stringify(theContent),
muteHttpExceptions: false
});
Logger.log(response);
return response;
}
return "is not connected";
}
function checkAccess(theService) {
//Access granted
if (theService.hasAccess()) {
return true;
//Access not yet granted
} else {
Logger.log("OAuth connection error!");
return resetAccess(theService);
}
}
function oauthCallback(request) {
var service = getService();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
function resetAccess(theService) {
var authorizationUrl = theService.getAuthorizationUrl();
var htmlOutput = HtmlService
.createHtmlOutput('<p style="font-family:verdana"> Before you\'ll be able get date form the platform, you\'ll have to open <a href="' + authorizationUrl + '">this link</a>. <br><br> You should login into the Toolsquare platform and rerun the request.<br><br>The red bar on top should disapear on the next run</p>')
.setWidth(400) //optional
.setHeight(200); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Authorization request');
//Logger.log('Open the following URL and re-run the script: %s',authorizationUrl);
return false;
}
function createUserTemplate() {
doc = SpreadsheetApp.getActiveSpreadsheet();
let sheet = doc.insertSheet();
let user_range = "A1:E10"; //Increase if needed
let rowlength = 10
let userdatalist = new Array(rowlength);
for (var i = 0; i < userdatalist.length; i++) {
userdatalist[i] = new Array(5);
}
//Table Headers
userdatalist[0][0] = "First Name";
userdatalist[0][1] = "Last Name";
userdatalist[0][2] = "Email";
userdatalist[0][3] = "User Group";
sheet.getRange(user_range).setValues(userdatalist);
setusergroupvalidation(sheet, rowlength);
}
function setusergroupvalidation(theSheet, theLength) {
let usergroupsResult = getTSData(USER_GROUPS_URL);
let usergroups = usergroupsResult.results;
let grouplist = [];
for (e in usergroups) {
grouplist.push(usergroups[e].name);
}
let range = "D2:D" + theLength;
let usergrouprange = theSheet.getRange(range);
let groupRule = SpreadsheetApp.newDataValidation().requireValueInList(grouplist);
usergrouprange.setDataValidation(groupRule);
}
/**
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}
function setKey_(KEY, Promt) {
let ui = SpreadsheetApp.getUi();
var scriptValue = ui.prompt('Please provide your ' + Promt, ui.ButtonSet.OK);
let value = scriptValue.getResponseText();
if (value.length > 1) {
theProps.setProperty(KEY, value);
}
}
function deleteAll() {
theProps.deleteAllProperties();
CacheService.getDocumentCache();
}