-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
expose.js
47 lines (38 loc) · 1.31 KB
/
expose.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
'use strict';
/**
* The middlewares here strictly act to "expose" certain values from the database,
* into `res.locals` for use in middlewares and/or controllers down the line
*/
const user = require('../user');
const privileges = require('../privileges');
const utils = require('../utils');
module.exports = function (middleware) {
middleware.exposeAdmin = async (req, res, next) => {
// Unlike `requireAdmin`, this middleware just checks the uid, and sets `isAdmin` in `res.locals`
res.locals.isAdmin = false;
if (!req.user) {
return next();
}
const isAdmin = await user.isAdministrator(req.user.uid);
res.locals.isAdmin = isAdmin;
return next();
};
middleware.exposePrivileges = async (req, res, next) => {
// Exposes a hash of user's ranks (admin, gmod, etc.)
const hash = await utils.promiseParallel({
isAdmin: user.isAdministrator(req.user.uid),
isGmod: user.isGlobalModerator(req.user.uid),
isPrivileged: user.isPrivileged(req.user.uid),
});
if (req.params.uid) {
hash.isSelf = parseInt(req.params.uid, 10) === req.user.uid;
}
res.locals.privileges = hash;
return next();
};
middleware.exposePrivilegeSet = async (req, res, next) => {
// Exposes a user's global privilege set
res.locals.privileges = await privileges.global.get(req.user.uid);
return next();
};
};