You are here

public function FacebookTrackingPixelTestHelper::remove_role_from_user in Facebook Tracking Pixel 7

Same name and namespace in other branches
  1. 8 tests/facebook_tracking_pixel.helper.php \facebookTrackingPixel\FacebookTrackingPixelTestHelper::remove_role_from_user()

Remove a role from a user.

Parameters

$user: User object or user ID.

$role_name: String value of role to be removed.

File

tests/facebook_tracking_pixel.helper.php, line 44
Contains tests for the Facebook Tracking Pixel module.

Class

FacebookTrackingPixelTestHelper

Namespace

facebookTrackingPixel

Code

public function remove_role_from_user($user, $role_name) {

  // For convenience, we'll allow user ids as well as full user objects.
  if (is_numeric($user)) {
    $user = user_load($user);
  }

  // Only remove the role if the user already has it.
  $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,
      ]);
    }
  }
}