Testing if user is in a group

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing if user is in a group

iamthemik3
How would I go about testing whether a user is in a particular group? I want to conditionally display content based on a user's membership.
I found the "n.visible_for_admins" macro but that only applies to the administrators group. Is there a more generic version of this?
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

GregChapman
Have you described the problem fully?

On the basis of what you set out it can be done with User Groups and Permissions and you don't need to delve into NAML at all.
Volunteer Helper - but recommending that users move off the platform!
Once the admin for GregHelp now deleted.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

iamthemik3
Not exactly what I meant. I want to be able to display different content on a page based on the user's group memberships.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

GregChapman
What type of content?

Posts made in different sub-forums can be made to display to designated user groups only, by use of user permissions. You don't need to play with NAML for that.

Why can't your "different content" be posted to a sub-forum only viewable by the members of certain designated groups?

You still need to be more precise about what you are trying to do.

Certainly, simple standard Permissions works for me, without editing any NAML, in one of my "Mixed" forums. I have a "Moderated Posts" sub-forum that is only seen by those in the "Administrators" and my "Moderators" group. I do have to turn on OPTIONS > APPLICATIONS > EXTRAS & ADDONS > SMART APPLICATION PAGES to stop the normal caching process that affects Mixed applications. If I don't the "Moderated Posts" forum is seen by those that should not see it.
Volunteer Helper - but recommending that users move off the platform!
Once the admin for GregHelp now deleted.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

iamthemik3
Sorry for being vague.
My application's top node serves as a landing page without any forum content displayed (just the node's description). If I need to change the landing page content, I use the "options" dropdown menu to edit the page description. Since there really isn't any use for the options menu for non-administrators, I'd like to hide the menu to general members. For now i've been using the <n.visible_for_admins> macro to hide the button however I would like certain power-user groups to be able to see the dropdown menu as well (basically sub-admins with restricted rights to certain parts of the forums).
This example isn't all too big of a deal since it is only a minor cosmetic change however I do see this sort of thing being useful in the future.

Here is a link to my app:
http://people.rit.edu/aim6864/vovkulaky/
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

GregChapman
OK! Now I see where you're coming from and, hopefully, someone with more knowledge than I can help you find the macros you need.

As a BTW, I'm surprised at the "isn't any use for the options menu for non-administrators" statement. But then, I originally turned to Nabble as it was an excellent alternative to the mail list software I was using. I really hate the way that forum users can just "go missing". You never know if they have visited and read anything you post. At least with a mail list you can be pretty sure subscribers are reading posts, because if they lose interest they unsubscribe!
Volunteer Helper - but recommending that users move off the platform!
Once the admin for GregHelp now deleted.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

iamthemik3
"isn't any use for the options menu for non-administrators" only pertains to the menu on my front page
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

DavidJCobb
You could use JavaScript and a custom-made AJAX macro to do it. I just wrote a macro to test and it worked (but I'd recommend trying it on a test site before copying it to your main Nabble app); here's the NAML involved:
<!-- These are just two convenience macros to properly format JavaScript/JSON output. -->

<macro name="_ajax_format_user_id" dot_parameter="id" unindent="true">
  <n.if.not.regex_matches pattern="^\d+$" text="[n.id/]">
    <then>"<n.id/>"</then>
    <else><n.id/></else>
  </n.if.not.regex_matches>
</macro>
<macro name="js_string_or_null" dot_parameter="value" unindent="true">
  <n.if.not.is_null.value>
      <then>"<n.javascript_string_encode.><n.value/></n.javascript_string_encode.>"</then>
      <else>null</else>
    </n.if.not.is_null.value>
</macro>

<!-- This macro returns a response to an AJAX request for a list of all user IDs in a certain user group.
It uses the macros above, though since each is used only once, you could just copy their code into 
this macro to turn three small macros into one big macro. Should work the same.
Call like this:  $.getJSON("/template/NamlServlet.jtp?macro=ajax_get_users_in_group&group=Test Group"+Nabble.getClientID(),  callback);
 -->

<macro name="ajax_get_users_in_group" requires="servlet" parameters="group" unindent="true">
  <n.compress.>
    {
    "group": <n.js_string_or_null value="[n.get_parameter name='group'/]"/>,
    "users": [
    <n.users_in_group. group="[n.get_parameter name='group'/]">
      <n.loop.>
        <n.if.not.is_first_element>
          <then>,</then>
        </n.if.not.is_first_element>
        <n._ajax_format_user_id id="[n.current_user.id/]"/>
      </n.loop.>
    </n.users_in_group.>]
    }
  </n.compress.>
 </macro>

Where callback would be a JavaScript function accepting one argument, data, which would be the JSON returned by the macro. So you could put JavaScript somewhere on the page and have it do:

$(function(){ // delay until page loads
$.getJSON("/template/NamlServlet.jtp?macro=ajax_get_users_in_group&group=Test Group"+Nabble.getClientID(),
   function(data) {
      if (Nabble.userId) {
         for(var i=0;i<data.users.length;i++) {
            if (data.users[i] == Nabble.userId)
               return
         }
         // CODE TO HIDE THE DROP-DOWN GOES HERE
      }
   }
);
});

My own test of it is here. I put myself in a new group called "Test Group" and then queried the macro. You should see { "group": "Test Group", "users": [ 282473] } when viewing that link.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

Hugo <Nabble>
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.
Reply | Threaded
Open this post in threaded view
|

Re: Testing if user is in a group

iamthemik3
Exactly what I was looking for. Thanks Hugo