You are here

function purge_users_blocked_user_ids in Auto Purge Users 8

Same name and namespace in other branches
  1. 8.3 purge_users.module \purge_users_blocked_user_ids()
  2. 8.2 purge_users.module \purge_users_blocked_user_ids()
  3. 7.2 purge_users.module \purge_users_blocked_user_ids()
  4. 7 purge_users.module \purge_users_blocked_user_ids()

Get blocked user ids.

1 call to purge_users_blocked_user_ids()
purge_users_get_user_ids in ./purge_users.module
Get user ids to purge.

File

./purge_users.module, line 136
Purge users module file.

Code

function purge_users_blocked_user_ids($param, $user_roles = array()) {

  // Get a time stamp according to the period selected.
  $blockdate = '';
  if (!empty($param['blocked_value'])) {
    $blockdate = strtotime('-' . $param['blocked_value'] . ' ' . $param['blocked_period']);
  }

  // Query through db table to get users ids.
  $db = \Drupal::database();
  $query = $db
    ->select('users_field_data', 'u')
    ->distinct();
  $query
    ->leftjoin('user__roles', 'ur', 'ur.entity_id = u.uid');
  $query
    ->fields('u', array(
    'uid',
  ));
  $query
    ->condition('u.uid', 1, '>');
  if (!empty($param['blocked_period'])) {
    $query
      ->condition('u.created', $blockdate, '<');
    $query
      ->condition('u.login', $blockdate, '<');
    $query
      ->condition('u.login', 0, '!=');
    $query
      ->condition('u.status', 0);
  }
  if (in_array(AccountInterface::AUTHENTICATED_ROLE, $user_roles)) {
    $db_or = new Condition('OR');
    $db_or
      ->isNull('ur.roles_target_id');
    $db_or
      ->condition('ur.roles_target_id', $user_roles, 'IN');
    $query
      ->condition($db_or);
  }
  else {
    $query
      ->condition('ur.roles_target_id', $user_roles, 'IN');
  }
  $uids = $query
    ->execute()
    ->fetchCol();
  return $uids;
}