Files
wekan/client/components/settings/emailToCardSettings.js
T
Lauri Ojansivu 718fdb5dac Newest features' settings: on every release platform and in the Admin Panel, and kept that way.
An audit of every environment variable the code reads found 30 that are new
since v12.00. Eleven were already Admin Panel settings and on every platform
the release workflows build. Of the rest:

Now Admin Panel settings, through models/lib/authConfigCatalog.js (a value
there wins, an empty field falls back to the variable):
- Email to card limits, beside its own Admin Panel settings:
  INBOUND_IMAP_POLL_SECONDS, INBOUND_EMAIL_PER_ADDRESS_PER_HOUR,
  INBOUND_EMAIL_MAX_BYTES, INBOUND_EMAIL_MAX_ATTACHMENT_BYTES and
  EMAIL_VIEW_SENDS_PER_HOUR. They were module-level constants, read before
  the Admin Panel values loaded; they are read when used now.
  createRateLimiter takes a limit function, and a changed poll interval
  restarts the IMAP poller.
- ACCOUNTS_COOKIE_REFRESH_RATE_LIMIT in People / Login, like the login
  expiry beside it, applied from the next start.
- Windows SSO (WINDOWS_SSO_ENABLED, _DOMAINS, _ACTIVE_DIRECTORY,
  _MERGE_EXISTING_USERS) in People / Login. It was already read through
  authEnv but had no form. Being Windows-only, its catalog section names
  start-wekan.bat as its one platform.

Listed on every platform (Dockerfile, the docker-compose files, start-wekan.sh
and .bat, the snap config and help), where none of these had been: the
variables above, INBOUND_EMAIL_WEBHOOK_SECRET (its own Admin Panel page
already has it), and the per-process switches that stay environment only -
INBOUND_IMAP_DISABLED (one pod of a multi-pod deployment), WEKAN_DISK_MONITOR,
WEKAN_DISK_WATCH_PATHS, WEKAN_RAM_MONITOR and WEKAN_IMPORT_RUN_STALE_MS.
Left unlisted on purpose: NEXTCLOUD_EXAPP, NEXTCLOUD_URL and
WEKAN_NEXTCLOUD_PROXY_KEY (set by Nextcloud's AppAPI), SANDSTORM_MAILDIR (set
by Sandstorm). DEFAULT_AVATAR_URL keeps no Admin Panel UI, as decided in #824.

tests/envVarCoverage.test.cjs keeps it so. It fails for a variable that is in
no Admin Panel catalog, OWN_PANE or ENV_ONLY with its reason; the variables
read before v12.00 are a frozen LEGACY list that can only shrink. An
environment-only or own-page setting a user sets must be on every platform,
and stale entries fail. tests/authConfigCatalog.test.cjs honours a section's
platform list. tests/adminPanelNewSettings.test.cjs pins the reads and the
forms. tests/cookieRefreshRateLimit.test.cjs's guard now expects the Admin
Panel value, and says why. tests/playwright/specs/admin-new-env-settings.e2e.js
opens the forms.

Thanks to xet7 !
2026-10-20 13:50:10 +03:00

277 lines
11 KiB
JavaScript

