fix bug in validation

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-03-24 22:10:17 +00:00
parent 33deb01b74
commit bd845b47f0
2 changed files with 5 additions and 3 deletions

4
db.js
View File

@ -25,10 +25,12 @@ function init () {
function requestResources (user, resources) { function requestResources (user, resources) {
let approved = true; let approved = true;
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
if(!db[user][element]) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it if(!(element in db[user])) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it
console.log("a")
approved = false; approved = false;
} }
else if (db[user][element] - resources[element] < 0) { else if (db[user][element] - resources[element] < 0) {
console.log("b")
approved = false; approved = false;
} }
}); });

View File

@ -262,8 +262,8 @@ app.post("/api/resources", async (req, res) => {
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken); let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
let request = { let request = {
cores: Number(req.body.cores) - currentConfig.data.data.cores, cores: Number(req.body.cores) - Number(currentConfig.data.data.cores),
memory: Number(req.body.memory) - currentConfig.data.data.memory memory: Number(req.body.memory) - Number(currentConfig.data.data.memory)
}; };
if (!requestResources(req.cookies.username, request)) { // check resource approval if (!requestResources(req.cookies.username, request)) { // check resource approval
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}}); res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});