-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
36 lines (34 loc) · 1.27 KB
/
helpers.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 settings = require('./settings.json')
exports.checkAdminRights = function(req, res, next){
/*
Returns:
A function which checks if a user session has admin rights.
Admin rights are decided by settings.json where the allowed
admin users are specified, or by dev_mode settings.
*/
const dev_mode_and_admin = (settings.dev_mode && settings.dev_mode_is_admin)
const user_is_admin = settings.admin_users.includes(req.session[settings.session_name])
if (dev_mode_and_admin || user_is_admin){
// User is permitted.
next()
}
else {
res.status(403)
res.json({
status: 403,
ok: false,
'message':'Not permitted, only admins can access this resource. Contact [email protected] if you believe this was a mistake.'
})
}
}
exports.validCharacters = function(filename){
// Returns true if the filename contains only whitelist characters, false otherwise.
const whitelist = ".abcdefghijklmnopqrstuvwxyzåäöøæ0123456789-_"
const name = filename.toLowerCase()
let i
for (i=0; i < filename.length; i++){
if (whitelist.includes(name[i])) continue
else return false
}
return true
}