This site has been archived. To learn more about our current products Ibexa Content, Ibexa Experience, Ibexa Commerce head over to the Ibexa Developer Portal

eZ Community » Forums » eZ Publish 5 Platform » How to log a user in a controller ?

How to log a user in a controller ?

How to log a user in a controller ?

Thursday 30 May 2013 4:13:18 pm - 13 replies

Hi all,

In my eZ 5, I need to create a user from an external DB. I do that in an my own controller, to replace /user/register.

 I read data, and create my user in eZ with a user service, and a the newUserCreateStruct().

To do that, I need to use a user with enough privileges :

 $user = $userService->loadUser( 14 );
 $repository->setCurrentUser( $user );

 My need is to create my new user, and to be logged in :

 $user = $userService->createUser( $userCreateStruct, array( $userGroup ) );
 $repository->setCurrentUser( $user );

 But I'm not logged in as administrator or as my new user in this case after the redirection.

I don't know what to do next (ajax request on /user/login, REST API...). Any idea ?


Modified on Thursday 30 May 2013 4:14:12 pm by Nicolas Lescure

Thursday 30 May 2013 4:53:32 pm


Is the new user created? Do you have something in the logs maybe?

Thursday 30 May 2013 5:52:10 pm

Yes, My user is created, with all data.

The error I have is an exception after the redirection saying "

User does not have access to 'read' 'content'


Thursday 30 May 2013 11:40:59 pm

You need to assign your user to a user group with the proper role to read the content.

Maybe your user group doesn't have the proper role ?

Modified on Friday 31 May 2013 12:07:29 am by Nguyen Huu Bruno

Friday 31 May 2013 9:03:07 am

I think roles are ok, since I can log in using /user/login.

My problem is "$repository->setCurrentUser( $user )" doesn't connect me.

Maybe using  the legacy function.

Friday 31 May 2013 9:55:22 am

I tryied to create and log in a new user too but I ran into the same problem.

The user is created but the setCurrentUser() function doesn't seem to work properly.

I can login using the default user/login form. And I have access to everything the role is giving access to.


in the back office, if I check the user object, I can't see the role nor the policies affected. Although, if I check the role object itself, I can see that the user IS using the role.

Maybe this is related, in some ways, to the issue.

Modified on Friday 31 May 2013 9:55:57 am by Julien Montavit

Friday 31 May 2013 5:59:02 pm

Hmm. Indeed, I can see that.

But on my created user, my left menu is the content tree, and not the user tree. I checked, and my user has the standard section, not the user one.

I have to check this too.

Nice clue Julien.

Friday 31 May 2013 10:01:50 pm

Quote from Julien Montavit :

The user is created but the setCurrentUser() function doesn't seem to work properly.

What do you mean by this?
setCurrentUser() is a repository function which does not persist in sessions, and the state is not shared with legacy kernel.

The eZ Symfony stack takes care of authentication and setting current user for both legacy(when loaded) and repository.

Sunday 02 June 2013 12:56:10 pm

Quote from André R :
Quote from Julien Montavit :

The user is created but the setCurrentUser() function doesn't seem to work properly.

What do you mean by this?
setCurrentUser() is a repository function which does not persist in sessions, and the state is not shared with legacy kernel.

Hi André,

It is our "problem". We are in the ez 5 stack and we would like to log a user in eZ Publish, both 5 and 4 stacks I guess.

I tried this in my controller, with no success :

 $userId = $user->id;
$myLegacyConnection = $this->getLegacyKernel()->runCallback(function () use ( $userId ) 
         $ez4User = eZUser::fetch( $userId ); 

The only way I see, is to force the user to log in by posting to /user/login (from ez4). And that is a big problem for user experience, because he has to register, then fill a form using his login and password.


Modified on Sunday 02 June 2013 12:58:19 pm by Nicolas Lescure

Monday 03 June 2013 9:36:05 am

Quote from André R :

setCurrentUser() is a repository function which does not persist in sessions, and the state is not shared with legacy kernel.

Ok. So if I understand right, the setCurrentUser() function is not what we should be using to log in the user.

Maybe there is a function to do what Nicolas explained in the previous post?

Thursday 06 June 2013 3:09:31 pm

I don't know what I'm doing wrong.

I tried to create a new module in the legacy stack. Here is the code:

$currentUser = eZUser::fetch( $userID );</span>
eZUser::updateLastVisit( $userID );
eZUser::setCurrentlyLoggedInUser( $currentUser, $userID );

And the trace:

Array(    [_ezpublish] => Array        (            [_security_ezpublish_front] => C:74:"Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken":3614:{a:3:{i:0;N;i:1;s:15:"ezpublish_front";i:2;s:3561:"a:4:{i:0;O:41:"eZ\Publish\Core\MVC\Symfony\Security\User":2:{s:47:"eZ\Publish\Core\MVC\Symfony\Security\Useruser";O:43:"eZ\Publish\Core\Repository\Values\User\User":7:{s:10:"*content";O:49:"eZ\Publish\Core\Repository\Values\Content\Content":3:{s:9:"*fields";a:5:{s:10:"first_name";a:1:{s:6:"fre-FR";O:40:"eZ\Publish\Core\FieldType\TextLine\Value":1:{s:4:"text";s:9:"Anonymous";}}s:9:"last_name";a:1:{s:6:"fre-FR";O:40:"eZ\Publish\Core\FieldType\TextLine\Value":1:{s:4:"text";s:4:"User";}}s:12:"user_account";a:1:{s:6:"fre-FR";O:36:"eZ\Publish\Core\FieldType\User\Value":8:{s:14:"hasStoredLogin";b:1;s:9:"contentId";i:10;s:5:"login";s:9:"anonymous";s:5:"email";s:12:"";s:12:"passwordHash";s:32:"4e6f6184135228ccd45f8233d72a0363";s:16:"passwordHashType";s:1:"2";s:7:"enabled";b:1;s:8:"maxLogin";i:1000;}}s:9:"signature";a:1:{s:6:"fre-FR";O:41:"eZ\Publish\Core\FieldType\TextBlock\Value":1:{s:4:"text";s:0:"";}}s:5:"image";a:1:{s:6:"fre-FR";N;}}s:14:"*versionInfo";O:53:"eZ\Publish\Core\Repository\Values\Content\VersionInfo":10:{s:8:"*names";a:1:{s:6:"fre-FR";s:14:"Anonymous User";}s:14:"*contentInfo";O:52:"eZ\Publish\API\Repository\Values\Content\ContentInfo":13:{s:5:"*id";i:10;s:16:"*contentTypeId";i:4;s:7:"*name";s:14:"Anonymous User";s:12:"*sectionId";i:2;s:19:"*currentVersionNo";i:2;s:12:"*published";b:1;s:10:"*ownerId";i:14;s:19:"*modificationDate";O:8:"DateTime":3:{s:4:"date";s:19:"2003-12-23 12:53:25";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}s:16:"*publishedDate";O:8:"DateTime":3:{s:4:"date";s:19:"2002-10-06 18:11:05";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}s:18:"*alwaysAvailable";i:1;s:11:"*remoteId";s:32:"faaeb9be3bd98ed09f606fc16d144eca";s:19:"*mainLanguageCode";s:6:"fre-FR";s:17:"*mainLocationId";s:2:"45";}s:5:"*id";i:474;s:12:"*versionNo";i:2;s:19:"*modificationDate";O:8:"DateTime":3:{s:4:"date";s:19:"2003-12-23 12:53:25";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}s:12:"*creatorId";i:14;s:15:"*creationDate";O:8:"DateTime":3:{s:4:"date";s:19:"2003-12-23 12:52:17";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}s:9:"*status";i:1;s:22:"*initialLanguageCode";s:6:"fre-FR";s:16:"*languageCodes";a:1:{i:0;s:6:"fre-FR";}}s:65:"eZ\Publish\Core\Repository\Values\Content\ContentinternalFields";a:5:{i:0;O:46:"eZ\Publish\API\Repository\Values\Content\Field":4:{s:5:"*id";i:19;s:21:"*fieldDefIdentifier";s:10:"first_name";s:8:"*value";r:7;s:15:"*languageCode";s:6:"fre-FR";}i:1;O:46:"eZ\Publish\API\Repository\Values\Content\Field":4:{s:5:"*id";i:20;s:21:"*fieldDefIdentifier";s:9:"last_name";s:8:"*value";r:10;s:15:"*languageCode";s:6:"fre-FR";}i:2;O:46:"eZ\Publish\API\Repository\Values\Content\Field":4:{s:5:"*id";i:21;s:21:"*fieldDefIdentifier";s:12:"user_account";s:8:"*value";r:13;s:15:"*languageCode";s:6:"fre-FR";}i:3;O:46:"eZ\Publish\API\Repository\Values\Content\Field":4:{s:5:"*id";i:177;s:21:"*fieldDefIdentifier";s:9:"signature";s:8:"*value";r:23;s:15:"*languageCode";s:6:"fre-FR";}i:4;O:46:"eZ\Publish\API\Repository\Values\Content\Field":4:{s:5:"*id";i:179;s:21:"*fieldDefIdentifier";s:5:"image";s:8:"*value";N;s:15:"*languageCode";s:6:"fre-FR";}}}s:8:"*login";s:9:"anonymous";s:8:"*email";s:12:"";s:15:"*passwordHash";s:32:"4e6f6184135228ccd45f8233d72a0363";s:16:"*hashAlgorithm";i:2;s:10:"*enabled";b:1;s:11:"*maxLogin";i:1000;}s:48:"eZ\Publish\Core\MVC\Symfony\Security\Userroles";a:0:{}}i:1;b:0;i:2;a:0:{}i:3;a:0:{}}";}}            [eZUserLoggedInID] => 14        )
    [_sf2_flashes] => Array        (        )
    [_sf2_meta] => Array        (            [u] => 1370523804            [c] => 1370523799            [l] => 0        )


This should work.We see eZUserLoggedInID = 14, but other data are still anonymous.

It is a bug I think. I'm in 2013.5. I reached a dead end sad.gif Emoticon

Modified on Thursday 06 June 2013 3:40:53 pm by Nicolas Pastorino

Friday 07 June 2013 6:02:11 am

Hi Nicolas,

Assuming that your User/Group/Roles are correctly defined, you can log any user in from a Symfony controller with this Quick&Dirty code:

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
// Ez User Id
$user_id = 14;
// Session
$session = $this->getRequest()->getSession();
$session->set('eZUserLoggedInID', $user_id);
// Cookie
$response = new Response();
$response->headers->setCookie(new Cookie('is_logged_in', 'true'));


Modified on Friday 07 June 2013 6:03:02 am by Romain Petit

Friday 07 June 2013 9:25:57 am

Thank you Romain!

You know, I have to try this, because my solution (fill the form with login / password...) is dirty too... 

Monday 29 July 2013 11:45:55 am

Yes the Romain code works perfectly blunk.gif Emoticon

I create a quick Gist with a abstract base controller which handle isLoggedIn and LoginUser methods.
Thanks !


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

36 542 Users on board!

Forums menu

Proudly Developed with from