Files
wekan/server/groupMembers.js
T
Lauri Ojansivu d04b8444a6 All Boards: a tab per organization and team, where their admins manage members.
The left menu of All Boards gets a tab for each organization and team
the member is in. A tab shows the boards shared with that group (through
the facet filter of #4791) and who is in it; the group's admins -
orgs.$.isAdmin, and now teams.$.isAdmin for teams - and site admins add
members by user name and remove them there. A group whose membership
comes from the sign-in provider (LDAP and other directory sync) is shown
read-only, even to a site admin: WeKan reads directory groups and never
writes them back. A member sees only their own groups; organization and
team memberships stay out of what a client may write.

models/lib/groupMembers.js decides access and builds the membership
changes; tests/groupMembers.test.cjs covers admins, site admins, members,
strangers, a directory-synced group, double membership and the server's
checks. The new strings are English placeholders until the next
translation pass.

Thanks to xet7 !

Fixes #802.
2026-10-14 15:52:07 +03:00

60 lines
2.7 KiB
JavaScript

// #802: All Boards organization and team tabs - who is in a group, and its
// admins adding and removing members (models/lib/groupMembers.js).
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import Org from '/models/org';
import Team from '/models/team';
const { KINDS, groupAccess, addModifier, removeModifier } = require('/models/lib/groupMembers');
async function load(userId, kind, id) {
if (!userId || !KINDS[kind]) throw new Meteor.Error('not-authorized');
const group = await (kind === 'org' ? Org : Team).findOneAsync(id);
const user = await Meteor.users.findOneAsync(userId, { fields: { orgs: 1, teams: 1, isAdmin: 1 } });
return { group, user, access: groupAccess(user, kind, group) };
}
Meteor.methods({
async groupMembers(kind, id) {
check(kind, String);
check(id, String);
const { access } = await load(this.userId, kind, id);
if (access === 'none') throw new Meteor.Error('not-authorized');
const k = KINDS[kind];
const members = await Meteor.users.find({ [`${k.list}.${k.idKey}`]: id }, { fields: { username: 1, 'profile.fullname': 1, [k.list]: 1 }, sort: { username: 1 }, limit: 1000 })
.fetchAsync();
return { canManage: access === 'manage', members: members.map(u => ({ _id: u._id, username: u.username,
fullname: (u.profile || {}).fullname || '', isAdmin: !!(u[k.list] || []).find(m => m && m[k.idKey] === id && m.isAdmin) })) };
},
async addGroupMember(kind, id, username) {
check(kind, String);
check(id, String);
check(username, String);
const { group, access } = await load(this.userId, kind, id);
if (access !== 'manage') throw new Meteor.Error('not-authorized');
const target = await Meteor.users.findOneAsync({ username: username.trim() }, { fields: { orgs: 1, teams: 1 } });
if (!target) throw new Meteor.Error('user-not-found');
const modifier = addModifier(target, kind, group);
if (modifier) await Meteor.users.updateAsync(target._id, modifier);
return true;
},
async removeGroupMember(kind, id, userId) {
check(kind, String);
check(id, String);
check(userId, String);
const { group, access } = await load(this.userId, kind, id);
if (access !== 'manage') throw new Meteor.Error('not-authorized');
const target = await Meteor.users.findOneAsync(userId, { fields: { orgs: 1, teams: 1 } });
const modifier = target && removeModifier(target, kind, group);
if (modifier) await Meteor.users.updateAsync(target._id, modifier);
return true;
},
});
for (const name of ['groupMembers', 'addGroupMember', 'removeGroupMember']) {
DDPRateLimiter.addRule({ type: 'method', name, connectionId: () => true }, 20, 10000);
}