Using Symfony 3.4, I need to do persist user before logout, so I've implemented the following listener:
<?php
namespace c975L\UserBundle\Listeners;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
use c975L\UserBundle\Entity\User;
class LogoutListener implements LogoutHandlerInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function logout(Request $request, Response $response, TokenInterface $token)
{
$user = $token->getUser();
if ($user instanceof User) {
$user->setLatestSignout(new \DateTime());
$this->em->persist($user);
$this->em->flush();
}
}
}
I have autowired in services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: true
c975L\UserBundle\Listeners\:
resource: '../../Listeners/*'
but I receive the following compilation error
Compile Error: ContainerNxlf3ac\appDevDebugProjectContainer::load(): Failed opening required '/.../var/cache/dev/ContainerNxlf3ac/getLogoutlistener2Service.php' (include_path='.:/usr/share/php')
It looks like I have to alias the service but I can't manage to do so...
I have the same result when using parent::__construct();
Error doesn't appear if __construct()
is removed but then $this->em
is not defined...
[EDIT - Added firewall part]
security.yml
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|fonts|images|js)/
security: false
main:
pattern: ^/
provider: c975_l_userbundle
form_login:
login_path: user_signin
check_path: user_signin
default_target_path: user_dashboard
csrf_token_generator: security.csrf.token_manager
remember_me:
secret: '%secret%'
lifetime: 31536000
path: /
secure: true
anonymous: true
logout_on_user_change: true
logout:
path: user_signout
handlers: [c975L\UserBundle\Listeners\Logoutlistener]
The problem was in the firewall part. It was a mistake using a "l" (lowercase) in place of a "L" (uppercase) for LogoutListener
name. Setting the following worked.
handlers: [c975L\UserBundle\Listeners\LogoutListener]
[EDIT] There was a bug, that has been solved by https://github.com/symfony/symfony/pull/26355