If user belongs to group, then

I have looked for ages, played with the velocity code, and tinkered for hours. Finally I came up with this:


= Check Group Membership
== Your are logged in as: $xcontext.user


====(i) Please note, you must enter the username WITHOUT the dot ('.') and must be LOWERCASE
====(i) Also worth mentioning the GROUPS are CASE SeNsItIvE
====(i) Lastly, do not preceed your username with the 'XWiki.' It's not necessary. 



<form action="$doc.getURL("view")" method="post">
<input type="hidden" name="action" value="null" />
User: <input type="text" name="user" value="$!request.user" size="60" /><br/><br/>
Group: <input type="text" name="group" value="$!request.group" size="60" /><br/><br/>
<input type="submit" value="Check Group Membership" onclick="document.getElementById('bequiet').style.visibility='visible';this.form.action.value='CheckGroupMembership'" /><br/>
<div id="bequiet" style="visibility:hidden">Working...</div>


#if(($request.action) && ($request.action == 'CheckGroupMembership')) 

#set($group = "XWiki.${request.group}")
#set($user = "XWiki.${request.user}")


(y) YES User $user IS in group $group


(n) NO User $user NOT in group $group




Paste that into a page and you can use it to check instantly. Please note, I use dots in the user names because I authenticate my users against active directory’s sAMAccountName, so the first warning may not apply to everyone.

Credit goes to this guy for the original code: http://xwiki.475771.n2.nabble.com/Group-Members-in-Velocity-tp2641137p2642207.html

My intention is, if a user belongs to a certain group, then display the appropriate menu (for a sub-wiki) in the left pane.

The code for my menu is as follows:


#set ($readgroup = $xwiki.getUser($xcontext.user).isUserInGroup('XWiki.DEMORead'))
#set ($editgroup = $xwiki.getUser($xcontext.user).isUserInGroup('XWiki.DEMOEdit'))

#if($editgroup || $readgroup || $hasAdmin)

## Escape special characters in macro parameter values.
#set ($openToDoc = $doc.documentReference.toString().replaceAll('([~"])', '~$1'))
{{documentTree showTranslations="false" showRoot="false" finder="true" showAttachments="false" compact="true" showWikis="true" root="wiki:demo" openTo="document:$openToDoc" /}}



PS: The link below did not work for me: https://www.xwiki.org/xwiki/bin/view/FAQ/HowCanIMakeContentAppearOnlyForASpecificGroup

The above code works for me… for the most part.
The problem I am facing now is, the title of the menu appears in the left pane when the user logged in doesn’t have access to it. So they know that the menu exists, but there are no links.

How do I remove the name of the menu? At the moment, the Menu Visibility Scope is set to ‘Current Wiki’, but when I change the menu visibility scope to ‘Global’, it disappears all together… Where is the logic in that?!.

I hope my question makes sense. And I hope the above code will provide useful to others. Sharing is caring! :smiley:


Just tested this code again and it worked very well for me. Could you elaborate on what’s not working and show us how you’ve used it? Maybe you didn’t put the right Group reference?


You have to know that for the “global” scope you need to have Programming Rights (and yes we need to be more explicit about this in the UI, would be great if you could create a jira issue about it - I don’t remember if we have one). The “wiki” scope only requires Admin right.

Also note that if you want to use menus as side panels, you could also simply use panels http://extensions.xwiki.org/xwiki/bin/view/Extension/Panels%20Application I don’t know about menus but with panels it’s easy to decide whether or not to display the title programmatically.

Just tested and it seems menu title is taken from the page name so you can’t not have it… So better use Panels for your use case.