mirror of
https://github.com/wekan/wekan.git
synced 2026-11-20 15:45:01 -05:00
ErrorBleed fixed boards.js and users.js. Thirty-four more REST handlers - lists, swimlanes, card counts, integrations, org, team, settings, attachment storage settings, rules and dependencies - still answered a refused request with HTTP 200 and the raw error object, or the raw object with its status, so a non-member's request read as a success and an unexpected failure exposed its internals. All now answer through publicErrorData(). The negative test scans the whole server tree for the old shapes, single- and multi-line; a browser test checks a non-member gets 403. Thanks to xet7 !
272 lines
11 KiB
JavaScript
272 lines
11 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { WebApp } from 'meteor/webapp';
|
|
import { Authentication } from '/server/authentication';
|
|
import { sendJsonResult } from '/server/apiMiddleware';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { TriggersDef } from '/server/triggersDef';
|
|
import Rules from '/models/rules';
|
|
import Triggers from '/models/triggers';
|
|
import Actions from '/models/actions';
|
|
import { Random } from 'meteor/random';
|
|
import { withRuleHistory, removeRuleWithUnusedParts, writeRuleComponent } from '/server/lib/ruleHistory';
|
|
// ErrorBleed: refusals answer with their real status and a safe message.
|
|
const { publicErrorData } = require('/server/lib/apiResponseHelpers');
|
|
const { ruleButtonMetadata } = require('/models/lib/ruleButtonMetadata');
|
|
|
|
// REST API for board automation Rules (add / edit / remove).
|
|
//
|
|
// A rule links a trigger (the event/schedule) to an action (what happens). The
|
|
// API embeds the full trigger and action inline so a rule is self-contained:
|
|
//
|
|
// POST /api/boards/:boardId/rules
|
|
// body: { "title": "...", "trigger": { "activityType": "...", ... },
|
|
// "action": { "actionType": "...", ... } }
|
|
// GET /api/boards/:boardId/rules
|
|
// GET /api/boards/:boardId/rules/:ruleId
|
|
// PUT /api/boards/:boardId/rules/:ruleId
|
|
// body: { "title"?, "trigger"?: {...}, "action"?: {...} }
|
|
// DELETE /api/boards/:boardId/rules/:ruleId
|
|
|
|
const STRIP = ['_id', 'boardId', 'createdAt', 'modifiedAt', 'updatedAt'];
|
|
|
|
function strip(doc) {
|
|
const out = {};
|
|
Object.keys(doc || {}).forEach(k => {
|
|
if (!STRIP.includes(k)) out[k] = doc[k];
|
|
});
|
|
return out;
|
|
}
|
|
|
|
// #2674 ("Rule: remove USER from card when move"): the rule matcher
|
|
// (server/rulesHelper.js buildMatchingFieldsMap) queries EVERY matching field
|
|
// of the trigger's activityType with {$in: [<activity value>, '*']}. A Mongo
|
|
// $in never matches a document that LACKS the field, so a trigger created over
|
|
// REST without e.g. userId / listName / swimlaneName / cardTitle could never
|
|
// match any activity and its rule silently never fired — exactly the "user is
|
|
// added when the card moves to the list, but never removed when it moves away"
|
|
// failure of #2674. The Rules UI wizard (sanitizeObject) and the rules JSON
|
|
// import (normalizeTrigger) both default missing matching fields to the '*'
|
|
// wildcard; do the same for API-created/edited triggers. Trigger kinds not
|
|
// driven by activities (scheduledTrigger, button — not in TriggersDef) are
|
|
// left untouched.
|
|
function normalizeTriggerDoc(trigger) {
|
|
const out = { ...trigger };
|
|
const def = TriggersDef[out.activityType];
|
|
if (def) {
|
|
def.matchingFields.forEach(field => {
|
|
if (out[field] === undefined || out[field] === null || out[field] === '') {
|
|
out[field] = '*';
|
|
}
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function serializeRule(rule) {
|
|
const trigger = await ReactiveCache.getTrigger(rule.triggerId);
|
|
const action = await ReactiveCache.getAction(rule.actionId);
|
|
const out = {
|
|
_id: rule._id,
|
|
title: rule.title,
|
|
enabled: rule.enabled !== false,
|
|
trigger: trigger ? strip(trigger) : null,
|
|
action: action ? strip(action) : null,
|
|
};
|
|
// #4294: further triggers (any fires the rule) and actions (run in order).
|
|
if ((rule.extraTriggerIds || []).length) {
|
|
out.extraTriggers = (await Promise.all(rule.extraTriggerIds.map(id => ReactiveCache.getTrigger(id)))).filter(Boolean).map(strip);
|
|
}
|
|
if ((rule.extraActionIds || []).length) {
|
|
out.extraActions = (await Promise.all(rule.extraActionIds.map(id => ReactiveCache.getAction(id)))).filter(Boolean).map(strip);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
if (Meteor.isServer) {
|
|
/**
|
|
* @operation get_board_rules
|
|
* @tag Rules
|
|
* @summary Get the list of automation rules of a board
|
|
*
|
|
* @param {string} boardId the board ID
|
|
* @return_type [{_id: string, title: string, trigger: object, action: object}]
|
|
*/
|
|
WebApp.handlers.get('/api/boards/:boardId/rules', async function(req, res) {
|
|
try {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAccess(req.userId, paramBoardId);
|
|
const rules = await ReactiveCache.getRules({ boardId: paramBoardId });
|
|
const data = [];
|
|
for (const rule of rules) {
|
|
// eslint-disable-next-line no-await-in-loop
|
|
data.push(await serializeRule(rule));
|
|
}
|
|
sendJsonResult(res, { code: 200, data });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @operation get_board_rule
|
|
* @tag Rules
|
|
* @summary Get a single automation rule
|
|
*
|
|
* @param {string} boardId the board ID
|
|
* @param {string} ruleId the rule ID
|
|
* @return_type {_id: string, title: string, trigger: object, action: object}
|
|
*/
|
|
WebApp.handlers.get('/api/boards/:boardId/rules/:ruleId', async function(req, res) {
|
|
try {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAccess(req.userId, paramBoardId);
|
|
const rule = await ReactiveCache.getRule({
|
|
_id: req.params.ruleId,
|
|
boardId: paramBoardId,
|
|
});
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
sendJsonResult(res, { code: 200, data: await serializeRule(rule) });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @operation new_board_rule
|
|
* @tag Rules
|
|
* @summary Add an automation rule to a board
|
|
*
|
|
* @description The trigger and action are embedded inline. See the Rules
|
|
* documentation for the available trigger activityTypes and action actionTypes.
|
|
* Trigger matching fields that are omitted (e.g. userId, listName,
|
|
* swimlaneName, cardTitle) default to the '*' wildcard, so e.g. issue #2674's
|
|
* "remove a member when a card is moved away from a list" only needs
|
|
* trigger {activityType: 'moveCard', oldListName: '...'} and
|
|
* action {actionType: 'removeMember', username: '...'}.
|
|
*
|
|
* @param {string} boardId the board ID
|
|
* @param {string} title the rule title
|
|
* @param {object} trigger the trigger document (must include activityType)
|
|
* @param {object} action the action document (must include actionType)
|
|
* @return_type {_id: string, triggerId: string, actionId: string}
|
|
*/
|
|
WebApp.handlers.post('/api/boards/:boardId/rules', async function(req, res) {
|
|
try {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
|
|
const { title, trigger, action } = req.body;
|
|
if (!trigger || !action) {
|
|
throw new Meteor.Error('bad-request', 'trigger and action are required');
|
|
}
|
|
if (!trigger.activityType) {
|
|
throw new Meteor.Error('bad-request', 'trigger.activityType is required');
|
|
}
|
|
if (!action.actionType) {
|
|
throw new Meteor.Error('bad-request', 'action.actionType is required');
|
|
}
|
|
const ruleId = Random.id();
|
|
const ids = await withRuleHistory(ruleId, req.userId, async () => {
|
|
const triggerId = await Triggers.insertAsync({
|
|
...normalizeTriggerDoc(strip(trigger)),
|
|
boardId: paramBoardId,
|
|
});
|
|
const actionId = await Actions.insertAsync({ ...strip(action), boardId: paramBoardId });
|
|
await Rules.insertAsync({
|
|
_id: ruleId,
|
|
title: title || 'API rule',
|
|
triggerId,
|
|
actionId,
|
|
boardId: paramBoardId,
|
|
...ruleButtonMetadata(trigger, title || 'API rule').$set,
|
|
});
|
|
return { _id: ruleId, triggerId, actionId };
|
|
});
|
|
sendJsonResult(res, { code: 200, data: ids });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @operation edit_board_rule
|
|
* @tag Rules
|
|
* @summary Edit an automation rule
|
|
*
|
|
* @description Any of title, enabled, trigger and action may be supplied; the trigger
|
|
* and action documents are replaced ($set) when present. A supplied trigger
|
|
* that includes an activityType gets its missing matching fields defaulted
|
|
* to the '*' wildcard, like on creation.
|
|
*
|
|
* @param {string} boardId the board ID
|
|
* @param {string} ruleId the rule ID
|
|
* @param {string} [title] the new rule title
|
|
* @param {boolean} [enabled] false pauses the rule without deleting it (its
|
|
* trigger and action stay as they are); true resumes it
|
|
* @param {object} [trigger] the new trigger document
|
|
* @param {object} [action] the new action document
|
|
* @return_type {_id: string}
|
|
*/
|
|
WebApp.handlers.put('/api/boards/:boardId/rules/:ruleId', async function(req, res) {
|
|
try {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
|
|
const rule = await ReactiveCache.getRule({
|
|
_id: req.params.ruleId,
|
|
boardId: paramBoardId,
|
|
});
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
|
|
await withRuleHistory(rule._id, req.userId, async () => {
|
|
if (typeof req.body.title === 'string') {
|
|
await Rules.updateAsync(rule._id, { $set: { title: req.body.title } });
|
|
}
|
|
// The "temporarily disable a rule" switch: a paused rule keeps its
|
|
// trigger and action and is simply skipped by the rule engine.
|
|
if (req.body.enabled !== undefined) {
|
|
const enabled = req.body.enabled === true || req.body.enabled === 'true';
|
|
await Rules.updateAsync(rule._id, { $set: { enabled } });
|
|
}
|
|
if (req.body.trigger) {
|
|
const triggerId = await writeRuleComponent(rule, 'trigger', { ...normalizeTriggerDoc(strip(req.body.trigger)), boardId: paramBoardId }, { patch: true });
|
|
await Rules.updateAsync(rule._id, { $set: { triggerId } });
|
|
await Rules.updateAsync(rule._id, ruleButtonMetadata(await Triggers.findOneAsync(triggerId), req.body.title || rule.title));
|
|
}
|
|
if (req.body.action) {
|
|
const actionId = await writeRuleComponent(rule, 'action', { ...strip(req.body.action), boardId: paramBoardId }, { patch: true });
|
|
await Rules.updateAsync(rule._id, { $set: { actionId } });
|
|
}
|
|
});
|
|
sendJsonResult(res, { code: 200, data: { _id: rule._id } });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @operation delete_board_rule
|
|
* @tag Rules
|
|
* @summary Remove an automation rule (and its trigger and action)
|
|
*
|
|
* @param {string} boardId the board ID
|
|
* @param {string} ruleId the rule ID
|
|
* @return_type {_id: string}
|
|
*/
|
|
WebApp.handlers.delete('/api/boards/:boardId/rules/:ruleId', async function(req, res) {
|
|
try {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
|
|
const rule = await ReactiveCache.getRule({
|
|
_id: req.params.ruleId,
|
|
boardId: paramBoardId,
|
|
});
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
await withRuleHistory(rule._id, req.userId, async () => {
|
|
await removeRuleWithUnusedParts(rule);
|
|
});
|
|
sendJsonResult(res, { code: 200, data: { _id: rule._id } });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
}
|