eZ Community » Forums » eZ Publish 5 Platform » Dynamically log user
expandshrink

Dynamically log user

Dynamically log user

Thursday 23 July 2015 7:26:10 pm - 2 replies

Hi there,

Here is what i want to do :

When a user is coming to my website he has to be connected directly with an eZUser. The user credentials are fetched from the database in a symfony (onKernelRequest) listener, once they have been fetched, i want to connect the user automatically.

Problem :

 eZUser::setCurrentlyLoggedInUser($myEzUser, $myEzUserId);

is rendering this error Uncaught PHP Exception ErrorException: "Notice: A session had already been started - ignoring session_start() in /vagrant/application/ezpublish_legacy/lib/ezsession/classes/ezpsessionhandler.php line 196".

More info :

- I am aware of this topic : http://share.ez.no/forums/ez-publish-5-platform/symfony-authentication-provider-ez-user-provider

- I've tried what they said but nothing worked.

Is there a problem by mixing the eZ 5 API to get user credentials with Legacy stack to set the current user ?

eZ Version : 5.4.

Thanks Cheers.

PS: If you need the full code of the listener i can provide it.

Modified on Thursday 23 July 2015 7:27:21 pm by Task Mikaël

Wednesday 06 April 2016 2:44:30 pm

You can authenticate a user programmatically this way (eZ 5.4) :

// use eZ\Publish\Core\MVC\Symfony\Security\User as SecurityUser;
// use eZ\Publish\API\Repository\Values\User\User;
// use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
 
    /**
     * Authenticate the user.
     * @param User $APIUser
     */
    public function login(User $APIUser)
    {
        $roles = array('ROLE_USER');
        $user = new SecurityUser($APIUser, $roles);
        $user->setAPIUser($APIUser);
        $token = new UsernamePasswordToken($user, null, 'ezpublish_front', $roles);
        $this->container->get('security.context')->setToken($token);
        $this->repository->setCurrentUser($APIUser);
    }

Monday 12 February 2018 4:15:03 pm

The API has changed (eZ5.4). The token is not handled by the same service :

<?php
// use eZ\Publish\Core\MVC\Symfony\Security\User as SecurityUser;
// use eZ\Publish\API\Repository\Values\User\User;
// use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
 
/**
 * Authenticate the user.
 *
 * @param User $APIUser
 */
public function login(User $APIUser)
{
    $roles = array('ROLE_USER');
    $user = new SecurityUser($APIUser, $roles);
    $user->setAPIUser($APIUser);
    $token = new UsernamePasswordToken($user, null, 'ezpublish_front', $roles);
    $this->container->get('security.token_storage')->setToken($token);
    $this->repository->setCurrentUser($APIUser);
}

@security.context -> @security.token_storage

Modified on Monday 12 February 2018 4:17:06 pm by Valentin Nivuahc

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from