The forum of the forums
Welcome to the Official Support Forum of Forumotion!

To take full advantage of everything offered by our forum, please log in if you are already a member, or join our community if you've not yet.



Create a free forum like this one.

How to test for group membership

View previous topic View next topic Go down

Solved How to test for group membership

Post by ian2000 on May 12th 2015, 11:09 pm

On a custom HTML page, is there code I can use to test if a user is in a group?

I would like to have a button that, when clicked, will take the user to one of two places depending on whether they are in a certain group or not.

Is this possible?

Thanks!

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 13th 2015, 1:36 am

Well anything is possible, it depends exactly what you want to accomplish. It could be as simple as adding a sinker (link) to the more advanced stuff like Ajax (which i have 0 knowledge of) and we could have Ange or someone help ya with that.

May you go into further detail what you want? What are the 2 links you want them to go to? Which groups would you like to see what?

-Brandon


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 13th 2015, 4:46 am

So I want to have a button that is essentially a link to a page. If the user is in group ABC it will take them to page X. If the user is NOT in group ABC, it will take them to page Y.

Does that help?

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 13th 2015, 5:05 am

What are the links to the pages? Also may you include any cuurent html you have?

-Brandon


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 13th 2015, 8:54 am

I mean the links would just be to any two pages that doesn't really matter too much. My only question is how do I specifically test group membership, aka how do I write code to test if the user is a member of group ABC (simply to return a true or false)

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 13th 2015, 1:07 pm

Well wouldnt you know who is a member of which group on the forum?

Yeah the html should be the easy part... However i am still a tad confused on what exactly what you want to accomplish?


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 13th 2015, 10:22 pm

I don't know how I can explain it differently... My question is that I need help writing a piece of code that will return true or false if a user is in a group.

So it might look like this:
If (userIsInGroupModerator)
{
Do this
} else {
or this
}

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 14th 2015, 1:12 pm

Yeah this is going to require a little more then html, probably will require atleast Javascript. I can help with the html parts, but if @Ange Tuteur can help with the javascript, it would be greatly apperciated as my skill level with code is not that high up yet Sad.

-Brandon


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by Ange Tuteur on May 14th 2015, 3:25 pm

Hi @ian2000,

You can send an AJAX request to the /groups page, then manipulate the data as you need. Here's a quick example :
Code:
<script type="text/javascript">// <![CDATA[
(function($) {
  $.get('/groups', function(d) {
    if ($('a[href*="/g1-"]', d)[0]) alert('ADMIN');
    else if ($('a[href*="/g2-"]', d)[0]) alert('MOD');
    else alert('MEMBER');
  });
})(jQuery);
// ]]></script>
( NOTE : Remove the script tags if you're placing this in JS code management )

You'll see I'm querying the selector a[href*="/g1-"] in the returned data accessiable by the argument d ( short for data ). g1 would be the ID of the group I'm looking for, and as long as you don't have widgets on the side that link to groups, this method should work fine. Smile

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 15th 2015, 12:04 am

This is super helpful @Ange Tuteur! Exactly what I needed help with! Your code works perfectly.

However I'm having trouble understanding something. I have some a piece in my HTML:
Code:
<p>I want <b id="test">THIS</b> to change</p>

And I slightly modified your code, only replacing one of your alerts with:

Code:
document.getElementById('test').innerHTML = "ABC"

And the result is:

Code:

<script type="text/javascript">// <![CDATA[
(function($) {
 $.get('/groups', function(d) {
  if ($('a[href*="/g1-"]', d)[0]) document.getElementById('test').innerHTML = "ABC";
  else if ($('a[href*="/g2-"]', d)[0]) alert('MOD');
  else alert('MEMBER');
 });
})(jQuery);
// ]]></script>
Could you help me figure out why I can't get this to work though? If I'm thinking about this correctly, I should get the "THIS" to change to "ABC" only ifthe user is an admin... But nothing happens.

"MOD" and "MEMBER" are still alerted as normal, though so thanks for that! Smile


Last edited by ian2000 on May 15th 2015, 9:58 am; edited 1 time in total

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 15th 2015, 12:46 am

Where did you install it? In the html page?


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 15th 2015, 10:00 am

It was placed in a small HTML page and the code that is relevant is in my post Smile

Thanks

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by brandon_g on May 15th 2015, 12:23 pm

Did you try Ange's code as is?


Remember to mark your topic when a solution is found.

brandon_g
Support Moderator
Support Moderator

Male Posts : 5144
Reputation : 478
Language : English
Location : USA

http://broadcastingduo.forumotion.com/

Back to top Go down

Solved Re: How to test for group membership

Post by Ange Tuteur on May 15th 2015, 4:46 pm

It should still work, unless the link to /g1- is not displaying on the /groups page. Here's an example similar to what you're trying to achieve :
Code:
<p>Your user-level is : <b id="test">undefined</b></p>

<script type="text/javascript">// <![CDATA[
(function($) {
  var node = document.getElementById('test');
 
  $.get('/groups', function(d) {
    if ($('a[href*="/g1-"]', d)[0]) node.innerHTML = 'Administrator';
    else if ($('a[href*="/g2-"]', d)[0]) node.innerHTML = 'Moderator';
    else node.innerHTML = 'User';
  });
})(jQuery);
// ]]></script>

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

Back to top Go down

Solved Re: How to test for group membership

Post by ian2000 on May 19th 2015, 10:33 pm

This code works wonderfully! Thanks for your support, awesome as usual @AngeTuteur!

I learned so much from this example.

ian2000
Forumember

Posts : 38
Reputation : 3
Language : English

http://onyxlegion.forumotion.net/

Back to top Go down

Solved Re: How to test for group membership

Post by Ange Tuteur on May 20th 2015, 7:41 pm

You're welcome ^^

Topic archived

See you bye

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum