Looks like a bit of a hole in the APIs, along with a bit of confusion as
to how to proceed. Actually, you're lucky: I didn't find the
addUserToGroup method, and had to hack it by hand.
The way it's done is to find an XWikiObject of class "XWiki.XWikiGroups"
in the document whose name is that of the group and remove that object
from that document.
For example, If user 'XWiki.GunterLeeb' is a member of a group named
'XWiki.Dept999Group', that is represented by a document named
XWiki.Dept999Group containing an XWiki.XWikiGroups object whose 'member'
field contains the value 'XWiki.GunterLeeb'. Simple, no?
To remove XWiki.GunterLeeb from XWiki.Dept999Group, you must do
something like this (assuming that context is the XWikiContext passed to
you and xwiki is the return from context.getWiki()):
String groupName = "Xwiki.Dept999Group";
String wikiName = "XWiki.GunterLeeb";
String groupClassName = xwiki.getGroupClass().getName();
// Get the XWiki document holding the objects comprising the group
membership list
XWikiDocument groupDoc = xwiki.getDocument(groupName, context);
// Get the specific group membership object for the user
BaseObject groupObj = groupDoc.getObject(groupClassName, "member",
wikiName);
/*
* This code imitates something I found in the XWiki code that
seemed
* to be necessary, but appears superfluous: get all the group
membership
* objects as a Vector and null the entry containing this one
*/
Vector objects = doc.getObjects(groupClassName);
objects.set(obj.getIntValue("id"), null);
// This removes the object from the group document.
groupDoc.addObjectsToRemove(groupObj);
xwiki.saveDocument(doc, context);
/*
* This may not be necessary, but the group service does have
* a cache, and I've found that adding or removing entries
* sometimes don't take effect if I don't do this.
*/
xwiki.getGroupService().flushCache();
This is how my servlet filter accomplishes this; if any of the
developers can critique this, I'd appreciate it, especially the bits
that looked just a little suspicious to me.
brain[sic]
-----Original Message-----
From: Gunter Leeb [mailto:Gunter.Leeb@mediaprint.at]
Sent: Monday, April 09, 2007 1:48 AM
To: xwiki-users(a)objectweb.org
Subject: [xwiki-users] How to remove a user from a group?
Hi,
I found the GroupService as in:
context.getWiki().getGroupService(context).addUserToGroup(user,
database, group, context);
This GroupService interface allows me to locate users in a
group and even add a user to a group.
I don't see a method to remove a user from an XWiki group.
How to do that in plug-in code?
Regards,
Gunter
--------------------------------------------------------------
-----------------
Diese E-Mail enthaelt vertrauliche und/oder rechtlich
geschuetzte Informationen. Wenn Sie nicht der richtige
Adressat sind oder diese E-Mail irrtuemlich erhalten haben,
informieren Sie bitte sofort den Absender und vernichten Sie
diese Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser Mail ist nicht gestattet.
The information transmitted is intended only for the person
or entity to which it is addressed and may contain
confidential and/or privileged material. Any review,
retransmission, dissemination or other use of, or taking of
any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited. If
you received this in error, please contact the sender and
delete the material from any computer.
--------------------------------------------------------------
-----------------