Login  Register

Re: Testing if user is in a group

Posted by Hugo <Nabble> on Nov 11, 2011; 3:55am
URL: https://support.nabble.com/Testing-if-user-is-in-a-group-tp6956317p6984287.html

The ajax solution would work, but the easiest solution is certainly to create a replica of the visible_for_admins macro. To do this, you can go to the advanced editor, paste the code of the visible_for_admins macro, rename it and customize it. Here is an example:
<macro name="visible_for_group" parameters="group_name,selector">
  <n.put_in_head.>
    <script type="text/javascript">
      var group_members = [<n.compress.>
		<n.users_in_group. group="[n.group_name/]">
			<n.loop.>
			  <n.if.not.is_first_element>
				<then>,</then>
			  </n.if.not.is_first_element>
			  <n.current_user.id/>
			</n.loop.>
		  </n.users_in_group.>
	  </n.compress.>];
    </script>
  </n.put_in_head.>
  <script type="text/javascript">
    $(document).ready(function() {
      if (Nabble.userId && group_members.indexOf(Number(Nabble.userId)) >= 0)
        $('<n.selector/>').show();
    });
  </script>
</macro>
So this macro receives the name of the group as a parameter. So you can call it like this:
<n.visible_for_group group_name="members" selector="#mydiv"/>
Note that this implementation only changes the visibility of a HTML element on the page. This is probably good enough for what you need.