Files
wekan/models/customFields.js
T
Lauri Ojansivu 87ae8895c3 Add read-only custom fields that every member sees and only admins set.
A custom field could be hidden from non-admins (adminOnly, #3141), but not
shown to everyone while editable only by admins, which a score or a value
computed elsewhere needs. A custom field can now be marked read-only in its
settings: every board member sees the value, and only a board admin - or the
REST API and rules acting as one - can set, change or clear it. The card
editor offers editing only to board admins, and the server refuses everyone
else through adminOnly's write guard, at the driver boundary, so no write
path is left out. Turning the flag on or off is itself an admin's change.

tests/readOnlyCustomField.test.cjs drives the rule, including setting an
empty field and clearing one. New text is in English, pending Transifex.

Thanks to CarloRampini and xet7 !

Fixes #3143.
2026-10-08 12:05:59 +03:00

212 lines
5.3 KiB
JavaScript

import { Mongo } from 'meteor/mongo';
import Cards from '/models/cards';
const { SimpleSchema } = require('/imports/simpleSchema');
const CustomFields = new Mongo.Collection('customFields');
/**
* A custom field on a card in the board
*/
CustomFields.attachSchema(
new SimpleSchema({
boardIds: {
/**
* the ID of the board
*/
type: Array,
},
'boardIds.$': {
type: String,
},
name: {
/**
* name of the custom field
*/
type: String,
},
type: {
/**
* type of the custom field
*/
type: String,
allowedValues: [
'text',
'number',
'date',
'dropdown',
'dropdownMultiSelect',
'checkbox',
'currency',
'stringtemplate',
],
},
settings: {
/**
* settings of the custom field
*/
type: Object,
},
'settings.jiraTimeField': {
type: String, optional: true, allowedValues: ['original', 'remaining'],
},
'settings.jiraEstimateFieldId': { type: String, optional: true, regEx: /^customfield_\d{1,20}$/ },
'settings.jiraEstimateUnit': { type: String, optional: true, max: 80 },
'settings.currencyCode': {
type: String,
optional: true,
},
'settings.dropdownItems': {
/**
* list of drop down items objects
*/
type: Array,
optional: true,
},
'settings.dropdownItems.$': {
type: new SimpleSchema({
_id: {
/**
* ID of the drop down item
*/
type: String,
},
name: {
/**
* name of the drop down item
*/
type: String,
},
}),
},
'settings.stringtemplateFormat': {
type: String,
optional: true,
},
'settings.stringtemplateSeparator': {
type: String,
optional: true,
},
showOnCard: {
/**
* should we show on the cards this custom field
*/
type: Boolean,
defaultValue: false,
},
automaticallyOnCard: {
/**
* should the custom fields automatically be added on cards?
*/
type: Boolean,
defaultValue: false,
},
alwaysOnCard: {
/**
* should the custom field be automatically added to all cards?
*/
type: Boolean,
defaultValue: false,
},
showLabelOnMiniCard: {
/**
* should the label of the custom field be shown on minicards?
*/
type: Boolean,
defaultValue: false,
},
showSumAtTopOfList: {
/**
* should the sum of the custom fields be shown at top of list?
*/
type: Boolean,
defaultValue: false,
},
readOnly: {
/**
* #3143: when true, every board member SEES this custom field's value,
* but only a board admin can set or change it - a "display-only" field
* for a score or a computed value. Set through the REST API, a rule or
* by an admin. Enforced on the server with adminOnly's write guard
* (models/lib/adminOnlyCustomFields.js mayWriteField). Defaults to false.
*/
type: Boolean,
defaultValue: false,
},
adminOnly: {
/**
* #3141: when true, this custom field's VALUE is hidden from - and
* cannot be set by - a non board-admin board member: not on the card
* detail view, not on the minicard, and not through the server-side
* setCustomField method (a UI-only hide is not real access control).
* Defaults to false so every existing custom field is unaffected.
*/
type: Boolean,
defaultValue: false,
},
sort: {
/**
* #4165: the user-settable DISPLAY order of the custom field, both in the
* board-settings list and on a card. Ascending, lower first - mirrors
* Lists' own `sort` field. A newly created field defaults to the end of
* the board's current list (the client passes `existingCount` on insert)
* rather than jumping to the top; fields created before this existed have
* no `sort` of their own and fall back to name order (see
* models/lib/customFieldsWD.js).
*/
type: Number,
optional: true,
},
createdAt: {
type: Date,
optional: true,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return { $setOnInsert: new Date() };
} else {
this.unset();
}
},
},
modifiedAt: {
type: Date,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert || this.isUpsert || this.isUpdate) {
return new Date();
} else {
this.unset();
}
},
},
}),
);
CustomFields.addToAllCards = async cf => {
await Cards.updateAsync(
{
boardId: { $in: cf.boardIds },
customFields: { $not: { $elemMatch: { _id: cf._id } } },
},
{
$push: { customFields: { _id: cf._id, value: null } },
},
{ multi: true },
);
};
CustomFields.helpers({
async addBoard(boardId) {
if (boardId) {
return await CustomFields.updateAsync(this._id, {
$push: { boardIds: boardId },
});
}
return null;
},
});
export default CustomFields;