Role Swap

Public

Get raw version
php
  1. //you will need to enter the uid of the user here, I am just using user 1
  2. $user = user_load(1);
  3.  
  4. //first we check if the user has the first role in question
  5. if (isset($user->roles[8])) {
  6.  
  7. //if so, we unset the role
  8. unset($user->roles[8]);
  9.  
  10. //now we check to see if they have the role we want to swap for
  11. if (!isset($user->roles[7])) {
  12.  
  13. //if not we add this role
  14. $user->roles[7] = 'role name seven';
  15. }
  16.  
  17. $edit = array('roles' => $user->roles);
  18. user_save($user, $edit);
  19. }