Kerio Connect Administration API - setting user quotas


2012/04/25

Recent versions of Kerio Connect support the "Administration API for Kerio Connect". This allows you to perform administrative tasks through scripting. This can be very convenient for making mass changes based on specific conditions.

For example, one of my customers wants to apply a 1 MB quota to any user who has never logged in and does not already have a quota set. That may sound like a strange request, but they are a large company and have many users that aren't necessarily using email yet. When they originally setup users, they did not assign quotas, but have started doing so to some users. Going in to find all the inactive users would be a lot of work to do manually, but an API script can do it quickly and easily.

I modified one of the example scripts that Kerio provides and also got some help from Lukas Nemec at the Kerio Developers forum. I had been playing with an earlier version of the API and was causing myself some trouble by reading current documentation without upgrading. Once I installed the current version, things worked MUCH better :)

To use this script, you'll need to download the Kerio api and install it per the instructions in the reference documentation found at the same link.

You'll also want to look at the documentation at http://manuals.kerio.com/connect/api/en/reference/index.html.

The script as presented here does not do extensive error checking. I have tested it against test servers, but (as usual) you need to understand what this does and you will use it at your own risk.

The script could be made more efficient by building an array of user id's and setting them all at once rather than individually. I didn't want to complicate this by doing that; it's easier to understand without it.

Here is sample output from two back-to-back runs.

<!--
/**
 * Administration API Sample Application.
 * 
 * Display all users with any administrator rights.
 * 
 * @copyright Copyright © 1997-2011 Kerio Technologies s.r.o.
 * and modified by A.P. Lawrence
 * 
 * See http://aplawrence.com/Kerio/api_user_quota.html
 *
 */
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="shortcut icon" href="favicon.ico">
        <title>Administration API</title>
    </head>
<body>

<?php
require_once(dirname(__FILE__) . '/examples/class/MyApi.php');

/* Application details */
$name = 'Set user quota';
$vendor = 'A.P. Lawrence';
$version = '1.0';


$hostname = '192.168.11.9';
$username = 'Admin';
$password = '753tkerioaHw2s93!';

$Api = new MyApi($name, $vendor, $version);
//$Api->setDebug(TRUE);

/* Local functions */
function getDomainList() {
    global $Api;
    $method = 'Domains.get';
    $params = array(
        'query' => array(
            'fields' => array(
                'id',
                'name'
            )
        )
    );
    $result = $Api->sendRequest($method, $params);
    return $result['list'];
}
function getUserList($domainId) {
    global $Api;
    $method = 'Users.get';
    $params = array(
        'query' => array(
            'fields' => array(
                'loginName',
                'id',
                'fullName',
                'lastLoginInfo',
                'consumedSize'
            ),
            'orderBy' => array(array(
                'columnName' => 'loginName',
                'direction' => 'Asc'
            ))
        ),
        'domainId' => $domainId
    );
    $result = $Api->sendRequest($method, $params);
    return $result['list'];
}

/* Main application */
print "<h1> Display users with no login and set quota</h1>";

try {

    
    
    /* Register application */
       date_default_timezone_set('America/New_York');
    /* Login */
    $login = $Api->login($hostname, $username, $password);
    /* Get domain list */
$domainList = $Api->getDomains(array('id', 'name'));
    foreach ($domainList as $domain) {
            $userList = $Api->getUsers(array('id','loginName',
            'fullName', 'diskSizeLimit', 'lastLoginInfo'),
            $domain['id']);

        foreach ($userList as $user) {
            $quota = $user['diskSizeLimit']['limit']['value'];

            if (! $user['lastLoginInfo']['dateTime']) {
                $id=$user['id'];
                $set="Already set";

                if (false == $user['diskSizeLimit']['isActive']) {
                    $set="Will set to 1 MB";
                    $params = array(
                        'userIds' => array(
                            $user['id']
                        ),

                        'pattern' => array(
                            'diskSizeLimit' => array(
                                'isActive' => TRUE,
                                'limit' => array(
                                    'value' => 1,
                                    'units' => 'MegaBytes'
                                )
                            )
                        )
                    );

                    $response = $Api->sendRequest('Users.set', $params);

                    $quota = '(From unlimited)';
                }
                $username = $user['loginName'] . '@' . $domain['name'];
                $fullname = $user['fullName'];

                printf('%s (%s) %s %s <br>', $username, $fullname, $set , $quota);
            }
        }
    }


} catch (Exception $error) {
    
    /* Catch possible errors */
    print $error->getMessage();
}

/* Logout */
if(isset($login)) {
    $Api->logout();
}

print '<hr>';
print date(DATE_RSS);
?>

</body>
</html>




Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Administration API - setting user quotas




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Your computer needn't be the first thing your see in the morning and the last thing you see at night. (Simon Mainwaring)

When Life Gives You Questions, Google has Answers (AJ Carpio)








This post tagged: