2020-05-27 18:23:01 +02:00
|
|
|
const {
|
|
|
|
adminPermissions,
|
|
|
|
ADMIN_LEVEL_ID,
|
|
|
|
POWERUSER_LEVEL_ID,
|
2020-06-18 17:59:31 +02:00
|
|
|
BUILDER_LEVEL_ID,
|
2020-05-27 18:23:01 +02:00
|
|
|
BUILDER,
|
|
|
|
} = require("../utilities/accessLevels")
|
|
|
|
|
|
|
|
module.exports = (permName, getItemId) => async (ctx, next) => {
|
|
|
|
if (!ctx.isAuthenticated) {
|
|
|
|
ctx.throw(403, "Session not authenticated")
|
|
|
|
}
|
|
|
|
|
2020-06-18 17:59:31 +02:00
|
|
|
if (!ctx.user) {
|
|
|
|
ctx.throw(403, "User not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.user.accessLevel._id === BUILDER_LEVEL_ID) {
|
2020-10-08 18:34:41 +02:00
|
|
|
return next()
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (permName === BUILDER) {
|
|
|
|
ctx.throw(403, "Not Authorized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const permissionId = ({ name, itemId }) => name + (itemId ? `-${itemId}` : "")
|
|
|
|
|
|
|
|
if (ctx.user.accessLevel._id === ADMIN_LEVEL_ID) {
|
2020-10-08 18:34:41 +02:00
|
|
|
return next()
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const thisPermissionId = permissionId({
|
|
|
|
name: permName,
|
|
|
|
itemId: getItemId && getItemId(ctx),
|
|
|
|
})
|
|
|
|
|
|
|
|
// power user has everything, except the admin specific perms
|
|
|
|
if (
|
|
|
|
ctx.user.accessLevel._id === POWERUSER_LEVEL_ID &&
|
|
|
|
!adminPermissions.map(permissionId).includes(thisPermissionId)
|
|
|
|
) {
|
2020-10-08 18:34:41 +02:00
|
|
|
return next()
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
ctx.user.accessLevel.permissions
|
|
|
|
.map(permissionId)
|
|
|
|
.includes(thisPermissionId)
|
|
|
|
) {
|
2020-10-08 18:34:41 +02:00
|
|
|
return next()
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.throw(403, "Not Authorized")
|
|
|
|
}
|