Files
wekan/server/methods/instanceFeatures.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

167 lines
8.5 KiB
JavaScript

// #6736: Admin Panel / Settings / Visibility / Features. Site admin only.
// The decisions are models/lib/instanceFeatures.js; this stores them and the
// pilot users who preview disabled features (a flag on the user, published
// only to that user, so nobody learns who the pilots are).
import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import Settings from '/models/settings';
const { featureSettingsModifier, parsePilotUsernames } = require('/models/lib/instanceFeatures');
const { cardFieldStatesValue } = require('/models/lib/cardFieldVisibility');
const { cleanOptionalApis } = require('/models/lib/optionalApis');
async function requireSiteAdmin() {
const user = await Meteor.userAsync();
if (user?.isAdmin !== true) throw new Meteor.Error('error-notAuthorized');
return user;
}
Meteor.methods({
// The usernames of the current pilot users, for the page's text box.
async getFeaturePilotUsernames() {
await requireSiteAdmin();
const users = await Meteor.users.find({ featurePreview: true },
{ fields: { username: 1 }, sort: { username: 1 }, limit: 500 }).fetchAsync();
return users.map(user => user.username).filter(Boolean);
},
// Saves every feature's decision, the two policies and the pilot users.
// Unknown usernames are returned rather than silently ignored.
// Admin Panel / Settings / Visibility / Optional APIs: which compatible
// APIs (Trello, Jira, GitHub, Gitea) answer. Site admins only; unknown keys
// are dropped and every value is a boolean (models/lib/optionalApis.js).
async saveOptionalApis(input) {
await requireSiteAdmin();
check(input, Object);
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
const optionalApis = cleanOptionalApis(input);
await Settings.updateAsync(setting._id, { $set: { optionalApis } });
return optionalApis;
},
// #2217: Admin Panel / Settings / Visibility - boards may show their Rules
// to members, read-only (models/lib/rulesReadOnly.js). Site admins only.
async saveRulesReadOnlyForMembers(enabled) {
await requireSiteAdmin();
check(enabled, Boolean);
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
await Settings.updateAsync(setting._id, { $set: { rulesReadOnlyForMembers: enabled } });
return enabled;
},
async saveInstanceFeatures(input) {
await requireSiteAdmin();
check(input, {
states: Object,
approvalRequired: Boolean,
previewAdmins: Boolean,
pilotUsernames: Match.Optional(String),
// The card fields every board shows (models/lib/cardFieldVisibility.js).
cardFields: Match.Optional(Object),
});
const { pilotUsernames, cardFields, ...decisions } = input;
const modifier = featureSettingsModifier(decisions);
if (modifier.error) throw new Meteor.Error('invalid-features', modifier.error);
if (cardFields !== undefined) {
const value = cardFieldStatesValue(cardFields);
if (value.error) throw new Meteor.Error('invalid-card-fields', value.error);
modifier.$set.cardFieldStates = value.cardFieldStates;
}
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
await Settings.updateAsync(setting._id, modifier);
let unknownUsernames = [];
if (typeof pilotUsernames === 'string') {
const names = parsePilotUsernames(pilotUsernames);
const pilots = names.length ? await Meteor.users.find({ username: { $in: names } },
{ fields: { _id: 1, username: 1 } }).fetchAsync() : [];
const ids = pilots.map(user => user._id);
unknownUsernames = names.filter(name => !pilots.some(user => user.username === name));
await Meteor.users.updateAsync({ featurePreview: true, _id: { $nin: ids } },
{ $unset: { featurePreview: '' } }, { multi: true });
if (ids.length) {
await Meteor.users.updateAsync({ _id: { $in: ids } }, { $set: { featurePreview: true } }, { multi: true });
}
}
return { saved: true, unknownUsernames };
},
});
// #6837: feature profiles (models/lib/featureProfiles.js). Site admin only.
// The page names teams, organizations and users; they are resolved here, and a
// name that matches nothing is returned rather than silently dropped.
const { cleanProfiles, storedProfiles, parseNames } = require('/models/lib/featureProfiles');
const { FEATURE_KEYS } = require('/models/lib/instanceFeatures');
Meteor.methods({
// The profiles with the names of what they apply to, for the page.
async getFeatureProfiles() {
await requireSiteAdmin();
const Org = require('/models/org').default;
const Team = require('/models/team').default;
const setting = await Settings.findOneAsync({});
const profiles = storedProfiles(setting);
const allTeamIds = profiles.flatMap(p => p.teamIds || []);
const allOrgIds = profiles.flatMap(p => p.orgIds || []);
const [teams, orgs, users] = await Promise.all([
Team.find({ _id: { $in: allTeamIds } }, { fields: { teamDisplayName: 1 } }).fetchAsync(),
Org.find({ _id: { $in: allOrgIds } }, { fields: { orgDisplayName: 1 } }).fetchAsync(),
Meteor.users.find({ featureProfileId: { $in: profiles.map(p => p._id) } },
{ fields: { username: 1, featureProfileId: 1 }, sort: { username: 1 }, limit: 5000 }).fetchAsync(),
]);
const teamName = new Map(teams.map(t => [t._id, t.teamDisplayName]));
const orgName = new Map(orgs.map(o => [o._id, o.orgDisplayName]));
return profiles.map(p => ({
_id: p._id, name: p.name, hidden: p.hidden || [],
teams: (p.teamIds || []).map(id => teamName.get(id)).filter(Boolean),
orgs: (p.orgIds || []).map(id => orgName.get(id)).filter(Boolean),
usernames: users.filter(u => u.featureProfileId === p._id).map(u => u.username).filter(Boolean),
}));
},
// Saves every profile: [{ _id?, name, hidden: [key], teams, orgs, usernames }]
// with teams/orgs/usernames as text, one name per line or comma. A user is in
// at most one profile directly: listed in two, the later one wins.
async saveFeatureProfiles(input) {
await requireSiteAdmin();
check(input, [Match.ObjectIncluding({ name: String, hidden: [String],
teams: Match.Optional(String), orgs: Match.Optional(String), usernames: Match.Optional(String) })]);
const Org = require('/models/org').default;
const Team = require('/models/team').default;
const { Random } = require('meteor/random');
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
const unknown = { teams: [], orgs: [], usernames: [] };
const resolve = async (names, collection, field, missing) => {
if (!names.length) return [];
const found = await collection.find({ [field]: { $in: names } }, { fields: { [field]: 1 } }).fetchAsync();
for (const name of names) if (!found.some(doc => doc[field] === name)) missing.push(name);
return found.map(doc => doc._id);
};
const prepared = [];
for (const row of input) {
prepared.push({ _id: row._id, name: row.name, hidden: row.hidden,
teamIds: await resolve(parseNames(row.teams), Team, 'teamDisplayName', unknown.teams),
orgIds: await resolve(parseNames(row.orgs), Org, 'orgDisplayName', unknown.orgs),
usernames: parseNames(row.usernames) });
}
const cleaned = cleanProfiles(prepared, FEATURE_KEYS, () => Random.id());
if (cleaned.error) throw new Meteor.Error(cleaned.error, cleaned.keys ? cleaned.keys.join(', ') : cleaned.name);
// Users: who is in which profile now. Everybody else loses a direct one.
const assigned = new Map();
cleaned.profiles.forEach((profile, i) => { for (const name of prepared[i].usernames) assigned.set(name, profile._id); });
const users = assigned.size
? await Meteor.users.find({ username: { $in: [...assigned.keys()] } }, { fields: { username: 1 } }).fetchAsync() : [];
for (const name of assigned.keys()) if (!users.some(u => u.username === name)) unknown.usernames.push(name);
await Settings.updateAsync(setting._id, { $set: { featureProfiles: cleaned.profiles } });
await Meteor.users.updateAsync({ featureProfileId: { $exists: true }, _id: { $nin: users.map(u => u._id) } },
{ $unset: { featureProfileId: '' } }, { multi: true });
for (const user of users) {
await Meteor.users.updateAsync(user._id, { $set: { featureProfileId: assigned.get(user.username) } });
}
return { profiles: cleaned.profiles.length, unknown };
},
});