canopy/src/validators/permissionsValidator.js

92 lines
2.9 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const { check, body, checkSchema, checkExact} = require('express-validator');
//local imports
const permissionModel = require("../schemas/permissionSchema");
const channelPermissionSchema = require("../schemas/channel/channelPermissionSchema");
module.exports.isRank = function(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 permTree = permissionModel.schema.tree;
//Create empty object for schema
var schema = {};
//Scrape out gunk
delete permTree.id;
delete permTree._id;
delete permTree.__v;
delete permTree.channelOverrides;
//For each object in the temporary permissions object
for(let key of Object.keys(permTree)){
//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 permTree = channelPermissionSchema.tree;
//Create empty object for schema
var schema = {};
//Scrape out gunk
delete permTree.id;
delete permTree._id;
delete permTree.__v;
//For each object in the temporary permissions object
for(let key of Object.keys(permTree)){
//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 = {
//Load validator from DB Schema
channelPermissionsMap: () => checkSchema(loadChanPermValidatorSchema())
}