mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
#2713 asked for two things. Editing a rule used to mean delete it and rebuild it from scratch, losing its position and its identity. rulesList's rule row gets a second "Edit trigger/action" button next to the existing title-rename pencil, opening the same trigger/action wizard used to create a rule (rulesMain.js / rulesTriggers / rulesActions and the board/card/checklist/mail action templates), pre-filled with the rule's current title. A new server method, rules.updateRule (server/rulesButton.js), mirrors rules.createRule's authorization and cross-board destination checks but REPLACES the existing trigger/action documents in place (full-document update by their existing _id, not remove+insert), so the rule keeps its own _id, and its trigger/ action keep theirs, across an edit. Every action-template click handler that used to call rules.createRule or insert Triggers/Actions/Rules directly now goes through one shared helper, client/components/rules/rulesSaveHelper.js, which picks create vs. update based on whether the wizard was opened to edit an existing rule. The "send an email" rule action already appended the card's title and a link to it automatically (#3301), even when the user's own template used none of the {card}/{cardLink} tokens; it did not do the same for the card's description. server/rulesHelper.js now appends "Description: ..." to the footer alongside the existing title/link lines, using the vars.description value buildRuleVars already computed. Actually attaching the card's FILE ATTACHMENTS to the outgoing email is deferred - see CHANGELOG TODO Later. Every WeKan email today goes through either Meteor's `Email.sendAsync`/`EmailLocalization.sendEmail`, which take { to, from, subject, text }, with no `attachments` parameter anywhere in this codebase's call sites; wiring a real file through would need the mailer wrapper itself to grow attachment support and code to read the file back out of whichever storage backend (local disk / GridFS / S3) holds it, which is mailer-level scope well past this rule action. tests/ruleUpdateInPlace.test.cjs pins rules.updateRule's authorization, its RuleBleed cross-board guard, and - the part that actually makes it an edit rather than a second create - that it keeps the rule/trigger/action _ids stable, including a logic-level round trip against a small in-memory mock proving a changed actionType does not leave the old type's fields behind. tests/ruleEmailIncludesDescription.test.cjs pins the new description line, guarded the same way the existing title/line lines are (only appended when non-empty). tests/ruleCardActionSchema.test.cjs (#6630's guard, that desc lives on the Action document and never leaks onto the Rule document) is updated for the new saveRuleTriggerAction call shape. Thanks to jvavassori and xet7 !
216 lines
9.1 KiB
JavaScript
216 lines
9.1 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check, Match } from 'meteor/check';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { RulesHelper } from '/server/rulesHelper';
|
|
import Rules from '/models/rules';
|
|
import Triggers from '/models/triggers';
|
|
import Actions from '/models/actions';
|
|
import { canDeleteBoardRule } from '/models/lib/ruleDeletePermission';
|
|
import { allowIsBoardMemberWithWriteAccess } from '/server/lib/utils';
|
|
import { tripCanary } from '/server/lib/canary';
|
|
|
|
// Button rules are manual: a user clicks a card/board button and we run the
|
|
// rule's action immediately. This method runs one button rule on demand.
|
|
Meteor.methods({
|
|
async 'rules.runButton'(ruleId, cardId) {
|
|
check(ruleId, String);
|
|
check(cardId, Match.Optional(String));
|
|
|
|
const rule = await ReactiveCache.getRule(ruleId);
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
|
|
const trigger = await ReactiveCache.getTrigger(rule.triggerId);
|
|
if (!trigger || trigger.activityType !== 'button') {
|
|
throw new Meteor.Error('not-a-button', 'Rule is not a button rule');
|
|
}
|
|
|
|
// The user must be a member of the board the rule belongs to.
|
|
const board = await ReactiveCache.getBoard(rule.boardId);
|
|
if (!board || !board.hasMember(this.userId)) {
|
|
throw new Meteor.Error('not-authorized', 'Not a board member');
|
|
}
|
|
|
|
const action = await ReactiveCache.getAction(rule.actionId);
|
|
if (!action) throw new Meteor.Error('not-found', 'Action not found');
|
|
|
|
await RulesHelper.performAction(
|
|
{
|
|
activityType: 'button',
|
|
cardId,
|
|
boardId: rule.boardId,
|
|
userId: this.userId,
|
|
},
|
|
action,
|
|
);
|
|
return true;
|
|
},
|
|
|
|
// Create a rule (trigger + action + rule) on the server in one call. The rule
|
|
// wizard previously did three client-side Collection.insert() calls; those are
|
|
// optimistic writes whose documents end up in client-minimongo limbo once the
|
|
// wizard's `boardRules` subscription stops on navigation, so a freshly created
|
|
// board-button's trigger never re-published to the board view (the button only
|
|
// appeared after a full page reload). Inserting on the server avoids the
|
|
// optimistic-ghost reconciliation problem entirely.
|
|
async 'rules.createRule'(boardId, title, trigger, action) {
|
|
check(boardId, String);
|
|
check(title, Match.Optional(String));
|
|
check(trigger, Object);
|
|
check(action, Object);
|
|
|
|
const board = await ReactiveCache.getBoard(boardId);
|
|
if (!board) throw new Meteor.Error('not-found', 'Board not found');
|
|
if (!board.hasAdmin(this.userId)) {
|
|
throw new Meteor.Error('not-authorized', 'Must be a board admin');
|
|
}
|
|
|
|
const clean = doc => {
|
|
const { _id, ...rest } = doc || {};
|
|
return rest;
|
|
};
|
|
|
|
// #5536: an action's own boardId is the DESTINATION board (e.g. "move card
|
|
// to board X" / "link card to board X"). Let it win over the source boardId
|
|
// so cross-board move/link rules created through this method keep their
|
|
// target — only fall back to the source board when the action omits one.
|
|
// #6472: "omits" must include an EMPTY boardId (a not-yet-loaded board
|
|
// select posts ''), which the spread would otherwise let override the
|
|
// fallback and make every list lookup in the action fail silently.
|
|
const actionDoc = { boardId, ...clean(action) };
|
|
if (!actionDoc.boardId) actionDoc.boardId = boardId;
|
|
// RuleBleed (GHSA-9w4x-hf2r-hc9v): automation actions execute on the
|
|
// server, where collection allow/deny callbacks do not run. Do not persist
|
|
// a cross-board destination unless the rule creator can write there.
|
|
if (actionDoc.boardId !== boardId) {
|
|
const destination = await ReactiveCache.getBoard(actionDoc.boardId);
|
|
if (!allowIsBoardMemberWithWriteAccess(this.userId, destination)) {
|
|
tripCanary('rule.cross-board-write', { userId: this.userId });
|
|
throw new Meteor.Error(
|
|
'not-authorized',
|
|
'Must have write access to the destination board',
|
|
);
|
|
}
|
|
}
|
|
const triggerId = await Triggers.insertAsync({ ...clean(trigger), boardId });
|
|
const actionId = await Actions.insertAsync(actionDoc);
|
|
const ruleDoc = {
|
|
title: title || 'Rule',
|
|
triggerId,
|
|
actionId,
|
|
boardId,
|
|
};
|
|
// Denormalise manual-button metadata onto the rule. The board view renders
|
|
// its board/card buttons from the rule document alone, because in this Meteor
|
|
// 3 setup the schemaless `triggers` collection's documents do not reach the
|
|
// client over the board subscription (the schema-backed `rules` collection
|
|
// does), so reading the label off the published trigger would leave the
|
|
// button blank/absent.
|
|
if (trigger && trigger.activityType === 'button') {
|
|
ruleDoc.buttonType = trigger.buttonType || 'card';
|
|
ruleDoc.buttonLabel = trigger.buttonLabel || title || 'Run';
|
|
}
|
|
const ruleId = await Rules.insertAsync(ruleDoc);
|
|
return { _id: ruleId, triggerId, actionId };
|
|
},
|
|
|
|
// #2713: edit an existing rule's trigger/action in place instead of forcing
|
|
// "delete the rule, recreate it from scratch". The rule document keeps its
|
|
// own _id (and, when it already has one, its trigger/action _ids too) —
|
|
// only their CONTENT is replaced, so anything that already refers to this
|
|
// rule by id keeps working after the edit.
|
|
async 'rules.updateRule'(ruleId, title, trigger, action) {
|
|
check(ruleId, String);
|
|
check(title, Match.Optional(String));
|
|
check(trigger, Object);
|
|
check(action, Object);
|
|
|
|
const rule = await ReactiveCache.getRule(ruleId);
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
|
|
const board = await ReactiveCache.getBoard(rule.boardId);
|
|
if (!board) throw new Meteor.Error('not-found', 'Board not found');
|
|
if (!board.hasAdmin(this.userId)) {
|
|
throw new Meteor.Error('not-authorized', 'Must be a board admin');
|
|
}
|
|
|
|
const clean = doc => {
|
|
const { _id, ...rest } = doc || {};
|
|
return rest;
|
|
};
|
|
|
|
const boardId = rule.boardId;
|
|
// Same cross-board destination handling as rules.createRule above.
|
|
const actionDoc = { boardId, ...clean(action) };
|
|
if (!actionDoc.boardId) actionDoc.boardId = boardId;
|
|
if (actionDoc.boardId !== boardId) {
|
|
const destination = await ReactiveCache.getBoard(actionDoc.boardId);
|
|
if (!allowIsBoardMemberWithWriteAccess(this.userId, destination)) {
|
|
tripCanary('rule.cross-board-write', { userId: this.userId });
|
|
throw new Meteor.Error(
|
|
'not-authorized',
|
|
'Must have write access to the destination board',
|
|
);
|
|
}
|
|
}
|
|
const triggerDoc = { ...clean(trigger), boardId };
|
|
|
|
// Full-document replace (not $set) so a trigger/action switched to a
|
|
// different type does not keep stale fields from the type it replaced -
|
|
// and keep the existing _id when there is one, so the rule's triggerId/
|
|
// actionId never have to change just because the configuration did.
|
|
let triggerId = rule.triggerId;
|
|
if (triggerId) {
|
|
await Triggers.updateAsync(triggerId, triggerDoc);
|
|
} else {
|
|
triggerId = await Triggers.insertAsync(triggerDoc);
|
|
}
|
|
let actionId = rule.actionId;
|
|
if (actionId) {
|
|
await Actions.updateAsync(actionId, actionDoc);
|
|
} else {
|
|
actionId = await Actions.insertAsync(actionDoc);
|
|
}
|
|
|
|
const ruleSet = {
|
|
title: title || rule.title || 'Rule',
|
|
triggerId,
|
|
actionId,
|
|
};
|
|
if (trigger && trigger.activityType === 'button') {
|
|
ruleSet.buttonType = trigger.buttonType || 'card';
|
|
ruleSet.buttonLabel = trigger.buttonLabel || ruleSet.title;
|
|
}
|
|
await Rules.updateAsync(ruleId, { $set: ruleSet });
|
|
return { _id: ruleId, triggerId, actionId };
|
|
},
|
|
|
|
// Delete a rule (and its trigger + action) on the server in one call. The rule
|
|
// wizard/list/workflow views previously ran three client-side
|
|
// Collection.remove() calls; each is gated by a per-collection allow() rule
|
|
// that resolves the board from that document's own boardId. A Trigger/Action
|
|
// whose boardId does not resolve to a board (legacy docs, or docs not
|
|
// published to the client) made the allow rule return false and Meteor
|
|
// rejected the mutation with 403 "Access denied", failing the delete even for
|
|
// a board admin. Authorizing once here and removing all three docs server-side
|
|
// (which bypasses allow/deny) fixes that without loosening any permission.
|
|
async 'rules.deleteRule'(ruleId) {
|
|
check(ruleId, String);
|
|
|
|
const rule = await ReactiveCache.getRule(ruleId);
|
|
if (!rule) throw new Meteor.Error('not-found', 'Rule not found');
|
|
|
|
const board = await ReactiveCache.getBoard(rule.boardId);
|
|
if (!board) throw new Meteor.Error('not-found', 'Board not found');
|
|
|
|
const user = await ReactiveCache.getUser(this.userId);
|
|
if (!canDeleteBoardRule(board, this.userId, { isSiteAdmin: !!(user && user.isAdmin) })) {
|
|
throw new Meteor.Error('not-authorized', 'Must be a board admin');
|
|
}
|
|
|
|
await Rules.removeAsync(rule._id);
|
|
if (rule.triggerId) await Triggers.removeAsync(rule.triggerId);
|
|
if (rule.actionId) await Actions.removeAsync(rule.actionId);
|
|
return { _id: rule._id };
|
|
},
|
|
});
|