How to add user to group by code ?

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!
MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

How to add user to group by code ?

Post by MikaelNazarenko » 09 Jul 2019, 12:04

Ubuntu 18.04
PHP 7.2
"aimeos/aimeos-laravel": "^2019.04"
Laravel: 5.8

I create user by code:

Code: Select all

$user = new App\User([
                            'id_customer' => $id_customer,
                            'id_customer_ref' => $id_customer_ref,
                            'firstname' => $firstname,
                            'lastname' => $lastname,
                            'email' => $email
                        ]);
But how can I add user to some group? Like editor, to allow user access admin panel ?

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to add user to group by code ?

Post by MikaelNazarenko » 09 Jul 2019, 13:57

This is the way how do I do it:

Code: Select all

        $context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' );
        $context->setEditor( 'aimeos:account' );

        $localeManager = \Aimeos\MShop::create( $context, 'locale' );
        $localeItem = $localeManager->bootstrap( 'default', '', '', false );
        $context->setLocale( $localeItem );

        $groupManager = \Aimeos\MShop::create( $context, 'customer/group' );

        $customerListsManager = \Aimeos\MShop::create( $context, 'customer/lists' );

        $editorGroupId = $groupManager->findItem( 'editor' )->getId();

         $item = $customerListsManager->createItem();

         $item->setDomain( 'customer/group' );
         $item->setParentId( $user->id );
         $item->setRefId( $editorGroupId );
         $item->setType( 'default' );
         $item->setStatus( 1 );

        $customerListsManager->saveItem( $item, false );
Is it right way ?

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

Re: How to add user to group by code ?

Post by aimeos » 10 Jul 2019, 08:20

If you want to do it from the command line, then yes. From the web you should use another context:
https://aimeos.org/docs/Laravel/Extend_Aimeos
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply