Best way to check if current user is staff?

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

Best way to check if current user is staff?

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28651
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 »

For the current logged in user you should use $auth->acl_get();
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

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.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

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

Post by GanstaZ »

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.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

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.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 949
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

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

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

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.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

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.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 949
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

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

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

The thanks go to church, we say in Italy. :P
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 949
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

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

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

That's for the current logged in user.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 949
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

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

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

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

Post by 3Di »

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.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 949
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

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

Post by Toxyy »

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!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
Post Reply

Return to “Extension Writers Discussion”