User groups for Decorators?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

User groups for Decorators?

Post by kdim95 » 06 Apr 2023, 12:35

Laravel framework version: 9.52.5
Aimeos Laravel version: ~2022.10
PHP Version: 8.2.4
Environment: Linux

Hello,

Is it possible to have a decorator work only for a specific user group?

If not, what is the best way to check if a user is in the admin group inside a decorator?

Thanks

User avatar
aimeos
Administrator
Posts: 7908
Joined: 01 Jan 1970, 00:00

Re: User groups for Decorators?

Post by aimeos » 07 Apr 2023, 08:43

Where do you want to use the decorator? Frontend? Admin backend?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

Re: User groups for Decorators?

Post by kdim95 » 07 Apr 2023, 08:53

Hello, I wanted to use the decorator in the backend.
I think I already found the solution.

Solution if you want to use the group ID:

Code: Select all

$context = $this->context();

$customer = \Aimeos\MShop::create( $context, 'customer' )->get( $context->user() );

$groups = $customer->getGroups();

$isAdmin = in_array( 1, $groups );
$isEditor = in_array( 2, $groups );

dump( 'isAdmin', $isAdmin );
dump( 'isEditor', $isEditor );
dump( 'isSuper', $customer->isSuper() );
Solution if you want to use the group code:

Code: Select all

$context = $this->context();

$customer = \Aimeos\MShop::create( $context, 'customer' )->get( $context->user() );

$groups = $customer->getRefItems( 'customer/group' );

$isAdmin = (bool) $groups->find( function( $value, $key ) {
	return $value->getCode() === 'admin';
});

$isEditor = (bool) $groups->find( function( $value, $key ) {
	return $value->getCode() === 'editor';
});

dump( 'isAdmin', $isAdmin );
dump( 'isEditor', $isEditor );
dump( 'isSuper', $customer->isSuper() );
But I don't need this code anymore, because I've created the decorator in a different way.
It used to mess with order modifications in the frontend, but I think it's fixed now.
The decorator is in a different location now -> Aimeos\Admin\JQAdm\Common\Decorator.
Used to be in Aimeos\MShop\Common\Manager\Decorator.

User avatar
aimeos
Administrator
Posts: 7908
Joined: 01 Jan 1970, 00:00

Re: User groups for Decorators?

Post by aimeos » 07 Apr 2023, 09:15

Don't mess with group IDs and use the access() view helper instead:
https://aimeos.org/docs/latest/infrastr ... ew/#access
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

Re: User groups for Decorators?

Post by kdim95 » 07 Apr 2023, 09:37

aimeos wrote: 07 Apr 2023, 09:15 Don't mess with group IDs and use the access() view helper instead:
https://aimeos.org/docs/latest/infrastr ... ew/#access
I tried this, but in the following case the user is in the admin group, but not in the editor group.

Code: Select all

$view = $this->view();
dump( $view->access( ['editor'] ) );
Whatever I put in the array, be it 'editor', 'admin', it always returns true.

This is done in a decorator Aimeos\Admin\JQAdm\Common\Decorator\<My Decorator>::save()

User avatar
aimeos
Administrator
Posts: 7908
Joined: 01 Jan 1970, 00:00

Re: User groups for Decorators?

Post by aimeos » 11 Apr 2023, 07:07

kdim95 wrote: 07 Apr 2023, 09:37 I tried this, but in the following case the user is in the admin group, but not in the editor group.

Code: Select all

$view = $this->view();
dump( $view->access( ['editor'] ) );
Whatever I put in the array, be it 'editor', 'admin', it always returns true.

This is done in a decorator Aimeos\Admin\JQAdm\Common\Decorator\<My Decorator>::save()
It's OK to do that in a decorator or a template - that doesn't matter.
If it returns true in both cases, then the user must be in both groups because the code for checking is very easy:
https://github.com/aimeos/aimeos-base/b ... rd.php#L52
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply