diff --git a/src/schemas/permissionSchema.js b/src/schemas/permissionSchema.js index 1fc2bc9..1f40309 100644 --- a/src/schemas/permissionSchema.js +++ b/src/schemas/permissionSchema.js @@ -84,29 +84,40 @@ permissionSchema.statics.rankToNum = function(rank){ } permissionSchema.statics.permCheck = async function(user, perm){ + //Get permission list const perms = await this.getPerms(); + //Set user to anon rank if no rank was found for the given user + if(user == null || user.rank == null){ + user ={ + rank: "anon" + }; + } + + console.log(user.rank); + + //Check if this permission exists if(perms[perm] != null){ + //if so get required rank as a number requiredRank = this.rankToNum(perms[perm]) + //if so get user rank as a number userRank = user ? this.rankToNum(user.rank) : 0; + //return whether or not the user is equal to or higher than the required rank for this permission return (userRank >= requiredRank); }else{ + //if not scream and shout throw new Error(`Permission check '${perm}' not found!`); } } permissionSchema.statics.reqPermCheck = function(perm){ return async (req, res, next)=>{ - if(req.session.user){ - if(await permissionSchema.statics.permCheck(req.session.user, perm)){ - next(); - }else{ - res.status(401); - res.send({error:`You do not have a high enough rank to access this resource.`}); - } + + if(await permissionSchema.statics.permCheck(req.session.user, perm)){ + next(); }else{ res.status(401); - res.send({error:`You must login to access this resource.`}); + res.send({error:`You do not have a high enough rank to access this resource.`}); } } }