diff --git a/nodejs/utils/permission.js b/nodejs/utils/permission.js new file mode 100644 index 0000000..0c7569b --- /dev/null +++ b/nodejs/utils/permission.js @@ -0,0 +1,22 @@ +'use static'; + +const {Group} = require('../models/group_ldap'); + +let byGroup = async function(user, groups){ + for(let group of groups){ + try{ + group = await Group.get(group); + if(group.member.includes(user.dn)) return true + }catch(error){ + throw error; + } + + let error = new Error('Insufficient Permission'); + error.name = 'Insufficient Permission'; + error.message = `You do not have permission to perform this action.`; + error.status = 401; + throw error; + } +} + +module.exports = {byGroup};