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:

{{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! :smiley:

Cheers.

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?

Thanks

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.

Almost necroposting I guess, but I chose to post anyway because I seldom face the login page while following links to XWiki wiki pages (see the following portion):

The first time I clicked that link I was presented the login page:

immagine

(I didn’t login)

the second and following times an error is shown:

immagine

Note: Thanks @watery . However this should probably be better posted in a new thread (and it’s related to xwiki.org, not to “Help/Discuss” category).

The reason for this new message is because 3 days ago I’ve removed some old committers from the committer group as they’re not active anymore. This broke all pages where they were the last author of and where the pages require script or programming rights. I’ll fix it. Thanks

1 Like

Fine, I’ll report it the next time I face it.

it’s fixed now btw :wink:

1 Like