Aurora Corporate documentation

Deleting inactive users

This sample demonstrates deleting user accounts nobody logged into in a while, for example, within last 6 months. One of the reasons why you may wish to do it is to maintain a reasonable number of user accounts, in order to stay within licensed users count.

Please note that deleting user account is an irreversible operation, all the contacts, calendars, account settings and linked accounts will be removed. Since mail is stored on IMAP server, mail messages and folders will not be affected. Should this user log in again, they will get back into their email account just fine.

<?php
include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$ul = $oCoreDecorator->GetUsers();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
    $sEmail = $item["PublicId"];
    $oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
    $dTime = $oUser->LastLogin; $iAge = time()-strtotime($dTime);
    if (($dTime !== null)&&($iAge > 6*30*24*60*60)) {
        $iUserId = $oUser->EntityId;
        $oCoreDecorator->DeleteUser($iUserId);
        echo "<p>$sEmail user deleted</p>";
    }
}

For production use, you'll probably wish to replace echo line with adding a log record, especially if this is placed into cronjobs for running periodically.

We're omitting the accounts which don't have LastLogin information saved for them - for instance, the account was created in admin panel but user never logged into it. Should you wish to remove those users as well, modify the condition as follows:

    if (($dTime === null)||($iAge > 6*30*24*60*60)) {


The code was tested on current v9 of Aurora Corporate and should work just fine on the previous 8.* as well.

Now let's complicate the task a little bit, assuming that you wish to get last login info of all the users, and then proceed with deleting the inactive ones - this can be done on two separate installations, if users log into WebMail but deleting needs to be done on mailserver level. We're going to use Batch delete user accounts page as a reference here:

<?php
function entry_add($str, $login, $add=TRUE) {
    file_put_contents("./users.txt", $str."\t".$login."\r\n", $add?FILE_APPEND:0);
}
function entry_adds($str, $add=TRUE) {
    file_put_contents("./users-del.txt", $str."\r\n", $add?FILE_APPEND:0);
}
include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$ul = $oCoreDecorator->GetUsers();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
    $sEmail = $item["PublicId"];
    $oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
    $dTime = $oUser->LastLogin; 
    entry_add($sEmail, $dTime);
    $iAge = time()-strtotime($dTime);
    if (($dTime === null)||($iAge > 2*30*24*60*60)) { 
        entry_adds($sEmail);
    }
}

As you can see, the script maintains two lists of users: users.txt will hold the list of all the accounts, with their last login info (in YYYY-MM-DD hh:mm:ss format), while users-del.txt is only about inactive users - those who either never logged in, or didn't login within the last 2 months. Now you can use the second list with the second part of the sample from here to delete the inactive users:

<?php
$aDelUsers = file('./users-del.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\System\Api::GetModuleDecorator('Core');
foreach ($aDelUsers as $sEmail) {
    $oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
    if ($oUser !== null) {
        $iUserId = $oUser->EntityId;
        $oCoreDecorator->DeleteUser($iUserId);
    }
}

WARNING: Last Login info only reflects when the user has actually logged into the web interface; this information isn't updated when user is automatically logged into their session - thus, please use this information with caution.