Skip to content

Instantly share code, notes, and snippets.

@mauricios
Last active September 8, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricios/ad06d3cf1b5328aee046759f9807d52e to your computer and use it in GitHub Desktop.
Save mauricios/ad06d3cf1b5328aee046759f9807d52e to your computer and use it in GitHub Desktop.
eZ Platform Service to login user programatically
<?php
namespace Vendor\MyLoginBundle\Services;
use Symfony\Component\HttpFoundation\RequestStack,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\HttpFoundation\Cookie;
class LoginService
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function setLoggedInUser($userId)
{
$session = $this->requestStack->getCurrentRequest()->getSession();
$session->set('eZUserLoggedInID', $userId);
$response = new Response();
$response->headers->setCookie(new Cookie('is_logged_in', 'true'));
}
}
@mauricios
Copy link
Author

You should inject the requestStack in the service configuration:

services:
    myvendor.login.service:
        class: Vendor\MyLoginBundle\Services\LoginService
        arguments:
            - @request_stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment