From 1bf89e62e796979a0c0b6e40b46aece388356d32 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Sun, 15 Dec 2024 19:06:06 -0500 Subject: [PATCH] Updated permissions validator to dynamically generate validation schema from DB schema. --- src/validators/permissionsValidator.js | 175 ++++++++----------------- 1 file changed, 58 insertions(+), 117 deletions(-) diff --git a/src/validators/permissionsValidator.js b/src/validators/permissionsValidator.js index 0036e53..ef619a2 100644 --- a/src/validators/permissionsValidator.js +++ b/src/validators/permissionsValidator.js @@ -18,134 +18,75 @@ along with this program. If not, see .*/ 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; } -module.exports.permissionsValidator = { - permissionsMap: () => checkSchema({ - 'permissionsMap.adminPanel': { +//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 - }, - }, - '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 - }, + } } - }) + }); + + //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 = { + //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()) } \ No newline at end of file