-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstartup.js
36 lines (32 loc) · 1011 Bytes
/
startup.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
const shortid = require("shortid");
const constants = require("./constants");
const models = require("./db/models");
const redis = require("./redis");
module.exports = async function () {
await redis.clearPermissionCache();
for (let groupName in constants.defaultGroups) {
let groupInfo = constants.defaultGroups[groupName];
let group = await models.Group.findOne({ name: new RegExp(`^${groupName}$`, "i") })
.select("_id");
let permissions = [];
if (Array.isArray(groupInfo.perms))
permissions = groupInfo.perms;
else if (groupInfo.perms == "*")
permissions = Object.keys(constants.allPerms);
if (!group) {
group = new models.Group({
id: shortid.generate(),
name: groupName,
rank: groupInfo.rank,
permissions: permissions,
visible: groupInfo.visible
});
await group.save();
}
else
await models.Group.updateOne(
{ _id: group._id },
{ $addToSet: { permissions: { $each: permissions } } }
).exec();
}
};