Files
wekan/models/lib/featureProfiles.js
T
Lauri Ojansivu fc21b0b177 Feature profiles: an administrator hides features from chosen users, teams and organizations.
Decided by the maintainer on 2026-11-18 (TODO.md): administrator-defined
profiles. Admin Panel / Settings / Visibility / Features gains Feature
profiles: a name, a tick for each optional feature the profile hides,
and the users, teams and organizations it applies to. A user's own
profile wins, then the first profile naming one of their teams, then one
naming one of their organizations.

Strictly opt-in, because hiding features by default was tried and
removed as a support nightmare: a user with no profile sees everything,
a profile only hides (no data or permission changes), and the member
menu tells the user which profile hides how many features. The decision
is models/lib/featureProfiles.js, applied in isFeatureAvailable, so
every existing place that hides a disabled feature follows it.

Profiles are stored in Settings (featureProfiles) and a direct
assignment on the user (featureProfileId, published to that user only
and not writable by them). tests/featureProfiles.test.cjs covers the
decision, cleaning, the admin-only methods and the wiring;
feature-profiles.e2e.js drives the page, the hidden board view and the
notice.

The 11 new strings are translated into every language. Low confidence:
 qu, rup, rn, rw, sah, se, sm, sn, ss, st (low); sd, so, si, scn (medium); sr uses standard Тимови/Организације; ff, fj, gn, gv, haw (low); ha, hsb, fur, gd, ga, fo (medium); ace, ak, ay, bm, bi, bo, bua (low); ba, bho, ar-DZ/ar-EG/ary dialect spelling (medium); iu, kl, ks, kw, lld, lg, ig, la (low); kok (medium); tig, ti, tlh, ve-PP, ve-CC, uz-AR, ve, ts, tn, to, tpi (low); szl (medium); wal, vo, zgh, wa, wo, wa-RR, yi, wuu-Hans (low); chr, dz, ee, csb, cv, co, ckb (low); cmn uses 配置 for profile; nah, nap, nd, nso, om, mai (low); mg, ny, pap, or_IN, ps (medium).

Thanks to mimZD and xet7 !

Fixes #6837.
2026-11-19 00:01:42 +02:00

115 lines
5.1 KiB
JavaScript

'use strict';
// #6837 (decided 2026-11-18): feature profiles. An administrator defines named
// profiles in Admin Panel / Settings / Visibility / Features, each naming the
// optional features (models/lib/instanceFeatures.js FEATURES) its users do not
// see, and assigns them to users, teams or organizations - to give new or
// occasional users a smaller WeKan.
//
// Strictly opt-in. A simple/advanced mode with features hidden by DEFAULT was
// tried on 2026-11-02 and removed: a feature missing by default is a support
// nightmare. So:
// * a user with no profile sees everything the instance offers - nothing
// changes for anybody until an administrator assigns a profile;
// * a profile only HIDES, like the instance switch: data is untouched and no
// permission changes, so a profile never grants or takes away access;
// * the user is told which profile they have (featureProfileNotice), so a
// hidden feature is never mistaken for a missing one.
//
// Which profile applies: the user's own (users.featureProfileId), else the
// first profile in the administrator's order naming one of the user's teams,
// else one naming one of their organizations, else none.
//
// Stored on the Settings document as featureProfiles: [{ _id, name, hidden,
// teamIds, orgIds }]. Pure CommonJS: the page, the save method, the client and
// server decisions and tests/featureProfiles.test.cjs all use these functions.
const MAX_PROFILES = 50;
const MAX_NAME = 100;
const MAX_GROUPS = 500;
const ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
const ids = (value, limit) => [...new Set((Array.isArray(value) ? value : [])
.filter(id => typeof id === 'string' && ID_RE.test(id)))].slice(0, limit);
// The profiles to store, cleaned: known features only, unique ids and names,
// bounded. `featureKeys` is the catalog (FEATURE_KEYS); `newId` makes an id
// for a profile that has none yet. Returns { profiles } or { error }.
function cleanProfiles(input, featureKeys, newId) {
if (!Array.isArray(input)) return { error: 'invalid' };
if (input.length > MAX_PROFILES) return { error: 'too-many-profiles' };
const profiles = [];
const seenIds = new Set();
const seenNames = new Set();
for (const raw of input) {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { error: 'invalid' };
const name = typeof raw.name === 'string' ? raw.name.trim().slice(0, MAX_NAME) : '';
if (!name) return { error: 'profile-name-required' };
if (seenNames.has(name.toLowerCase())) return { error: 'profile-name-taken', name };
seenNames.add(name.toLowerCase());
const id = typeof raw._id === 'string' && ID_RE.test(raw._id) ? raw._id : newId();
if (seenIds.has(id)) return { error: 'invalid' };
seenIds.add(id);
const hidden = Array.isArray(raw.hidden) ? raw.hidden : [];
const unknown = hidden.filter(key => !featureKeys.includes(key));
if (unknown.length) return { error: 'unknown-feature', keys: unknown };
profiles.push({ _id: id, name, hidden: featureKeys.filter(key => hidden.includes(key)),
teamIds: ids(raw.teamIds, MAX_GROUPS), orgIds: ids(raw.orgIds, MAX_GROUPS) });
}
return { profiles };
}
function storedProfiles(setting) {
const profiles = setting && setting.featureProfiles;
return Array.isArray(profiles) ? profiles.filter(p => p && typeof p._id === 'string') : [];
}
const groupIds = (list, field) => (Array.isArray(list) ? list : [])
.map(entry => entry && entry[field]).filter(id => typeof id === 'string' && id);
// The profile that applies to this user, or null.
function profileForUser(setting, user) {
if (!user) return null;
const profiles = storedProfiles(setting);
if (!profiles.length) return null;
if (typeof user.featureProfileId === 'string' && user.featureProfileId) {
const own = profiles.find(p => p._id === user.featureProfileId);
if (own) return own;
}
const teams = groupIds(user.teams, 'teamId');
const byTeam = profiles.find(p => (p.teamIds || []).some(id => teams.includes(id)));
if (byTeam) return byTeam;
const orgs = groupIds(user.orgs, 'orgId');
return profiles.find(p => (p.orgIds || []).some(id => orgs.includes(id))) || null;
}
// Whether this user's profile hides this catalog feature.
function hiddenByProfile(setting, user, key) {
const profile = profileForUser(setting, user);
return !!(profile && Array.isArray(profile.hidden) && profile.hidden.includes(key));
}
// What the user is told: their profile's name and how many features it hides,
// or null when nothing is hidden for them.
function featureProfileNotice(setting, user) {
const profile = profileForUser(setting, user);
if (!profile || !Array.isArray(profile.hidden) || !profile.hidden.length) return null;
return { name: profile.name, hiddenCount: profile.hidden.length };
}
// "alice, bob\ncarol" -> ['alice', 'bob', 'carol'], unique, bounded.
function parseNames(text, limit = MAX_GROUPS) {
if (typeof text !== 'string') return [];
return [...new Set(text.split(/[\n,;]+/).map(name => name.trim()).filter(Boolean))].slice(0, limit);
}
module.exports = {
MAX_PROFILES,
cleanProfiles,
storedProfiles,
profileForUser,
hiddenByProfile,
featureProfileNotice,
parseNames,
};