The forum of the forums

Would you like to react to this message? Create an account in a few clicks or log in to continue.
The forum of the forums
3 posters

    How to test for group membership

    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved How to test for group membership

    Post by ian2000 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!
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

    Post by brandon_g 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



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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?
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

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

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

    -Brandon



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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)
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

    Post by brandon_g 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?



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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
    }
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

    Post by brandon_g 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



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: How to test for group membership

    Post by Ange Tuteur 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
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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 if the 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
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

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

    Where did you install it? In the html page?



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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
    brandon_g
    brandon_g
    Manager
    Manager


    Male Posts : 10113
    Reputation : 923
    Language : English
    Location : USA

    Solved Re: How to test for group membership

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

    Did you try Ange's code as is?



    check group - How to test for group membership Brando10
    Remember to mark your topic check group - How to test for group membership Solved15 when a solution is found.
    General Rules | Tips & Tricks | FAQ | Forgot Founder Password?

    check group - How to test for group membership Scre1476
    Team Leader
    Review Section Rules | Request A Review | Sticker Points
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: How to test for group membership

    Post by Ange Tuteur 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>
    avatar
    ian2000
    Forumember


    Posts : 42
    Reputation : 3
    Language : English

    Solved Re: How to test for group membership

    Post by ian2000 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.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: How to test for group membership

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

    You're welcome ^^

    Topic archived

    See you bye

      Current date/time is November 13th 2024, 7:01 pm