You are here

function user_expire_expire_by_role in User Expire 8

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

Expires user by roles according to rules in the database.

2 calls to user_expire_expire_by_role()
UserExpireTest::testUserExpire in tests/src/Functional/UserExpireTest.php
Tests user expiration functionality.
user_expire_cron in ./user_expire.module
Implements hook_cron().

File

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

Code

function user_expire_expire_by_role() {
  $rules = user_expire_get_role_rules();
  $logger = \Drupal::logger('user_expire');
  foreach ($rules as $rid => $inactivity_period) {
    $uids_to_expire = user_expire_find_users_to_expire_by_role($rid, $inactivity_period);
    if ($uids_to_expire) {
      foreach ($uids_to_expire as $uid) {
        $account = \Drupal::entityTypeManager()
          ->getStorage('user')
          ->load($uid->uid);
        if (!$account) {
          $logger
            ->warning('Skipping @uid as it failed to load a valid user', [
            '@uid' => $uid->uid,
          ]);
        }
        else {
          $logger
            ->info('Expiring account @name by role', [
            '@name' => $account
              ->getAccountName(),
          ]);
          user_expire_expire_user($account);
        }
      }
    }
  }
}