How to change current custom attributes and keep them throughout your session
I validate my users with IMAG / Ldap-Bundle (BorisMorel / LdapBundle) and I can't save any changes made to mine User
to my LDAP server. But whenever a user logs in, I need to check some things locally and add user roles according to some rules.
Whenever imag checks the user / password on my LDAP server, it is called by the service I created listening to the "bind" event. Here's how it's done:
ldap_user_verifier:
class: MyBundle\Service\LdapUserVerifierService
arguments: [ @security.context, @doctrine.orm.default_entity_manager, @twig ]
tags:
- { name: 'kernel.event_listener', event: imag_ldap.security.authentication.pre_bind, method: loadUser }
Note: bind_username_before
set to false
, which means it LdapUserVerifierService::loadUser
is only called after the user is logged in and the class is User
loaded on mine security.context
.
Then I have mine LdapUserVerifierService::loadUser
as follows:
public function loadUser(LdapUserEvent $event) {
$ldapUser = $event->getUser();
[...]
$ldapUser->setLocalUser($localUser);
$ldapUser->addRole('ROLE_USER');
[...]
}
This works great, except that when my user changes the page (from /login_check
to /dashboard
, for example) my changes security.context.getToken().getUser()
are lost. My user has localUser
and ROLE_USER
associated with /login_check
, but not on /dashboard
.
Since my user is not loaded from my application database, I cannot save it to the database that will be loaded in subsequent pages, and therefore cannot save my object changes without saving it to another session variable.
Does anyone know I have to deal with this?
Thank.
source to share