How to access user groups from a laravel controller?

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!
shainanand
Posts: 26
Joined: 16 Oct 2023, 15:05

How to access user groups from a laravel controller?

Post by shainanand » 16 Oct 2023, 15:07

I am trying to get user groups from Laravel Controller. Is there any sample code that I can refer to?

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

Re: How to access user groups from a laravel controller?

Post by aimeos » 18 Oct 2023, 07:10

What do you want them for?

For access control use the group codes like this (https://laravel.com/docs/10.x/authoriza ... er-helpers):

Code: Select all

$this->authorize( 'admin', [get_class($this), ['admin', 'editor']] );
That works because of these lines: https://github.com/aimeos/aimeos-larave ... entication

The group IDs are available in the context:

Code: Select all

$groupIds = app( 'aimeos.context' )->get()->groups()
You can get the user group items like this:

Code: Select all

$user = Aimeos\Shop\Facades\Customer::uses( ['customer/group'] )->get()
$groups = $user->getRefItems( 'customer/group' );
Alternatively using the customer frontend controller:

Code: Select all

$user = \Aimeos\Controller\Frontend::create( app( 'aimeos.context' )->get(), 'customer' )
	->uses( ['customer/group'] )->get()
$groups = $user->getRefItems( 'customer/group' );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

shainanand
Posts: 26
Joined: 16 Oct 2023, 15:05

Re: How to access user groups from a laravel controller?

Post by shainanand » 21 Oct 2023, 15:24

Thank you

shainanand
Posts: 26
Joined: 16 Oct 2023, 15:05

Re: How to access user groups from a laravel controller?

Post by shainanand » 17 Nov 2023, 12:04

Tried the code today and groups was returning empty. This is the printed $user variable. Any missing configuration?
Attachments
Image 26.png
Image 26.png (22.67 KiB) Viewed 20198 times

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

Re: How to access user groups from a laravel controller?

Post by aimeos » 20 Nov 2023, 16:59

Which of the alternatives did you use?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

shainanand
Posts: 26
Joined: 16 Oct 2023, 15:05

Re: How to access user groups from a laravel controller?

Post by shainanand » 21 Nov 2023, 10:49

May I know what is meant by alternatives ?

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

Re: How to access user groups from a laravel controller?

Post by aimeos » 22 Nov 2023, 12:51

Which of those lines did you use?
aimeos wrote: 18 Oct 2023, 07:10 The group IDs are available in the context:

Code: Select all

$groupIds = app( 'aimeos.context' )->get()->groups()
You can get the user group items like this:

Code: Select all

$user = Aimeos\Shop\Facades\Customer::uses( ['customer/group'] )->get()
$groups = $user->getRefItems( 'customer/group' );
Alternatively using the customer frontend controller:

Code: Select all

$user = \Aimeos\Controller\Frontend::create( app( 'aimeos.context' )->get(), 'customer' )
	->uses( ['customer/group'] )->get()
$groups = $user->getRefItems( 'customer/group' );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

shainanand
Posts: 26
Joined: 16 Oct 2023, 15:05

Re: How to access user groups from a laravel controller?

Post by shainanand » 22 Nov 2023, 14:07

I used this one

Code: Select all

$user = \Aimeos\Controller\Frontend::create( app( 'aimeos.context' )->get(), 'customer' )
	->uses( ['customer/group'] )->get()
$groups = $user->getRefItems( 'customer/group' );

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

Re: How to access user groups from a laravel controller?

Post by aimeos » 24 Nov 2023, 07:09

Remember that you must be logged in to retrieve the user and his groups.
According to your screenshot, this isn't the case.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply