Updated permissions validator to dynamically generate validation schema from DB schema.

This commit is contained in:
rainbow napkin 2024-12-15 19:06:06 -05:00
parent 58b6d18f26
commit 1bf89e62e7

View file

@ -18,134 +18,75 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const { check, body, checkSchema, checkExact} = require('express-validator');
//local imports
const permissionSchema = require("../schemas/permissionSchema");
const permissionModel = require("../schemas/permissionSchema");
const channelPermissionSchema = require("../schemas/channel/channelPermissionSchema");
module.exports.isRank = function(value){
rankVal = permissionSchema.rankToNum(value);
rankVal = permissionModel.rankToNum(value);
return rankVal != -1;
}
//Internal functions for loading validator schema through the database so we only have to maintain permissions in one place
function loadPermValidatorSchema(){
//Pull permissions keys
var tempPerms = permissionModel.schema.tree;
//Create empty object for schema
var schema = {};
//Scrape out gunk
delete tempPerms.id;
delete tempPerms._id;
delete tempPerms.__v;
delete tempPerms.channelOverrides;
//For each object in the temporary permissions object
Object.keys(tempPerms).forEach((key) => {
//Create an entry in the validation schema for the current permission
schema[`permissionsMap.${key}`] = {
optional: true,
custom: {
options: module.exports.isRank
}
}
});
//return the auto-generated schema
return schema;
}
function loadChanPermValidatorSchema(){
//Pull permissions keys
var tempPerms = channelPermissionSchema.tree;
//Create empty object for schema
var schema = {};
//Scrape out gunk
delete tempPerms.id;
delete tempPerms._id;
delete tempPerms.__v;
//For each object in the temporary permissions object
Object.keys(tempPerms).forEach((key) => {
//Create an entry in the validation schema for the current permission
schema[`channelPermissionsMap.${key}`] = {
optional: true,
custom: {
options: module.exports.isRank
}
}
});
//return the schema
return schema;
}
module.exports.permissionsValidator = {
permissionsMap: () => checkSchema({
'permissionsMap.adminPanel': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.changeRank': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.changePerms': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.announce': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.editTokeCommands': {
optional: true,
custom: {
options: module.exports.isRank
}
},
'permissionsMap.banUser': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.nukeUser': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.genPasswordReset': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'permissionsMap.registerChannel': {
optional: true,
custom: {
options: module.exports.isRank
},
}
})
//Load validator from DB Schema
permissionsMap: () => checkSchema(loadPermValidatorSchema())
}
module.exports.channelPermissionValidator = {
channelPermissionsMap: () => checkSchema({
'channelPermissionsMap.manageChannel': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.changeRank': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.changePerms': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.changeSettings': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.kickUser': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.banUser': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.announce': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.clearChat': {
optional: true,
custom: {
options: module.exports.isRank
},
},
'channelPermissionsMap.editTokeCommands': {
optional: true,
custom: {
options: module.exports.isRank
}
},
'channelPermissionsMap.deleteChannel': {
optional: true,
custom: {
options: module.exports.isRank
},
}
})
//Load validator from DB Schema
channelPermissionsMap: () => checkSchema(loadChanPermValidatorSchema())
}