Files
wekan/server/models/attachments.js
T
Lauri Ojansivu bbcd98d9d8 Add REST API for attachment soft delete and restore, Admin Panel Problems, OAuth login provider and passwordless settings, card field order and pausing a rule.
Every feature of the recent releases that had a UI but no endpoint, on the
same server-side code the UI uses:

- DELETE /api/boards/:boardId/attachments/:attachmentId soft-deletes,
  POST .../attachments/:attachmentId/restore restores, and
  GET /api/boards/:boardId/attachments/deleted lists what is restorable -
  all through the attachments.softDelete / attachments.restore methods the
  card and the card history use, run as the request's user. No hard delete.
- GET /api/admin/problems (the Problems status overview plus the new-problem
  count per stream), GET /api/admin/problems/:stream (one page, with
  limit/skip/search) and POST /api/admin/problems/:stream/acknowledge,
  through the admin-only eventLog* methods the Admin Panel pages call.
- GET /api/admin/oauth-providers, PUT /api/admin/oauth-providers/:providerKey
  and PUT /api/admin/passwordless, through saveOauthProviderSettings,
  savePasswordlessSettings and getOauthProviderConfigSources. A provider's
  secret is reported only as { source, hasValue }, never its value.
- GET/PUT /api/boards/:boardId/cardFieldOrder, normalised with the same
  applyCardFieldOrder() the card renders with.
- PUT /api/boards/:boardId/rules/:ruleId accepts `enabled` to pause and
  resume a rule, and GET reports it.

The OpenAPI generator is fixed on the way: server/models/boards.js has had a
bare `catch {` since v11.67, the esprima parser cannot read that, and a parse
failure was silently skipped - so public/api/wekan.yml has shipped WITHOUT
the whole Boards API since then. It now downlevels `catch {` and
`for await (`, warns when a file cannot be parsed, and no longer emits an
empty sub-schema for a primitive array-element marker such as
`wipLimitGroups.$.listIds.$`, which made the spec unparseable YAML.
public/api/wekan.yml and wekan.html are regenerated with the release
workflow's own commands: 156 operations, up from 122.

tests/restApiNewFeatureRoutes.test.cjs pins every new route to its method,
path, @operation block and authentication check, that the OAuth endpoints
never mention a secret outside the input whitelist, the generator's two
fixes, that the generated and the committed spec carry the Boards API and the
new operations, and - as the negative sweep - that no route in models/ or
server/models/ lacks an authentication check.

Thanks to xet7 !
2026-09-11 21:18:35 +03:00

140 lines
5.7 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { DDP } from 'meteor/ddp';
import { Authentication } from '/server/authentication';
import { sendJsonResult } from '/server/apiMiddleware';
import { publicErrorData } from '/server/lib/apiResponseHelpers';
import Attachments from '/models/attachments';
// REST API for the attachment soft delete (docs/Features/Reports/History/
// History.md §12). Every write here goes through the SAME Meteor methods the
// card and the card history use - `attachments.softDelete` and
// `attachments.restore` in server/attachmentSoftDelete.js - run as the
// request's user, so the permission check, the cover unset, the activity and
// the history row are the ones the UI gets. Nothing here hard-deletes: the one
// hard delete of attachments is the archived-board purge (§12.4).
// The raw collection, not ReactiveCache.getAttachment(): that one falls back to
// the legacy CollectionFS lookup, and a soft-deleted document must be found as
// itself, deletedAt and all.
async function attachmentOfBoard(attachmentId, boardId) {
return Attachments.collection.findOneAsync({
_id: attachmentId,
'meta.boardId': boardId,
});
}
function callAsUser(userId, name, ...args) {
return DDP._CurrentMethodInvocation.withValue(
{ userId },
async () => Meteor.callAsync(name, ...args),
);
}
function serializeDeleted(attachment) {
return {
attachmentId: attachment._id,
attachmentName: attachment.name,
attachmentType: attachment.type,
boardId: attachment.meta.boardId,
swimlaneId: attachment.meta.swimlaneId,
listId: attachment.meta.listId,
cardId: attachment.meta.cardId,
deletedAt: attachment.deletedAt,
deletedBy: attachment.deletedBy,
deleteBatchId: attachment.deleteBatchId,
};
}
/**
* @operation get_board_deleted_attachments
* @tag Attachments
* @summary Get the soft-deleted attachments of a board
*
* @description Deleting an attachment from a card is a soft delete: the file
* is kept, the card hides it, and the card history can restore it. This lists
* the attachments of a board that are currently deleted, with who deleted each
* and when, so a script can find one to restore. The live attachments are at
* `GET /api/boards/:boardId/attachments`. Requires board access.
*
* @param {string} boardId the board ID
* @return_type [{attachmentId: string, attachmentName: string, attachmentType: string, boardId: string, swimlaneId: string, listId: string, cardId: string, deletedAt: string, deletedBy: string, deleteBatchId: string}]
*/
WebApp.handlers.get('/api/boards/:boardId/attachments/deleted', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
await Authentication.checkBoardAccess(req.userId, paramBoardId);
const deleted = await Attachments.collection
.find({ 'meta.boardId': paramBoardId, deletedAt: { $ne: null } })
.fetchAsync();
sendJsonResult(res, { code: 200, data: deleted.map(serializeDeleted) });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
/**
* @operation delete_board_attachment
* @tag Attachments
* @summary Soft-delete an attachment
*
* @description The same "Delete" the card offers: the attachment gets
* `deletedAt`, `deletedBy` and `deleteBatchId`, its file is kept, the card and
* the minicard badge hide it, its cover is unset if it was the card's cover,
* and the card history records who deleted it. Restore it with
* `POST /api/boards/:boardId/attachments/:attachmentId/restore`. There is no
* hard delete of a single attachment over the API. Requires permission to
* edit the card the attachment is on (or write access to the board for a
* board-level attachment). Idempotent: an already-deleted attachment is left
* as it is.
*
* @param {string} boardId the board ID
* @param {string} attachmentId the attachment ID
* @return_type {deleted: boolean, batchId: string}
*/
WebApp.handlers.delete('/api/boards/:boardId/attachments/:attachmentId', async function(req, res) {
try {
Authentication.checkLoggedIn(req.userId);
const attachment = await attachmentOfBoard(req.params.attachmentId, req.params.boardId);
if (!attachment) {
sendJsonResult(res, { code: 404, data: { error: 'Attachment not found' } });
return;
}
const result = await callAsUser(req.userId, 'attachments.softDelete', attachment._id);
sendJsonResult(res, { code: 200, data: result });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
/**
* @operation restore_board_attachment
* @tag Attachments
* @summary Restore a soft-deleted attachment
*
* @description The card history's Restore for an attachment row: clears the
* delete mark so the card, its attachment count and the minicard badge include
* the attachment again, and records the restore in the card history. The
* cover is never re-set by a restore. Requires the same permission as
* deleting. Idempotent: `restored` is false for an attachment that is not
* deleted.
*
* @param {string} boardId the board ID
* @param {string} attachmentId the attachment ID
* @return_type {restored: boolean}
*/
WebApp.handlers.post('/api/boards/:boardId/attachments/:attachmentId/restore', async function(req, res) {
try {
Authentication.checkLoggedIn(req.userId);
const attachment = await attachmentOfBoard(req.params.attachmentId, req.params.boardId);
if (!attachment) {
sendJsonResult(res, { code: 404, data: { error: 'Attachment not found' } });
return;
}
const result = await callAsUser(req.userId, 'attachments.restore', attachment._id);
sendJsonResult(res, { code: 200, data: result });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});