eZ Community » Forums » eZ Publish 5 Platform » Import User with Ezpublish 5
expandshrink

Import User with Ezpublish 5

Import User with Ezpublish 5

Monday 20 January 2014 4:29:19 pm - 19 replies

Hi,

I need to develop a user ezpublish import functionality :

Daily a software makes me has a csv file in a FTP server. I need to get this file and recover the contents of the file and turn it into a list ezpublish user belonging to a specific group. Can you give me advice on how to start with ezpublish 5 to import this? 

Thank you in advance,

Monday 20 January 2014 7:56:34 pm

With eZ Publish 5 Public API this should be really straightforward. 

Check https://confluence.ez.no/display/EZP52/3.+Managing+Content#id-3.ManagingContent-Creatingcontent 

start with:

$contentType = $contentTypeService->loadContentTypeByIdentifier( 'user' );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' ); 

and follow instructions on that page

Modified on Monday 20 January 2014 8:00:44 pm by Ivo Lukač

Tuesday 21 January 2014 8:57:16 am

Thanks Ivo,

I'm starting now happy.gif Emoticon

Tuesday 21 January 2014 9:24:32 am

Hi,

I remember un ez4 it's possible to use a RemoteID. It's always possible ?

More, this is my algorithm, what do you think of this :

 

 
 
$arrayOfUsers = getAnArrayOfUsersFromMyCsv()
 
foreach ( $arrayOfUsers as $user) {
 
    if ( $userIsAlreadyIneZpublish($user) ) {
 
        updateUser($user) // Doc : https://github.com/ezsystems/CookbookBundle/blob/master/Command/UpdateContentCommand.php
 
    }else{
 
        createUser($user) // Doc : https://github.com/ezsystems/CookbookBundle/blob/master/Command/CreateContentCommand.php
 
    }
 
}

Modified on Tuesday 21 January 2014 9:27:45 am by Johann Roc

Tuesday 21 January 2014 9:41:54 am

There is a method for fetching content by remoteid:

 $contentService->loadContentInfoByRemoteId( "your_remote_id" )

Tuesday 21 January 2014 9:42:59 am

Awesome happy.gif Emoticon

Tuesday 21 January 2014 11:00:44 am

Hi,

I try to use method $contentService->loadContentInfoByRemoteId to check if a user is already created. But when a user it's not in the database the function return an exception. There is another method to test if i have an user created by its remote id (I see nothing in the class contentservice of API) ?

Tuesday 21 January 2014 11:07:33 am

Quote from Johann Roc :

Hi,

I try to use method $contentService->loadContentInfoByRemoteId to check if a user is already created. But when a user it's not in the database the function return an exception. There is another method to test if i have an user created by its remote id (I see nothing in the class contentservice of API) ?

Exactly!

Handle the exception happy.gif Emoticon

If it returns NotFoundException then go with createUser, otherwise the updateUser you got

Tuesday 21 January 2014 11:16:38 am

Waiting for your answer i try this :

$repository = $this -> getContainer() -> get('ezpublish.api.repository');
$searchService = $repository -> getSearchService();
$query = new \eZ\Publish\API\Repository\Values\Content\Query(); 
$query -> criterion = new \eZ\Publish\API\Repository\Values\Content\Query\Criterion\RemoteId($user['id']); 
$result = $searchService -> findContent($query); 
echo $result -> totalCount;

What do you think of this ? In fact, it's maybe in my head, but for me generate an exception seems to be not good :s ? (Exception is the evillll :d)

Tuesday 21 January 2014 11:21:16 am

Quote from Johann Roc :

Waiting for your answer i try this :

$repository = $this -> getContainer() -> get('ezpublish.api.repository');
$searchService = $repository -> getSearchService();
$query = new \eZ\Publish\API\Repository\Values\Content\Query(); 
$query -> criterion = new \eZ\Publish\API\Repository\Values\Content\Query\Criterion\RemoteId($user['id']); 
$result = $searchService -> findContent($query); 
echo $result -> totalCount;

What do you think of this ? In fact, it's maybe in my head, but for me generate an exception seems to be not good :s ? (Exception is the evillll :d)

Well, if this works for you then go for it happy.gif Emoticon

Wednesday 22 January 2014 5:34:19 pm

Hi,

I try to create my new user, but i have a problem with the user account attribute. I try this :

$contentType = $contentTypeService->loadContentTypeByIdentifier( 'user' );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'fre-FR' );
$contentCreateStruct->setField( 'nom', $user['nom'] );            
$contentCreateStruct->setField( 'prenom', $user['prenom'] );            
$myNewUser = new \eZ\Publish\Core\FieldType\User\Value($user['email']); 
$contentCreateStruct->setField( 'user_account', $myNewUser );
 

I think it's not the good way to do ?

Thanks

Wednesday 22 January 2014 5:37:15 pm

I have found happy.gif Emoticon

 $myNewUser = new \eZ\Publish\Core\FieldType\User\Value(array('email'=>$user['email']));

Thursday 23 January 2014 9:53:32 am

In fact, i have no error, but my attribute user_account is empty. I don't understand how to add this part. When in display the content of my created user is empty sad.gif Emoticon

 eZ\Publish\API\Repository\Values\Content\Field Object
(    [id:protected] => 1083                    
     [fieldDefIdentifier:protected] => user_account                    
     [value:protected] => eZ\Publish\Core\FieldType\User\Value Object                        
     (                            
                 [hasStoredLogin] =>                             
                 [contentId] =>                             
                 [login] =>                             
                 [email] =>                             
                 [passwordHash] =>                             
                 [passwordHashType] =>                             
                 [enabled] =>                             
                 [maxLogin] =>                         
     ) 
 
 [languageCode:protected] => fre-FR                
)

Modified on Thursday 23 January 2014 9:54:00 am by Johann Roc

Thursday 23 January 2014 10:01:20 am

Hi Johann,

Seems that creating user objects is a special case. Try the createUser method in UserService:

https://github.com/ezsystems/ezpu...Core/Repository/UserService.php#L378 

Thursday 23 January 2014 10:57:21 am

Thanks happy.gif Emoticon Here is the code to create a User with attributes :

$repository = $this->getContainer()->get( 'ezpublish.api.repository' ); 
$repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) );
$contentTypeService = $repository->getContentTypeService(); 
$userType = $contentTypeService->loadContentTypeByIdentifier( 'user_reseau' ); 
$userService = $repository->getUserService(); 
$userCreateStruct = $userService->newUserCreateStruct( $email, $email, "password", 'fre-FR', $userType ); 
$userCreateStruct->setField( 'name', $name );            
$userGroup = $userService->loadUserGroup('123'); 
$repository->getUserService()->createUser($userCreateStruct,array($userGroup));

Modified on Thursday 23 January 2014 10:58:00 am by Johann Roc

Thursday 23 January 2014 11:30:44 am

I want to set a RemoteId for objects that I created. it's possible ? 

Thursday 23 January 2014 11:41:57 am

Quote from Johann Roc :

I want to set a RemoteId for objects that I created. it's possible ? 

Should be easy. Did you try:

$userCreateStruct->setField('remoteid',$remoteid); 

Modified on Thursday 23 January 2014 11:51:40 am by Ivo Lukač

Thursday 23 January 2014 12:52:08 pm

I tried but i have the error object :

  [eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue]
  Argument 'login' is invalid: '''' is wrong value in class 'UserCreateStruct' 

I don't undestand, there is no link with the remote id :s

Modified on Thursday 23 January 2014 2:47:48 pm by Johann Roc

Thursday 23 January 2014 3:03:28 pm

Raaaaaaa

It's just :

 $userCreateStruct->remoteId = '9999d3c29e15006e45197295722c7ade';

I found the solution in tests included in Kernel https://github.com/ezsystems/ezpublish-kernel/blob/master/eZ/Publish/API/Repository/Tests/ContentServiceTest.php

Modified on Thursday 23 January 2014 3:03:55 pm by Johann Roc

Sunday 21 February 2016 9:26:42 pm

Further note for devs importing users and updating them as well: to update the remoteId, it is not enough to use a userUpdateStruct and a contentUpdateStruct, you will need as well to create a ContentMetadataUpdateStruct. This is a bit more verbose than the user creation. Also take a look at issue EZP-21797 which might give you troubles

Modified on Sunday 21 February 2016 11:44:23 pm by Gaetano Giunta

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from