I have looked for ages, played with the velocity code, and tinkered for hours. Finally I came up with this:
{{velocity}}
= Check Group Membership
== Your are logged in as: $xcontext.user
{{warning}}
====(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.
{{/warning}}
{{html}}
<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>
</form>
{{/html}}
#if(($request.action) && ($request.action == 'CheckGroupMembership'))
#set($group = "XWiki.${request.group}")
#set($user = "XWiki.${request.user}")
#if($xwiki.getUser($user).isUserInGroup($group))
{{success}}
(y) YES User $user IS in group $group
{{/success}}
#else
{{error}}
(n) NO User $user NOT in group $group
{{/error}}
#end
#end
{{/velocity}}
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:
{{velocity}}
#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" /}}
#end
{{/velocity}}
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!
Cheers.