title: Symfony - user gets logged out even on a name change
body: <p>I am facing a really weird situation a user with <code>ROLE_ADMIN</code> gets logged out as soon as I change the name and even if i dont change anything and press save button it gets logged out. if i change the user's role directly in database to <code>ROLE_USER</code> the same code works fine and user is not logged out.</p>
  
  <p>Following is the controller that takes care of the profile update</p>
  
  <pre><code>    /**
       * @Route("/profile", name="profile")
       */
      public function profileAction(Request $request)
      {
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $userInfo = $this-&gt;getUser();
  
          //create the form object
          $profileForm = $this-&gt;createForm(UserType::class, $userInfo);
          $profileForm-&gt;handleRequest($request);
  
  
          //check data validity
          if($profileForm-&gt;isValid()){
              $em-&gt;persist($userInfo);
              $em-&gt;flush();
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add(
                  'success',
                  'Your profile information has been updated'
              );
  
              return $this-&gt;render('AppBundle:admin/user:admin-edit.html.twig',array(
                  'edit_form' =&gt; $profileForm-&gt;createView()
              ));
          }
  
  
          // render registration form
          return $this-&gt;render('AppBundle:admin/user:admin-edit.html.twig',array(
              'edit_form' =&gt; $profileForm-&gt;createView()
          ));
      }
  
  }
  </code></pre>
  
  <p>This is my <code>security.yml</code></p>
  
  <pre><code>security:
      encoders:
          # Our user class and the algorithm we'll use to encode passwords
          # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
          AppBundle\Entity\User: bcrypt
  
      providers:
          # Simple example of loading users via Doctrine
          # To load users from somewhere else: http://symfony.com/doc/current/cookbook/security/custom_provider.html
          database_users:
              entity: { class: AppBundle:User, property: username }
  
      firewalls:
          # disables authentication for assets and the profiler, adapt it according to your needs
          dev:
              pattern: ^/(_(profiler|wdt)|css|images|js)/
              security: false
  
          main:
              http_basic: ~
              anonymous: ~
              logout: ~
              guard:
                  authenticators:
                      - app.form_login_authenticator
                      - app.facebook_authenticator
                  # by default, use the start() function from FormLoginAuthenticator
                  entry_point: app.form_login_authenticator
      access_control:
          - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/admin/, roles: ROLE_ADMIN }
          - { path: ^/user, roles: ROLE_USER }
  </code></pre>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>This is my <code>UserType</code></p>
  
  <pre><code>namespace AppBundle\Form;
  
  use AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber;
  use AppBundle\Form\EventListener\AddProfileFieldSubscriber;
  use Doctrine\ORM\EntityManager;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  
  class UserType extends AbstractType
  {
  
      private $addDepartmentDegreeCourseFieldSubscriber;
      private $addProfileFieldSubscriver;
  
      function __construct(AddDepartmentDegreeCourseFieldSubscriber $subscriber, AddProfileFieldSubscriber $fields)
      {
          $this-&gt;addDepartmentDegreeCourseFieldSubscriber = $subscriber;
          $this-&gt;addProfileFieldSubscriver = $fields;
      }
  
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;addEventSubscriber($this-&gt;addProfileFieldSubscriver);
          $builder-&gt;addEventSubscriber($this-&gt;addDepartmentDegreeCourseFieldSubscriber);
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\User'
          ));
      }
  }
  </code></pre>
  
  <p>This is my <code>AddProfileFieldSubscriber</code></p>
  
  <pre><code>namespace AppBundle\Form\EventListener;
  
  
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\Extension\Core\Type\EmailType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  use Symfony\Component\Validator\Constraints\NotBlank;
  
  
  class AddProfileFieldSubscriber implements EventSubscriberInterface
  {
      protected $authorizationChecker;
  
  
      function __construct(AuthorizationChecker $authorizationChecker)
      {
          $this-&gt;authorizationChecker = $authorizationChecker;
      }
  
      public static function getSubscribedEvents()
      {
          // Tells the dispatcher that you want to listen on the form.pre_set_data
          // event and that the preSetData method should be called.
          return array(FormEvents::PRE_SET_DATA =&gt; 'preSetData');
      }
  
      public function preSetData(FormEvent $event)
      {
  
  
          $user = $event-&gt;getData();
          $form = $event-&gt;getForm();
  
  
  
  
          if($user){
              $form-&gt;add('firstName', TextType::class);
              $form-&gt;add('lastName', TextType::class);
              $form-&gt;add('password', PasswordType::class, array(
                  'mapped' =&gt; false
              ));
              $form-&gt;add('profileImage', FileType::class, array(
                  'data_class' =&gt; null
              ));
              if (in_array("ROLE_USER", $user-&gt;getRoles())) {
                  $form-&gt;add('contactNumber', TextType::class);
  
                  $form-&gt;add('gender', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'Male' =&gt; 'm',
                          'Female' =&gt; 'f'
                      ),
                      'placeholder' =&gt; 'provide_gender'
                  ));
                  $form-&gt;add('college', EntityType::class, array(
                          'placeholder' =&gt; 'provide_college',
                          'class' =&gt; 'AppBundle\Entity\College')
                  );
                  $form-&gt;add('interest', EntityType::class, array(
                          'class' =&gt; 'AppBundle\Entity\Interest',
                          'multiple' =&gt; true,
                          'expanded' =&gt; false,
                          'by_reference' =&gt; false,
                      )
                  );
  
              }
  
              if($this-&gt;authorizationChecker-&gt;isGranted('ROLE_ADMIN')  ) {
  
                  $form-&gt;add('isActive', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'account_active' =&gt; '1',
                          'account_inactive' =&gt; '0'
                      ),
                      'placeholder' =&gt; 'provide_status'
                  ));
              }
  
          }
          //if the selected user has role_user only then display the following fields in edit profile view
         else {
              $form-&gt;add('username', EmailType::class);
              $form-&gt;add('password', PasswordType::class, array(
                  'constraints' =&gt; array(new NotBlank(array(
                          'message' =&gt; 'user.password.not_blank'
                      )
                  ),),
              ));
          }
      }
  }
  </code></pre>
  
  <p>This is <code>AddDepartmentDegreeCourseFieldSubscriber</code> </p>
  
  <pre><code>namespace AppBundle\Form\EventListener;
  
  
  use AppBundle\Entity\Degree;
  use AppBundle\Entity\Department;
  use Doctrine\ORM\EntityManager;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use Symfony\Component\Form\FormInterface;
  
  class AddDepartmentDegreeCourseFieldSubscriber implements EventSubscriberInterface
  {
  
      protected $em;
  
  
      function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      }
  
      public static function getSubscribedEvents()
      {
          // Tells the dispatcher that you want to listen on the form.pre_set_data
          // event and that the preSetData method should be called.
          return array(
              FormEvents::PRE_SET_DATA =&gt; 'onPreSetData',
              FormEvents::PRE_SUBMIT =&gt; 'onPreSubmit'
          );
      }
      protected function addElements(FormInterface $form, Department $departments = null, Degree $degree = null)
      {
  
          // Add the department element
          $form-&gt;add('department', EntityType::class, array(
                  'data' =&gt; $departments,
                  'placeholder' =&gt; 'provide_department',
                  'class' =&gt; 'AppBundle\Entity\Department')
          );
          // Degree are empty, unless we actually supplied a department
          $degree = array();
          if ($departments) {
              // Fetch the courses from specified degree
              $repo = $this-&gt;em-&gt;getRepository('AppBundle:Degree');
              $degree = $repo-&gt;findByDepartment($departments, array('name' =&gt; 'asc'));
          }
  
  
          // Add the province element
          $form-&gt;add('degree', EntityType::class, array(
                  'placeholder' =&gt; 'provide_degree',
                  'class' =&gt; 'AppBundle\Entity\Degree',
                  'choices' =&gt; $degree)
          );
          // Cities are empty, unless we actually supplied a province
          $courses = array();
          if ($degree) {
              // Fetch the cities from specified province
              $repo = $this-&gt;em-&gt;getRepository('AppBundle:Course');
              $courses = $repo-&gt;findByDegree($degree, array('name' =&gt; 'asc'));
          }
          // Add the Course element
          $form-&gt;add('course', EntityType::class, array(
              'class' =&gt; 'AppBundle\Entity\Course',
              'choices' =&gt; $courses,
          ));
  
      }
      function onPreSubmit(FormEvent $event) {
  
          $form = $event-&gt;getForm();
          $data = $event-&gt;getData();
          if (isset($data['degree'])) {
  
              // Note that the data is not yet hydrated into the entity.
              $degree = $this-&gt;em-&gt;getRepository('AppBundle:Degree')-&gt;find($data['degree']);
              $department = $this-&gt;em-&gt;getRepository('AppBundle:Department')-&gt;find($data['department']);
              $this-&gt;addElements($form, $department, $degree);
          }
      }
      function onPreSetData(FormEvent $event) {
          //echo "before submit";die;
          $user = $event-&gt;getData();
          $form = $event-&gt;getForm();
  
  
  
          if($user){
              //if the selected user has role_user only then display the following fields in edit profile view
              if (in_array("ROLE_USER", $user-&gt;getRoles())) {
                  $degree = ( !empty($user) &amp;&amp; !empty($user-&gt;getCourse())) ? $user-&gt;getCourse()-&gt;getDegree() : null;
                  $departments = ( !empty($user) &amp;&amp; !empty($user-&gt;getCourse())) ? $user-&gt;getCourse()-&gt;getDegree()-&gt;getDepartment() : null;
                  $this-&gt;addElements($form, $departments, $degree);
              }
  
          }
  
      }
  }
  </code></pre>
  
  <p>At this point I am really clueless what could be causing this, I will really appreciate any help here...</p>