-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathUpdateUsers.js
94 lines (85 loc) · 3.97 KB
/
UpdateUsers.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
javascript:
/*
Bookmark name: /Update Users#
https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/ImportGroupsFromCSV.js
Setup:
1. Show your bookmarks toolbar. In Chrome, ... > Bookmarks > Show Bookmarks Bar. In Firefox/IE, right-click in the title bar and click Bookmarks Toolbar.
2. Drag all this to the bookmark toolbar to create a Bookmarklet.
Or, copy/paste this code to the browser Console, or, if using Chrome, to a Snippet:
1. Press F12 (Windows) to open DevTools.
2. Go to Sources > Snippets, click New Snippet.
3. Give it a name, eg, "UpdateUsers.js".
4. Copy/paste this code.
5. Save (Ctrl+S, Windows).
Usage:
1. Navigate your browser to your Okta admin console.
2. Run the code. Click the Bookmarklet, or if using a Snippet, Press F12 (Windows) to open DevTools, there's a Run button on the bottom right, or press Ctrl+Enter (Windows).
3. Look for the popup window in the upper-left corner of your browser.
Sample CSV. Header row is required and case-sensitive:
id,login,email
00u14w0...,[email protected],[email protected]
*/
(function () {
const popup = createPopup("Update Users");
const users = [];
$('<input type="file">')
.appendTo(popup)
.change(function () {
const reader = new FileReader();
reader.onload = () => parseFile(reader.result);
reader.readAsText(this.files[0]);
});
function parseFile(file) {
const lineSeparator = /\r\n|\r|\n/;
const fieldSeparator = ",";
const lines = file.split(lineSeparator);
const fields = lines.shift().split(fieldSeparator);
const headers = Object.fromEntries(fields.map((val, i) => [val, i])); /* Map header name to number. */
lines.forEach(line => {
if (line == '') return;
const fields = line.split(fieldSeparator);
const user = {
id: fields[headers.id],
profile: {
login: fields[headers.login],
email: fields[headers.email]
}
};
users.push(user);
});
updateUser(0);
}
function updateUser(i) {
popup.html("Updating " + users[i].profile.login + " (" + (i + 1) + " of " + users.length + ")");
$.post({
url: `/api/v1/users/${users[i].id}`,
data: JSON.stringify(users[i]),
contentType: "application/json"
}).then(function (user, status, jqXHR) {
if (++i < users.length) {
const remaining = jqXHR.getResponseHeader("X-Rate-Limit-Remaining");
if (remaining && remaining <= 10) {
popup.html("Sleeping...");
const intervalID = setInterval(() => {
if ((new Date()).getTime() / 1000 > jqXHR.getResponseHeader("X-Rate-Limit-Reset")) {
clearInterval(intervalID);
updateUser(i);
}
}, 1000);
} else {
updateUser(i);
}
} else {
popup.html("Updated " + users.length + " users. Done.");
}
}).fail(jqXHR => popup.html("Error on line " + (i + 1) + ": " + jqXHR.responseJSON.errorCauses[0].errorSummary));
}
function createPopup(title) {
const popup = $(`<div style='position: absolute; z-index: 1000; top: 0px; max-height: calc(100% - 28px); max-width: calc(100% - 28px); padding: 8px; margin: 4px; overflow: auto; ` +
`background-color: white; border: 1px solid #ddd;'>` +
`${title}<div style='display: block; float: right;'><a target='_blank' href='https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/UpdateUsers.js' rel='noopener' style='padding: 4px'>?</a> ` +
`<a class=close style='cursor: pointer; padding: 4px'>X</a></div><br><br></div>`).appendTo(document.body);
popup.find('a.close').click(() => popup.remove());
return $("<div></div>").appendTo(popup);
}
})();