I need to disable the "administrator" role from user accounts who log into my website from outside my network. For security, I only want users who have this role to be able to use it when they are on my network (determined by some custom PHP code I have).
I know I can remove a role from a user permanently using the appropriate entity methods like this:
<?php use DrupaluserEntityUser; $user = User::load(Drupal::currentUser()->id()); $user->removeRole('administrator'); $user->save();
But what about to do so temporarily, without updating the user permanently?
Looking at changes made to a user’s roles in a custom module using hook_user_login()
are permanent. In Drupal 7, I was able to achieve this by modifying the global $user->roles
in various hooks.
I am also open to other ways Drupal allows me to meet my needs.