How do I find out if current user belongs to a certain group.

When a user is logged in, I need some script to determine if he/she is a member of a certain group.


Example: if the current user is logged in the script needs to determine if the user is a member of the 'Sales' group.


Thanks

Can you give the bigger picture? would help knowing what you are trying to do.

I have a dashboard with a button. If the logged in user is part of the 'Sales' group, I want to show the button, otherwise hide the button.


if ( /* user is a member of 'Sales' group */ )

button1.hidden = false;

else

button1.hidden = true;

If you want the script to simply check the group membership, you can loop through all the groups the user is a member of and compare the group ID. It'll look something like this:


// Get the group ID from the details page for your specific group.

var groupID = '23422323-0cd3-42b7-baa6-8a73e00ba6c1';

var groupIDs = dundas.context.currentSession.groupMembershipIds;

for (i = 0; i < groupIDs.length; i++) {
if (groupIDs[i] == groupID)

{
button1.hidden = false;
break;

}
else

{
button1.hidden = true;

}
}


Another approach would be to use Custom Attributes. This approach will likely be beneficial when the case becomes more complicated (such as using multiple groups). After you have assigned a custom attribute to the group, you can read its value for the current session. You can find an example in the Dundas BI Script Library.