import { ReactiveCache } from '/imports/reactiveCache';
import { ReactiveVar } from 'meteor/reactive-var';
import { Random } from 'meteor/random';
import { TAPi18n } from '/imports/i18n';
import { Utils } from '/client/lib/utils';
import { EMAIL_PARTS, cleanParts } from '/models/lib/emailToCard';
const { AUTH_CONFIG_SECTIONS } = require('/models/lib/authConfigCatalog');
// Email to card in Notification Settings (emailToCardSettings.jade). The
// admin form is read through getInboundMailSettings - the settings document's
// inboundMail is never published, because it holds the passwords - and saved
// with saveInboundMailSettings, which keeps a stored password or secret when
// its field is left empty. Board and member choices save on every change.
function currentBoard() {
return ReactiveCache.getBoard(Utils.getCurrentBoardId());
}
function errorText(error) {
const key = error && (error.error || error.reason);
return key ? TAPi18n.__(String(key)) : TAPi18n.__('error-notAuthorized');
}
const PART_LABELS = {
description: 'email-to-card-part-description',
stripQuoted: 'email-to-card-part-strip-quoted',
attachments: 'email-to-card-part-attachments',
senderLine: 'email-to-card-part-sender-line',
};
function partOptions(name, value) {
const kind = EMAIL_PARTS[name];
const values = Array.isArray(kind) ? kind : [true, false];
return [{ value: '', labelKey: 'default', selected: value === undefined },
...values.map(v => ({
value: String(v),
labelKey: v === true ? 'yes' : v === false ? 'no' : `email-to-card-description-${v}`,
selected: value === v,
}))];
}
function storedParts(scope) {
if (scope === 'board') {
const board = currentBoard();
return cleanParts(board && board.emailToCard && board.emailToCard.parts);
}
const user = ReactiveCache.getCurrentUser();
return cleanParts(user && user.profile && user.profile.emailToCard && user.profile.emailToCard.parts);
}
function boardEmailToCard() {
const board = currentBoard();
return (board && board.emailToCard) || {};
}
function saveBoard(instance, change) {
const board = currentBoard();
if (!board) return;
const current = boardEmailToCard();
const next = {
enabled: current.enabled === true,
listId: current.listId || null,
swimlaneId: current.swimlaneId || null,
parts: cleanParts(current.parts),
...change,
};
Meteor.call('setBoardEmailToCard', board._id, next, error => {
instance.message.set(error ? errorText(error) : '');
if (!error) loadAddress(instance);
});
}
function saveMember(instance, change) {
const user = ReactiveCache.getCurrentUser();
const current = (user && user.profile && user.profile.emailToCard) || {};
const next = {
enabled: current.enabled === true || current.enabled === false ? current.enabled : null,
parts: cleanParts(current.parts),
...change,
};
Meteor.call('setMemberEmailToCard', next, error => instance.message.set(error ? errorText(error) : ''));
}
function loadAddress(instance) {
const boardId = Utils.getCurrentBoardId();
if (!boardId) return;
Meteor.call('emailToCardAddress', boardId, (error, result) => {
instance.address.set(error ? { enabled: false, reason: 'not-authorized' } : result);
});
}
// The admin form, read back from the inputs (accounts keep their order).
function readAdminForm(instance) {
const admin = instance.admin.get();
const passwords = {};
const accounts = (admin.accounts || []).map(account => {
const box = instance.$(`.email-to-card-account[data-id="${account.id}"]`);
const out = { id: account.id };
box.find('.js-etc-account').each((i, el) => {
const key = el.dataset.key;
out[key] = key === 'secure' || key === 'enabled' ? el.value === 'true' : el.value;
});
const password = box.find('.js-etc-account-password').val();
if (password) passwords[account.id] = password;
return out;
});
return {
enabled: admin.enabled === true,
domain: String(instance.$('.js-etc-domain').val() || admin.domain || '').trim(),
webhookSecret: String(instance.$('.js-etc-webhook-secret').val() || ''),
clearWebhookSecret: instance.clearSecret.get(),
accounts,
passwords,
};
}
Template.emailToCardSettings.onCreated(function () {
this.admin = new ReactiveVar(null);
this.address = new ReactiveVar(null);
this.message = new ReactiveVar('');
this.clearSecret = new ReactiveVar(false);
if (this.data.scope === 'admin') {
Meteor.call('getInboundMailSettings', (error, result) => {
if (error) this.message.set(errorText(error));
else this.admin.set(result);
});
}
if (this.data.scope === 'board') loadAddress(this);
});
Template.emailToCardSettings.helpers({
isAdminScope() { return Template.instance().data.scope === 'admin'; },
inboundLimitsData() {
return {
section: 'inboundEmail',
fields: AUTH_CONFIG_SECTIONS.inboundEmail.fields,
loadMethod: 'getAuthConfigSources',
saveMethod: 'saveAuthConfigSettings',
};
},
isBoardScope() { return Template.instance().data.scope === 'board'; },
isMemberScope() { return Template.instance().data.scope === 'member'; },
isSandstorm() { return !!Meteor.settings?.public?.sandstorm; },
admin() { return Template.instance().admin.get(); },
clearSecret() { return Template.instance().clearSecret.get(); },
message() { return Template.instance().message.get(); },
address() { return Template.instance().address.get() || {}; },
addressOffKey() {
const address = Template.instance().address.get();
return `email-to-card-off-${(address && address.reason) || 'admin-off'}`;
},
sandstormBoards() {
const admin = Template.instance().admin.get() || {};
const chosen = admin.sandstormBoard && admin.sandstormBoard.boardId;
const user = ReactiveCache.getCurrentUser();
const boards = ReactiveCache.getBoards({ archived: false, type: 'board', 'members.userId': user && user._id },
{ sort: { title: 1 } }) || [];
return boards.map(b => ({ _id: b._id, title: b.title, selected: b._id === chosen }));
},
canEditBoard() {
const user = ReactiveCache.getCurrentUser();
return !!(user && (user.isBoardAdmin() || user.isAdmin));
},
boardEnabled() { return boardEmailToCard().enabled === true; },
boardLists() {
const board = currentBoard();
if (!board) return [];
const chosen = boardEmailToCard().listId;
return (ReactiveCache.getLists({ boardId: board._id, archived: false }, { sort: { sort: 1 } }) || [])
.map(l => ({ _id: l._id, title: l.title, selected: l._id === chosen }));
},
boardSwimlanes() {
const board = currentBoard();
if (!board) return [];
const chosen = boardEmailToCard().swimlaneId;
return (ReactiveCache.getSwimlanes({ boardId: board._id, archived: false }, { sort: { sort: 1 } }) || [])
.map(s => ({ _id: s._id, title: s.title, selected: s._id === chosen }));
},
memberEnabled() {
const user = ReactiveCache.getCurrentUser();
const value = user && user.profile && user.profile.emailToCard && user.profile.emailToCard.enabled;
return value === true || value === false ? String(value) : '';
},
partRows() {
const instance = Template.instance();
const scope = instance.data.scope;
const parts = storedParts(scope);
const user = ReactiveCache.getCurrentUser();
const readOnly = scope === 'board' && !(user && (user.isBoardAdmin() || user.isAdmin));
return Object.keys(EMAIL_PARTS).map(name => ({
name, labelKey: PART_LABELS[name], readOnly, options: partOptions(name, parts[name]),
}));
},
});
function partValue(name, raw) {
if (raw === '') return undefined;
return Array.isArray(EMAIL_PARTS[name]) ? raw : raw === 'true';
}
Template.emailToCardSettings.events({
// --- Admin Panel ---
'click .js-etc-admin-enabled'(event, instance) {
event.preventDefault();
const admin = instance.admin.get();
instance.admin.set({ ...admin, ...readAdminForm(instance), enabled: !admin.enabled });
},
'click .js-etc-clear-secret'(event, instance) {
event.preventDefault();
instance.clearSecret.set(!instance.clearSecret.get());
},
'click .js-etc-add-account'(event, instance) {
event.preventDefault();
const admin = instance.admin.get();
const form = readAdminForm(instance);
const accounts = form.accounts.map(a => ({ ...(admin.accounts || []).find(o => o.id === a.id), ...a }));
accounts.push({ id: Random.id(12), label: '', host: '', port: 993, secure: true, username: '',
mailbox: 'INBOX', after: 'seen', enabled: true });
instance.admin.set({ ...admin, domain: form.domain, accounts });
},
'click .js-etc-remove-account'(event, instance) {
event.preventDefault();
const id = event.currentTarget.closest('.email-to-card-account').dataset.id;
const admin = instance.admin.get();
instance.admin.set({ ...admin, accounts: (admin.accounts || []).filter(a => a.id !== id) });
},
'click .js-etc-admin-save'(event, instance) {
event.preventDefault();
Meteor.call('saveInboundMailSettings', readAdminForm(instance), (error, result) => {
if (error) {
instance.message.set(errorText(error));
return;
}
instance.clearSecret.set(false);
instance.$('input[type="password"]').val('');
instance.admin.set({ ...instance.admin.get(), ...result });
instance.message.set(TAPi18n.__('email-to-card-saved'));
});
},
'change .js-etc-sandstorm-board'(event, instance) {
Meteor.call('setSandstormEmailBoard', event.currentTarget.value, (error, result) => {
if (error) instance.message.set(errorText(error));
else instance.admin.set({ ...instance.admin.get(), sandstormBoard: result });
});
},
// --- Board Settings ---
'click .js-etc-board-enabled'(event, instance) {
event.preventDefault();
saveBoard(instance, { enabled: !boardEmailToCard().enabled });
},
'change .js-etc-board-place'(event, instance) {
saveBoard(instance, { [event.currentTarget.dataset.key]: event.currentTarget.value || null });
},
'click .js-etc-new-address'(event, instance) {
event.preventDefault();
Meteor.call('emailToCardNewAddress', Utils.getCurrentBoardId(), (error, result) => {
if (error) instance.message.set(errorText(error));
else instance.address.set(result);
});
},
// --- Member Settings ---
'change .js-etc-member-enabled'(event, instance) {
const raw = event.currentTarget.value;
saveMember(instance, { enabled: raw === '' ? null : raw === 'true' });
},
// --- both ---
'change .js-etc-part'(event, instance) {
const name = event.currentTarget.dataset.part;
const parts = { ...storedParts(instance.data.scope) };
const value = partValue(name, event.currentTarget.value);
if (value === undefined) delete parts[name];
else parts[name] = value;
if (instance.data.scope === 'board') saveBoard(instance, { parts });
else saveMember(instance, { parts });
},
});