Fixed "anon" rank in perm checks.

This commit is contained in:
rainbownapkin 2024-11-19 08:37:12 -05:00
parent ce34d2e4d7
commit 6350963e77

View file

@ -84,29 +84,40 @@ permissionSchema.statics.rankToNum = function(rank){
} }
permissionSchema.statics.permCheck = async function(user, perm){ permissionSchema.statics.permCheck = async function(user, perm){
//Get permission list
const perms = await this.getPerms(); 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(perms[perm] != null){
//if so get required rank as a number
requiredRank = this.rankToNum(perms[perm]) requiredRank = this.rankToNum(perms[perm])
//if so get user rank as a number
userRank = user ? this.rankToNum(user.rank) : 0; 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); return (userRank >= requiredRank);
}else{ }else{
//if not scream and shout
throw new Error(`Permission check '${perm}' not found!`); throw new Error(`Permission check '${perm}' not found!`);
} }
} }
permissionSchema.statics.reqPermCheck = function(perm){ permissionSchema.statics.reqPermCheck = function(perm){
return async (req, res, next)=>{ return async (req, res, next)=>{
if(req.session.user){
if(await permissionSchema.statics.permCheck(req.session.user, perm)){ if(await permissionSchema.statics.permCheck(req.session.user, perm)){
next(); next();
}else{
res.status(401);
res.send({error:`You do not have a high enough rank to access this resource.`});
}
}else{ }else{
res.status(401); 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.`});
} }
} }
} }