1

I was doing a custom code for a custom page that I have created through my controller where I set a user field named MyChoice and I add a role "freeaccess" for the user who opens this page. However , I noticed that when I opened this page anonymously, the user whose ID is 0 which is supposed to be the anonymous user, has gained this new role and had his field set . in fact I obtained Anonymous and freeaccess when I printed the Roles array like so :

$uid = \Drupal::currentUser()->id();
     $user = \Drupal\user\Entity\User::load($uid);
     $roles = $user->getRoles();
print_r($roles);

After setting a condition to stop anonymous user from accessing this page , I tried to do this to undo what i previously caused by messing with drupal user ID 0 :

$uid = \Drupal::currentUser()->id(); //which is zero
 $user = \Drupal\user\Entity\User::load($uid);
 $user-> RemoveRole('freeaccess');
 $user ->save();

And I unset the field MyChoice.

Is there something else I should do (in the database for example) to reset the user whose ID is zero to his initial state ? Is the drupal user #0 just like any other user ? It has fields and roles ..?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.