-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
36 lines (28 loc) · 946 Bytes
/
util.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 morgan = require('morgan');
const isSuperAdmin = (req = {}) => {
if (!req.session || !req.session.user || !req.session.user.perms)
return false;
let perms = req.session.user.perms || [];
for (let p of perms) {
if (p && (p.toUpperCase() === 'WEBAPP SUPER ADMIN' || p.toUpperCase() === 'TIER 3 SUPPORT'))
return true;
}
return false;
};
const getUserID = (req = {}) => {
if (!req.session || !req.session.user) {
return 'Unknown';
}
return req.session?.user?.id || req.get('SSL_CLIENT_S_DN_CN');
}
const attachHttpLogging = (app) => {
morgan.token('sessionUserId', function getId(req) {
return req.session?.user?.id || 'userIdFallback';
});
app.use(
morgan(
':remote-addr - :sessionUserId :date[iso] ":method :url HTTP/:http-version" :req[SSL_CLIENT_S_DN_CN] :status :res[content-length] :referrer :user-agent - :response-time ms'
)
);
};
module.exports = { isSuperAdmin, getUserID, attachHttpLogging };