Re: Testing if user is in a group
Posted by
DavidJCobb on
Nov 04, 2011; 7:40am
URL: https://support.nabble.com/Testing-if-user-is-in-a-group-tp6956317p6961889.html
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.