You are here

function user_expire_find_users_to_expire_by_role in User Expire 8

Same name and namespace in other branches
  1. 7 user_expire.module \user_expire_find_users_to_expire_by_role()

Finds users to expire by role and expiration period.

Parameters

int $role_id: The role ID to search for.

int $seconds_since_login: Seconds since login. To find users *about* to expire, use a smaller number.

Return value

\Drupal\Core\Database\StatementInterface|null Returns an iterator for use in a loop.

2 calls to user_expire_find_users_to_expire_by_role()
user_expire_expire_by_role in ./user_expire.module
Expires user by roles according to rules in the database.
user_expire_expire_by_role_warning in ./user_expire.module
Warns users with an upcoming expiration by roles.

File

./user_expire.module, line 391
Main module file for User expire module.

Code

function user_expire_find_users_to_expire_by_role($role_id, $seconds_since_login) {

  // An inactivity period of zero means the rule is disabled for the role.
  if (empty($seconds_since_login)) {
    return NULL;
  }

  // Find all the of users that need to be expired.
  $query = \Drupal::database()
    ->select('users_field_data', 'u');
  $query
    ->fields('u', [
    'uid',
  ])
    ->condition('status', 1, '=')
    ->condition('u.uid', 0, '<>');

  // Conditional fragment for checking on access.
  $db_and_access = new Condition('AND');
  $db_and_access
    ->condition('u.access', \Drupal::time()
    ->getRequestTime() - $seconds_since_login, '<=')
    ->condition('u.access', 0, '>');

  // Conditional fragment for checking on created.
  $db_and_created = new Condition('AND');
  $db_and_created
    ->condition('u.created', \Drupal::time()
    ->getRequestTime() - $seconds_since_login, '<=')
    ->condition('u.access', 0, '=');

  // Now OR the access and created fragments together.
  $access_or_created = new Condition('OR');
  $access_or_created
    ->condition($db_and_access)
    ->condition($db_and_created);

  // And finally, AND them together with the status and uid checks.
  $query
    ->condition($access_or_created);

  // If this role is not the authenticated role, add a condition on the role.
  // The Authenticated "role" is not in this table as it affects all users.
  if (RoleInterface::AUTHENTICATED_ID != $role_id) {
    $query
      ->join('user__roles', 'ur', 'u.uid = ur.entity_id');
    $query
      ->condition('ur.roles_target_id', $role_id, '=');
  }
  return $query
    ->execute();
}