var roles = {
Guest: ["CAN_REGISTER"],
Player: ["CAN_LOGIN", "CAN_CHAT"],
Admin: ["CAN_KICK", "CAN_LOGIN", "CAN_CHAT"]
};
I have an object called 'roles' containing different permissions for each role, and I am implementing a function to check if a user has specific permissions.
get_perms: function(player, perm) {
let arrayLength = accounts.length;
let name = player.name;
if (arrayLength == 0 && (perm == "CAN_CHAT" || perm == "CAN_LOGIN" || perm == "CAN_KICK")){
return false;
}
for (var i = 0; i < arrayLength; i++)
{
if (accounts[i][0] == name)
{
for (var key in roles)
{
if (roles.hasOwnProperty(key))
{
if (accounts[i][2] == key){
if (roles[key] == perm){
for (var x = 0; x < roles[key].length; x++){
if (roles[key][x] == perm){
return true;
}
}
}
}
}
}
}
else{
return false;
}
}
}
The value of account[i][2]
represents the role of the player with the corresponding name. The main aim is to validate whether this role possesses the specific permission- denoted by the variable perm
that is passed as an argument to the function, for instance, "CHAT_PERMS"
.