Hi!
I am trying to implement a custom EventListener to prevent restricted users from changing some specific properties.
I mostly used the documentation and the example on this page:
https://extensions.xwiki.org/xwiki/bin/view/Extension/Script%20Component/
I also had a look at several Script snippets, but couldn’t find a solution, why the Listener is not fired at all.
What I want to do
- If a document is updated, it should be checked if it is a user profile
- If the user is in one of the “RESTRICTED_GROUP”, it should reset some properties to a given value.
What I did:
- I installed the Script Component extension
- I added a ScriptComponentClass object to my FlamingoTheme page (since this is always loaded) with Language “groovy” and Scope “global”
I use this code, mainly from the examples with some changes
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.bridge.event.DocumentUpdatingEvent;
import org.xwiki.observation.AbstractEventListener;
import org.xwiki.observation.event.Event;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import java.util.Arrays;
import java.util.List;
@Component
@Named("preventRestrictedUserChangesListener")
@Singleton
public class preventRestrictedUserChanges extends AbstractEventListener {
static final List<String> RESTRICTED_GROUPS = Arrays.asList(
"XWiki.GroupA", "XWiki.GroupB", "XWiki.GroupC", "XWiki.GroupD"
);
public preventRestrictedUserChanges() {
super("preventRestrictedUserChangesListener", new DocumentUpdatingEvent());
}
@Override
public void onEvent(Event event, Object source, Object data) {
XWikiContext xcontext = (XWikiContext) data;
XWikiDocument doc = (XWikiDocument) source;
String currentUser = xcontext.getUser();
boolean isRestrictedUser = RESTRICTED_GROUPS.stream().anyMatch(group ->
xcontext.getWiki().getGroupService(xcontext).isMember(currentUser, group)
);
if (isRestrictedUser) {
BaseObject userObj = doc.getXObject("XWiki.XWikiUsers");
if (userObj != null) {
userObj.set("displayHiddenDocuments", "0", xcontext);
userObj.set("usertype", "Simple", xcontext);
}
}
}
}
The problem is that it is not fired at all.
Anyone any idea what is wrong here?
Thanks in advance
Dalli
5 posts - 2 participants