how to check if user is in group

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
AdamsUKMB
Registered User
Posts: 16
Joined: Mon Jun 22, 2015 1:53 am

how to check if user is in group

Post by AdamsUKMB »

Image
User avatar
bonelifer
Community Team Member
Community Team Member
Posts: 3504
Joined: Wed Oct 27, 2004 11:35 pm
Name: William
Contact:

Re: how to check if user is in group

Post by bonelifer »

This extension should help:
[BETA] Group Template Variables -->> viewtopic.php?f=456&t=2325391

Their example uses Group 5

Code: Select all

<!-- IF S_GROUP_5 --> This is only visible for members of the group with id 5 <!-- ENDIF -->
Change it like to for group 11:

Code: Select all

<!-- IF S_GROUP_11 --> This is only visible for members of the group with id 11 <!-- ENDIF -->
Knowledge Base | phpBB Board Rules | Search Customisation Database
Image
Please don't contact me via PM or email for phpBB support .
AdamsUKMB
Registered User
Posts: 16
Joined: Mon Jun 22, 2015 1:53 am

Re: how to check if user is in group

Post by AdamsUKMB »

Not quite dude. That checks the user that's logged in rather than a poster.
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: how to check if user is in group

Post by MuhClaren »

Here's how I would approach this. Keep in mind that there are always other ways, and my way may not be the best, as I'm still a novice at writing phpBB extensions.

Goal: Modify the posts template block

Overview:
1. Listen for event core.viewtopic_modify_post_row
2. When event happens, use a function to get poster group_id's.
3. Test if a specific group_id is in that result.
4. Create a template variable to hold the true / false condition from step 3.
5. Pass the new template variable back to phpBB via your event listener.
6. In your HTML template, test for the true/false condition of the new template variable and do something.

Narrative:
Write an extension which has an event listener that is bound to the event core.viewtopic_modify_post_row.

When that event fires, it gives your extension the ability to read / modify post data.

Bind a function to the event, and in that function get all group_id's for the poster, storing them in an array. Then, check the array to determine if a certain group_id exists or not. Set the results of that check into a new template variable. (see footnote)

Use array_merge PHP function in your listener to append the new variable to phpBB's core array.

At that point, you can check if the new template variable is true / false in your HTML template, and conditionally render a display element, such as a star icon / image.

Note:
A native phpBB function may already exist which does the work of getting the group_id's, I'm not sure. If so, one could probably just call that function instead of re-inventing the wheel.
User avatar
3Di
Former Team Member
Posts: 15900
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: how to check if user is in group

Post by 3Di »

The USERS_TABLE already contains the group_id value for each user.

Also read: viewtopic.php?f=461&t=2269361
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
User avatar
3Di
Former Team Member
Posts: 15900
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: how to check if user is in group

Post by 3Di »

Also, that function already exists
into includes/functions_user.php file: function group_memberships

your choice.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2780
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: how to check if user is in group

Post by kinerity »

If you'd like, you could probably do this everywhere by calling the core.modify_username_string event. Here's an example, but it's untested. The 5 in if condition is the group id and the + is what would be added to the end of the username.

Code: Select all

static public function getSubscribedEvents()
{
    return array(
        'core.modify_username_string'   => 'modify_username_string',
    );
}

public function modify_username_string($event)
{
    $user_id = $event['user_id'];
    $username_string = $event['username_string'];

    if (!function_exists('group_memberships'))
    {
        include($this->root_path . 'includes/functions_user.' . $this->php_ext);
    }
    $groups = group_memberships(false, $user_id);

    if (in_array(5, $groups))
    {
        $username_string = $username_string . '+';
    }

    $event['username_string'] = $username_string;
}
Kailey Truscott - Community Team
GitHub repositories (including un-released extensions)
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5188
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: how to check if user is in group

Post by MattF »

The fact of the matter is you're gonna need to write an extension using both PHP and template changes, if you want to add an image next to usernames in the side profile, if they belong to certain groups.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
david63
Registered User
Posts: 18110
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: how to check if user is in group

Post by david63 »

It may also be worth considering using the cache to store the resultant data
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: how to check if user is in group

Post by MuhClaren »

david63 wrote:It may also be worth considering using the cache to store the resultant data
Good idea. +1
User avatar
3Di
Former Team Member
Posts: 15900
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: how to check if user is in group

Post by 3Di »

VSE wrote:The fact of the matter is you're gonna need to write an extension using both PHP and template changes, if you want to add an image next to usernames in the side profile, if they belong to certain groups.
To avoid template changes you could use this, I believe..

Code: Select all

        $username_string = $username_string . '<img src="image_path/image_name" width="14" height="14" alt="" title="" />' ;
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
Post Reply

Return to “Extension Writers Discussion”