Best way to check if current user is staff?

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Best way to check if current user is staff?

Post by Toxyy » Sat Nov 24, 2018 2:57 pm

This has been asked before and I gave one of you guys a solution that I reworked that I found from here not long ago. What's the most efficient way to make this function? I don't want to use unnecessary resources since I use it so much. I figured checking the mod list first is probably the best, though I also considered adding an optional parameter for forum_id. Not sure if I care enough to do that.

Code: Select all

public function is_staff()
{
        $user_id = $this->user->data['user_id'];

        if(!empty($this->auth->acl_get_list($user_id, 'm_')))
                return true;

        if(!empty($this->auth->acl_get_list($user_id, 'a_')))
                return true;

        return false;
}
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25183
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Best way to check if current user is staff?

Post by Paul » Sat Nov 24, 2018 3:12 pm

For the current logged in user you should use $auth->acl_get();
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Sat Nov 24, 2018 3:30 pm

Forum perms are "sowieso" moderator permission, then:

Code: Select all

	public function is_staff()
	{
		/* Grabs all admins and mods, it is a catch all. */
		$admin_ary = $this->auth->acl_get_list(false, 'a_', false);
		$admin_ary = (!empty($admin_ary[0]['a_'])) ? $admin_ary[0]['a_'] : [];
		$mod_ary = $this->auth->acl_get_list(false, 'm_', false);
		$mod_ary = (!empty($mod_ary[0]['m_'])) ? $mod_ary[0]['m_'] : [];
		/* Groups the above results */
		return array_unique(array_merge($admin_ary, $mod_ary));
	}
Edit: this function is meant to be used in other circumstancies, see below.
Last edited by 3Di on Sat Nov 24, 2018 4:04 pm, edited 2 times in total.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Best way to check if current user is staff?

Post by GanstaZ » Sat Nov 24, 2018 3:36 pm

For current user maybe something like this:

Code: Select all

	function is_staff()
	{
		return $this->auth->acl_gets('a_', 'm_'));
	}
Yeah.. you are right. I'm too sleepy.. just forgot about it for a moment).
Last edited by GanstaZ on Sat Nov 24, 2018 4:08 pm, edited 2 times in total.
"When answer lies in the question,.. question becomes redundant!"

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Sat Nov 24, 2018 3:50 pm

In that case, simply:

Code: Select all

public function is_staff($user_id)
{
	return ( $this->auth->acl_get($user_id, 'm_') || $this->auth->acl_get($user_id, 'a_') )
}
$auth always returns the bool.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Best way to check if current user is staff?

Post by Toxyy » Sat Nov 24, 2018 5:07 pm

3Di wrote:
Sat Nov 24, 2018 3:30 pm
"sowieso"
I believe the right translation here would be "inherent", as you mean that it automatically checks for the current forum's perms based on where you call it?
3Di wrote:
Sat Nov 24, 2018 3:30 pm

Code: Select all

$admin_ary = (!empty($admin_ary[0]['a_'])) ? $admin_ary[0]['a_'] : [];
...
$mod_ary = (!empty($mod_ary[0]['m_'])) ? $mod_ary[0]['m_'] : [];
Edit: this function is meant to be used in other circumstancies, see below.
Wouldn't this check only the first member of the admin/mod array though, ignoring all of the others in the list? Also which circumstance should that one be used?

@GangstaZ I'll benchmark yours, mine, and 3Di's in a bit to see which is faster, I'm guessing either yours or 3Di's are the same or one is sliiiiightly better.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Sat Nov 24, 2018 5:10 pm

3Di wrote:
Sat Nov 24, 2018 3:50 pm
In that case, simply:

Code: Select all

public function is_staff($user_id)
{
	return ( $this->auth->acl_get($user_id, 'm_') || $this->auth->acl_get($user_id, 'a_') )
}
$auth always returns the bool.
The way to go IMHO.

On a side note: "sowieso" correctly applies, I meant Forum perms are "in any case" moderator permissions. AFAIK.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Sat Nov 24, 2018 5:46 pm

Toxyy wrote:
Sat Nov 24, 2018 5:07 pm
3Di wrote:
Sat Nov 24, 2018 3:30 pm

Code: Select all

$admin_ary = (!empty($admin_ary[0]['a_'])) ? $admin_ary[0]['a_'] : [];
...
$mod_ary = (!empty($mod_ary[0]['m_'])) ? $mod_ary[0]['m_'] : [];
Edit: this function is meant to be used in other circumstancies, see below.
Wouldn't this check only the first member of the admin/mod array though, ignoring all of the others in the list? Also which circumstance should that one be used?
No. This function grabs a "list" of all of the users who have Admin or Moderator permission(s) over the whole board and returns the array. To be used in a "sql_in_set" for example, to exclude/include those users. That's not your use case here as far as I understood.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Best way to check if current user is staff?

Post by Toxyy » Sat Nov 24, 2018 6:13 pm

3Di wrote:
Sat Nov 24, 2018 5:46 pm
No. This function grabs a "list" of all of the users who have Admin or Moderator permission(s) over the whole board and returns the array. To be used in a "sql_in_set" for example, to exclude/include those users. That's not your use case here as far as I understood.
There is a 6 hour bumping rule...

just kidding Image Thanks again 3Di.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Sat Nov 24, 2018 6:19 pm

The thanks go to church, we say in Italy. :P
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Best way to check if current user is staff?

Post by Toxyy » Mon Jan 28, 2019 10:10 am

3Di wrote:
Sat Nov 24, 2018 3:50 pm

Code: Select all

public function is_staff($user_id)
{
	return ( $this->auth->acl_get($user_id, 'm_') || $this->auth->acl_get($user_id, 'a_') )
}
This would only work with acl_get_list, the way you've written it.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Mon Jan 28, 2019 1:29 pm

That's for the current logged in user.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Best way to check if current user is staff?

Post by Toxyy » Mon Jan 28, 2019 6:24 pm

3Di wrote:
Mon Jan 28, 2019 1:29 pm
That's for the current logged in user.
I meant that acl get has the options for $opt and forum id, not user id and opt. Written that way it would only work with acl_get_list
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

User avatar
3Di
Former Team Member
Posts: 13664
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Best way to check if current user is staff?

Post by 3Di » Mon Jan 28, 2019 8:42 pm

Ah I see, correct.

I am wondering if
$auth->acl_raw_data_single_user($user_id)
or
$auth->acl_raw_data($user_id = false, $opts = false, $forum_id = false)
can be what you are looking for, used in get_mask() and ghost_permissions(), as aside note.
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
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
Toxyy
Registered User
Posts: 689
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Best way to check if current user is staff?

Post by Toxyy » Tue Jan 29, 2019 2:00 am

Those seem to run a lot more queries than acl_gets or acl_gets, though.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

Post Reply

Return to “Extension Writers Discussion”