From 8d295550f9aafc5a46bdd8f0d0a2a88dadad52ab Mon Sep 17 00:00:00 2001 From: DarkPhoenix2704 Date: Tue, 3 Sep 2024 09:09:09 +0000 Subject: [PATCH] fix: webhook validation for user fields --- packages/nocodb/src/helpers/webhookHelpers.ts | 203 +++++++++++------- 1 file changed, 121 insertions(+), 82 deletions(-) diff --git a/packages/nocodb/src/helpers/webhookHelpers.ts b/packages/nocodb/src/helpers/webhookHelpers.ts index 3218849956..62fe9e730c 100644 --- a/packages/nocodb/src/helpers/webhookHelpers.ts +++ b/packages/nocodb/src/helpers/webhookHelpers.ts @@ -227,88 +227,127 @@ export async function validateCondition( break; } - switch (filter.comparison_op) { - case 'eq': - res = val == filter.value; - break; - case 'neq': - res = val != filter.value; - break; - case 'like': - res = - data[field] - ?.toString?.() - ?.toLowerCase() - ?.indexOf(filter.value?.toLowerCase()) > -1; - break; - case 'nlike': - res = - data[field] - ?.toString?.() - ?.toLowerCase() - ?.indexOf(filter.value?.toLowerCase()) === -1; - break; - case 'empty': - case 'blank': - res = - data[field] === '' || - data[field] === null || - data[field] === undefined; - break; - case 'notempty': - case 'notblank': - res = !( - data[field] === '' || - data[field] === null || - data[field] === undefined - ); - break; - case 'checked': - res = !!data[field]; - break; - case 'notchecked': - res = !data[field]; - break; - case 'null': - res = res = data[field] === null; - break; - case 'notnull': - res = data[field] !== null; - break; - case 'allof': - res = ( - filter.value?.split(',').map((item) => item.trim()) ?? [] - ).every((item) => (data[field]?.split(',') ?? []).includes(item)); - break; - case 'anyof': - res = ( - filter.value?.split(',').map((item) => item.trim()) ?? [] - ).some((item) => (data[field]?.split(',') ?? []).includes(item)); - break; - case 'nallof': - res = !( - filter.value?.split(',').map((item) => item.trim()) ?? [] - ).every((item) => (data[field]?.split(',') ?? []).includes(item)); - break; - case 'nanyof': - res = !( - filter.value?.split(',').map((item) => item.trim()) ?? [] - ).some((item) => (data[field]?.split(',') ?? []).includes(item)); - break; - case 'lt': - res = +data[field] < +filter.value; - break; - case 'lte': - case 'le': - res = +data[field] <= +filter.value; - break; - case 'gt': - res = +data[field] > +filter.value; - break; - case 'gte': - case 'ge': - res = +data[field] >= +filter.value; - break; + if ( + [UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy].includes( + column.uidt, + ) + ) { + const userIds = Array.isArray(data[field]) + ? data[field].map((user) => user.id) + : data[field]?.id + ? [data[field].id] + : []; + + const filterValues = filter.value.split(',').map((v) => v.trim()); + + switch (filter.comparison_op) { + case 'anyof': + res = userIds.some((id) => filterValues.includes(id)); + break; + case 'nanyof': + res = !userIds.some((id) => filterValues.includes(id)); + break; + case 'allof': + res = filterValues.every((id) => userIds.includes(id)); + break; + case 'nallof': + res = !filterValues.every((id) => userIds.includes(id)); + break; + case 'empty': + case 'blank': + res = userIds.length === 0; + break; + case 'notempty': + case 'notblank': + res = userIds.length > 0; + break; + default: + res = false; // Unsupported operation for User fields + } + } else { + switch (filter.comparison_op) { + case 'eq': + res = val == filter.value; + break; + case 'neq': + res = val != filter.value; + break; + case 'like': + res = + data[field] + ?.toString?.() + ?.toLowerCase() + ?.indexOf(filter.value?.toLowerCase()) > -1; + break; + case 'nlike': + res = + data[field] + ?.toString?.() + ?.toLowerCase() + ?.indexOf(filter.value?.toLowerCase()) === -1; + break; + case 'empty': + case 'blank': + res = + data[field] === '' || + data[field] === null || + data[field] === undefined; + break; + case 'notempty': + case 'notblank': + res = !( + data[field] === '' || + data[field] === null || + data[field] === undefined + ); + break; + case 'checked': + res = !!data[field]; + break; + case 'notchecked': + res = !data[field]; + break; + case 'null': + res = res = data[field] === null; + break; + case 'notnull': + res = data[field] !== null; + break; + case 'allof': + res = ( + filter.value?.split(',').map((item) => item.trim()) ?? [] + ).every((item) => (data[field]?.split(',') ?? []).includes(item)); + break; + case 'anyof': + res = ( + filter.value?.split(',').map((item) => item.trim()) ?? [] + ).some((item) => (data[field]?.split(',') ?? []).includes(item)); + break; + case 'nallof': + res = !( + filter.value?.split(',').map((item) => item.trim()) ?? [] + ).every((item) => (data[field]?.split(',') ?? []).includes(item)); + break; + case 'nanyof': + res = !( + filter.value?.split(',').map((item) => item.trim()) ?? [] + ).some((item) => (data[field]?.split(',') ?? []).includes(item)); + break; + case 'lt': + res = +data[field] < +filter.value; + break; + case 'lte': + case 'le': + res = +data[field] <= +filter.value; + break; + case 'gt': + res = +data[field] > +filter.value; + break; + case 'gte': + case 'ge': + res = +data[field] >= +filter.value; + break; + } } } }