Fixed "anon" rank in perm checks.
This commit is contained in:
parent
ce34d2e4d7
commit
6350963e77
|
|
@ -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.`});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue