You are here

public function LdapTestFunctions::removeRoleFromUser in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_test/LdapTestFunctions.class.php \LdapTestFunctions::removeRoleFromUser()

From http://www.midwesternmac.com/blogs/jeff-geerling/programmatically-adding....

File

ldap_test/LdapTestFunctions.class.php, line 161

Class

LdapTestFunctions

Code

public function removeRoleFromUser($user, $role_name) {
  if (is_numeric($user)) {
    $user = user_load($user);
  }
  $key = array_search($role_name, $user->roles);
  if ($key == TRUE) {

    // Get the rid from the roles table.
    $roles = user_roles(TRUE);
    $rid = array_search($role_name, $roles);
    if ($rid != FALSE) {

      // Make a copy of the roles array, without the deleted one.
      $new_roles = [];
      foreach ($user->roles as $id => $name) {
        if ($id != $rid) {
          $new_roles[$id] = $name;
        }
      }
      user_save($user, [
        'roles' => $new_roles,
      ]);
    }
  }
}