Browse Source

Fix: Remove user reference from webhook context (#2337)

* fix: remove user info from webhook handlebar context

Signed-off-by: Pranav C <pranavxc@gmail.com>

* docs: update webhook context variables docs

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2342/head
Pranav C 2 years ago committed by GitHub
parent
commit
269a19c2ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      packages/noco-docs/content/en/developer-resources/webhooks.md
  2. 5
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts
  3. 69
      packages/nocodb/src/lib/meta/helpers/webhookHelpers.ts

8
packages/noco-docs/content/en/developer-resources/webhooks.md

@ -66,15 +66,9 @@ For INSERT/ UPDATE based triggers, use following handlebars to access correspond
Note that, for Update trigger - all the fields in the ROW will be accessible, not just the field updated. Note that, for Update trigger - all the fields in the ROW will be accessible, not just the field updated.
For DELETE based triggers, **only** {{ data.id }} is accessible representing ID of the column deleted. For DELETE based triggers, **only** {{ data.id }} is accessible representing ID of the column deleted.
For all trigger, following **user** information associated with person trigger can be accessed.
- {{ **user**.id }} : Unique auto incremented NocoDB system value
- {{ **user**.email }} : User E-mail.
- {{ **user**.roles }} : User Role amongst [Owner, Creator, Editor, Commenter, Viewer].
### JSON format ### JSON format
Use {{ json data }} {{ json user }} to dump complete data & user information available in JSON format Use {{ json data }} to dump complete data & user information available in JSON format
### Additional references: ### Additional references:

5
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -34,8 +34,7 @@ import Hook from '../../../../models/Hook';
import NcPluginMgrv2 from '../../../../meta/helpers/NcPluginMgrv2'; import NcPluginMgrv2 from '../../../../meta/helpers/NcPluginMgrv2';
import { import {
_transformSubmittedFormDataForEmail, _transformSubmittedFormDataForEmail,
invokeWebhook, invokeWebhook
parseBody
} from '../../../../meta/helpers/webhookHelpers'; } from '../../../../meta/helpers/webhookHelpers';
import Validator from 'validator'; import Validator from 'validator';
import { customValidators } from './customValidators'; import { customValidators } from './customValidators';
@ -1791,7 +1790,7 @@ class BaseModelSqlv2 {
// todo: notification template // todo: notification template
(await NcPluginMgrv2.emailAdapter())?.mailSend({ (await NcPluginMgrv2.emailAdapter())?.mailSend({
to: emails.join(','), to: emails.join(','),
subject: parseBody('NocoDB Form', req, data, {}), subject: 'NocoDB Form',
html: ejs.render(formSubmissionEmailTemplate, { html: ejs.render(formSubmissionEmailTemplate, {
data: transformedData, data: transformedData,
tn: this.model.table_name, tn: this.model.table_name,

69
packages/nocodb/src/lib/meta/helpers/webhookHelpers.ts

@ -7,21 +7,13 @@ import Filter from '../../models/Filter';
import HookLog from '../../models/HookLog'; import HookLog from '../../models/HookLog';
import { HookLogType } from 'nocodb-sdk'; import { HookLogType } from 'nocodb-sdk';
export function parseBody( export function parseBody(template: string, data: any): string {
template: string,
user: any,
data: any,
payload: any
): string {
if (!template) { if (!template) {
return template; return template;
} }
return Handlebars.compile(template, { noEscape: true })({ return Handlebars.compile(template, { noEscape: true })({
data, data
user,
payload,
env: process.env
}); });
} }
@ -121,28 +113,24 @@ export async function handleHttpWebHook(apiMeta, user, data) {
// } // }
} }
export function axiosRequestMake(_apiMeta, user, data) { export function axiosRequestMake(_apiMeta, _user, data) {
const apiMeta = { ..._apiMeta }; const apiMeta = { ..._apiMeta };
if (apiMeta.body) { if (apiMeta.body) {
try { try {
apiMeta.body = JSON.parse(apiMeta.body, (_key, value) => { apiMeta.body = JSON.parse(apiMeta.body, (_key, value) => {
return typeof value === 'string' return typeof value === 'string' ? parseBody(value, data) : value;
? parseBody(value, user, data, apiMeta)
: value;
}); });
} catch (e) { } catch (e) {
apiMeta.body = parseBody(apiMeta.body, user, data, apiMeta); apiMeta.body = parseBody(apiMeta.body, data);
} }
} }
if (apiMeta.auth) { if (apiMeta.auth) {
try { try {
apiMeta.auth = JSON.parse(apiMeta.auth, (_key, value) => { apiMeta.auth = JSON.parse(apiMeta.auth, (_key, value) => {
return typeof value === 'string' return typeof value === 'string' ? parseBody(value, data) : value;
? parseBody(value, user, data, apiMeta)
: value;
}); });
} catch (e) { } catch (e) {
apiMeta.auth = parseBody(apiMeta.auth, user, data, apiMeta); apiMeta.auth = parseBody(apiMeta.auth, data);
} }
} }
apiMeta.response = {}; apiMeta.response = {};
@ -150,23 +138,18 @@ export function axiosRequestMake(_apiMeta, user, data) {
params: apiMeta.parameters params: apiMeta.parameters
? apiMeta.parameters.reduce((paramsObj, param) => { ? apiMeta.parameters.reduce((paramsObj, param) => {
if (param.name && param.enabled) { if (param.name && param.enabled) {
paramsObj[param.name] = parseBody(param.value, user, data, apiMeta); paramsObj[param.name] = parseBody(param.value, data);
} }
return paramsObj; return paramsObj;
}, {}) }, {})
: {}, : {},
url: parseBody(apiMeta.path, user, data, apiMeta), url: parseBody(apiMeta.path, data),
method: apiMeta.method, method: apiMeta.method,
data: apiMeta.body, data: apiMeta.body,
headers: apiMeta.headers headers: apiMeta.headers
? apiMeta.headers.reduce((headersObj, header) => { ? apiMeta.headers.reduce((headersObj, header) => {
if (header.name && header.enabled) { if (header.name && header.enabled) {
headersObj[header.name] = parseBody( headersObj[header.name] = parseBody(header.value, data);
header.value,
user,
data,
apiMeta
);
} }
return headersObj; return headersObj;
}, {}) }, {})
@ -208,24 +191,9 @@ export async function invokeWebhook(
case 'Email': case 'Email':
{ {
const res = await (await NcPluginMgrv2.emailAdapter())?.mailSend({ const res = await (await NcPluginMgrv2.emailAdapter())?.mailSend({
to: parseBody( to: parseBody(notification?.payload?.to, data),
notification?.payload?.to, subject: parseBody(notification?.payload?.subject, data),
user, html: parseBody(notification?.payload?.body, data)
data,
notification?.payload
),
subject: parseBody(
notification?.payload?.subject,
user,
data,
notification?.payload
),
html: parseBody(
notification?.payload?.body,
user,
data,
notification?.payload
)
}); });
hookLog = { hookLog = {
...hook, ...hook,
@ -258,16 +226,9 @@ export async function invokeWebhook(
const res = await ( const res = await (
await NcPluginMgrv2.webhookNotificationAdapters(notification.type) await NcPluginMgrv2.webhookNotificationAdapters(notification.type)
).sendMessage( ).sendMessage(
parseBody( parseBody(notification?.payload?.body, data),
notification?.payload?.body,
user,
data,
notification?.payload
),
JSON.parse(JSON.stringify(notification?.payload), (_key, value) => { JSON.parse(JSON.stringify(notification?.payload), (_key, value) => {
return typeof value === 'string' return typeof value === 'string' ? parseBody(value, data) : value;
? parseBody(value, user, data, notification?.payload)
: value;
}) })
); );

Loading…
Cancel
Save