Something went wrong. Try again.
An all-in-one API for unified control and management over every facet of Inovus' digital ecosystem api.inovuslabs.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
const User = require('../models/users');const Role = require('../models/roles');
const checkPermission = (permissions, mode = 'any') => async (req, res, next) => { try { const email = req.user.email; const user = await User.findOne({ email: email });
// Check if user exists if (!user) { return res.status(400).json({ status: 400, message: 'User not found' }); }
// Check if user has permission const roles = user.roles; const rolePermissions = await Role.find({ name: { $in: roles } });
let hasPermission = false; rolePermissions.forEach(role => { role.permissions.forEach(permission => { if (permissions.includes(permission)) { hasPermission = true; } }); });
// Check if user has all permissions or any permission if (mode === 'all') { hasPermission = permissions.every(permission => rolePermissions.some(role => role.permissions.includes(permission))); } else if (mode === 'any') { hasPermission = permissions.some(permission => rolePermissions.some(role => role.permissions.includes(permission))); } else { return res.status(400).json({ status: 400, message: 'Invalid permission mode' }); }
// If user has permission, continue if (hasPermission) { return next(); } // If user does not have permission, return unauthorized return res.status(401).json({ status: 401, message: 'You do not have permission to perform this action' }); } catch (err) { return res.status(400).json({ status: 400, message: 'Error checking permissions', error: err }); }}
module.exports = checkPermission;