-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.js
More file actions
66 lines (63 loc) · 1.77 KB
/
sync.js
File metadata and controls
66 lines (63 loc) · 1.77 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
const {iterableSearch} = require("./util");
async function syncUser(db, user) {
const id = user.attributes
.find((attribute) => attribute.type == "ipaUniqueID")
._vals[0].toString("utf8");
let memberOf = user.attributes
.find((attribute) => attribute.type == "memberOf");
if (!memberOf)
return;
const document = {
groups: memberOf._vals.map((value) => value.toString("utf8")),
disabled:
user.attributes
.find((attribute) => attribute.type == "nsAccountLock")
?._vals[0].toString("utf8")
.toLowerCase() == "true",
};
await db.collection("users").updateOne(
{
id: {
$eq: id,
},
},
{
$set: document,
},
{upsert: true}
);
return {...document, id};
}
async function syncUsers(db) {
console.log("Running sync job!");
const cursor = await iterableSearch(
"cn=users,cn=accounts,dc=csh,dc=rit,dc=edu",
{
// filter: "(uid)",
scope: "one", // one level under user DN (no need to recurse)
paged: true,
timeLimit: 60 * 30, // 30 minutes
attributes: ["memberOf", "ipaUniqueID", "nsAccountLock"],
sizeLimit: 0, // unlimited
}
);
console.log(cursor);
let userCount = 0;
const promises = [];
// Some day these should be batched...
// Maybe we could have a map of Group[] => User[] and use `updateMany`?
// This won't let us upsert, but that should be okay because enroll will fix it?
for await (const user of cursor) {
if (!user.attributes.find((attribute) => attribute.type == "ipaUniqueID")) {
console.log("Missing attributes!", user);
continue;
}
promises.push(syncUser(db, user));
}
await Promise.all(promises);
console.log(`Synced ${promises.length} users!`);
}
module.exports = {
syncUsers,
syncUser,
};