apply validation

This commit is contained in:
Alex Holliday committed 2025-07-22 13:26:45 -07:00
1 parent 73f2f29c2a
commit 22826afd21
2 files changed
+53 -10

No files matched your search

+16 -3
View File
@@ -5,6 +5,10 @@ import {
recoveryValidation, recoveryValidation,
recoveryTokenValidation, recoveryTokenValidation,
newPasswordValidation, newPasswordValidation,
getUserByIdParamValidation,
editUserByIdParamValidation,
editUserByIdBodyValidation,
editSuperadminUserByIdBodyValidation,
} from "../validation/joi.js"; } from "../validation/joi.js";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js"; import { getTokenFromHeaders } from "../utils/utils.js";
@@ -396,6 +400,7 @@ class AuthController {
getUserById = asyncHandler( getUserById = asyncHandler(
async (req, res, next) => { async (req, res, next) => {
await getUserByIdParamValidation.validateAsync(req.params);
const userId = req?.params?.userId; const userId = req?.params?.userId;
const roles = req?.user?.role; const roles = req?.user?.role;
@@ -417,14 +422,22 @@ class AuthController {
editUserById = asyncHandler( editUserById = asyncHandler(
async (req, res, next) => { async (req, res, next) => {
const userId = req.params.userId;
const user = req.body;
const roles = req?.user?.role; const roles = req?.user?.role;
if (!roles.includes("superadmin")) { if (!roles.includes("superadmin")) {
throw createError("Unauthorized", 403); throw createError("Unauthorized", 403);
} }
const userId = req.params.userId;
const user = { ...req.body };
await editUserByIdParamValidation.validateAsync(req.params);
// If this is superadmin self edit, allow "superadmin" role
if (userId === req.user._id) {
await editSuperadminUserByIdBodyValidation.validateAsync(req.body);
} else {
await editUserByIdBodyValidation.validateAsync(req.body);
}
await this.db.editUserById(userId, user); await this.db.editUserById(userId, user);
return res.success({ msg: "ok" }); return res.success({ msg: "ok" });
}, },
+37 -7
View File
@@ -1,4 +1,5 @@
import joi from "joi"; import joi from "joi";
import { ROLES, VALID_ROLES } from "../../client/src/Utils/roleUtils.js";
//**************************************** //****************************************
// Custom Validators // Custom Validators
@@ -164,11 +165,7 @@ const createMonitorBodyValidation = joi.object({
.custom((value, helpers) => { .custom((value, helpers) => {
// 1. Standard URLs: must have protocol and pass canParse() // 1. Standard URLs: must have protocol and pass canParse()
if (/^(https?:\/\/)/.test(value)) { if (/^(https?:\/\/)/.test(value)) {
if ( if (typeof URL !== "undefined" && typeof URL.canParse === "function" && URL.canParse(value)) {
typeof URL !== "undefined" &&
typeof URL.canParse === "function" &&
URL.canParse(value)
) {
return value; return value;
} }
// else, it's a malformed URL with protocol // else, it's a malformed URL with protocol
@@ -197,8 +194,7 @@ const createMonitorBodyValidation = joi.object({
.messages({ .messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
"string.invalidUrl": "string.invalidUrl": "Please enter a valid URL, hostname, or container name (with optional port).",
"Please enter a valid URL, hostname, or container name (with optional port).",
}), }),
ignoreTlsErrors: joi.boolean().default(false), ignoreTlsErrors: joi.boolean().default(false),
port: joi.number(), port: joi.number(),
@@ -657,6 +653,36 @@ const sendTestEmailBodyValidation = joi.object({
systemEmailTLSServername: joi.string().allow("").optional(), systemEmailTLSServername: joi.string().allow("").optional(),
}); });
const getUserByIdParamValidation = joi.object({
userId: joi.string().required(),
});
const editUserByIdParamValidation = joi.object({
userId: joi.string().required(),
});
const editUserByIdBodyValidation = joi.object({
firstName: nameValidation.required(),
lastName: nameValidation.required(),
email: joi.string().email().required(),
role: joi
.array()
.items(joi.string().valid(...VALID_ROLES))
.min(1)
.required(),
});
const editSuperadminUserByIdBodyValidation = joi.object({
firstName: nameValidation.required(),
lastName: nameValidation.required(),
email: joi.string().email().required(),
role: joi
.array()
.items(joi.string().valid(...VALID_ROLES, ROLES.SUPERADMIN))
.min(1)
.required(),
});
export { export {
roleValidatior, roleValidatior,
loginValidation, loginValidation,
@@ -724,4 +750,8 @@ export {
webhookConfigValidation, webhookConfigValidation,
createAnnouncementValidation, createAnnouncementValidation,
sendTestEmailBodyValidation, sendTestEmailBodyValidation,
getUserByIdParamValidation,
editUserByIdParamValidation,
editUserByIdBodyValidation,
editSuperadminUserByIdBodyValidation,
}; };