Connect. Communicate. Collaborate. Securely.

Home » Developer Zone » API/SDK Writers » Setting quotas on inactive users
  •  
pcunix

Messages: 594
Karma: 33
Send a private message to this user
I have a BIG customer (over 900 users) who wants to set a 1 MB quota for users who have never logged in.

I wrote this code and have tested it against very small domains.

I'm looking for advice on what could go wrong and what error checking I might want to add.


I realize it would be more efficient to build the array of id's and set them all at once - my concern is what happens if there is an error on one or more?

I also attached the code to make it easier for those who actually want to run it..

<!--
/**
 * Administration API Sample Application.
 * 
 * Display all users with any administrator rights.
 * 
 * <_at_>copyright Copyright &copy; 1997-2011 Kerio Technologies s.r.o.
 */
 -->
<!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__) . '/class/KConnect.class.php');

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


$hostname = '192.168.11.9';
$username = 'ksadmin';
$password = '7j5hkeriobJk3q87#';

$Api = new KConnect();
//$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 */
	$Api->setApplication($name, $vendor, $version);
       date_default_timezone_set('America/New_York');
	/* Login */
	$login = $Api->login($hostname, $username, $password);

	/* Get domain list */
	$domainList = getDomainList();
	foreach ($domainList as $domain) {
		$userList = getUserList($domain['id']);
		foreach ($userList as $user) {
if (! $user['lastLoginInfo']['dateTime']) {
                        $id=$user['id'];
		$params = array(
                  'userIds' => array(
                    $user['id']
                   ),
       
                'pattern' => array(
                  'diskSizeLimit' => array(
                    'isActive' => TRUE,
                    'limit' => array(
                      'value' => 1,
                      'units' => 'MegaBytes'
             )
          )
        )
      );
$response = $Api->sendRequest('Users.set', $params);
			$username = $user['loginName'] . '<_at_>' . $domain['name'];
			$fullname = $user['fullName'];
			printf('%s (%s) set   <br>', $username, $fullname);
		}
}
	}

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

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

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

</body>
</html>


Tony Lawrence
Kerio Preferred Partner and Reseller
Certified for Connect, Control
http://aplawrence.com
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Hello Tony,
first, thank you very much for sharing your work. It is incredible how quickly you have done such powerful script. Really impressive!

To your question, let me answer in two points.

Use latest Kerio APIs Client Library for PHP
From the code I can see it seems to me that you are using a pre-beta version announced last year. It is recommended to use the version available on http://www.kerio.com/developers (1.0.0.26) as there have been some changes in the library. It will help you upgrade to future versions less painful and also allows other members to run your script smoothly.

Use try/catch block
Basically, the whole code above will stop on first error. Let's have a scenario where you are setting a quota to 50 users and while processing user no. 20 some unexpected (damn) error has occurred. Then the remaining thirty remain unprocessed.

To solve it you can decide if stop it immediately when first error occurs or not.

try {
    doSomeStuff();
    try {
        doUserStuff();
    }
    catch (Error $e) {
        print 'Error in user loop: ' . $e->getMessage();
    }
    doNextUserStuff(); // will be executed if no error in doUserStuff()
}
catch (Error $e) {
    print 'Some serious error:' . $e->getMessage();
}

As you can see, you can easily wrap every API call by try/catch block and decide what to do. If just display an error and skip it or if you would rather stop the whole processing.

Is this answer sufficient? If you are looking for what all the errors can occur on the server, it could lead to confusing answers.

Best regards,

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
pcunix

Messages: 594
Karma: 33
Send a private message to this user
That's helpful, thank you.

As to "what could go wrong", my main concern is "can I break the server using these calls?" I've always avoided PHP (I prefer Perl) and am uncomfortable with its syntax so I make dumb mistakes. I wonder if I should just go the json route - might be less chance of falling into "wrong syntax in this language" errors..


Tony Lawrence
Kerio Preferred Partner and Reseller
Certified for Connect, Control
http://aplawrence.com
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Well, the server is supposed to stay alive Smile

Technically speaking, every create/set/delete call will create an entry in server's config.log (and also saved on disk). So of course if you would be doing this very often in business hours when your clients are using the server, server's response might be a bit laggy and users unhappy.

That's the only significant limitation I am aware of.

Best regards,

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
Previous Topic: Trying to get / print diskSizeLimit
Next Topic: How to share code I've written
Goto Forum:
  


Disclaimer:
Kerio discussion forums are intended for open communication between forum members and may contain information and material posted by members which may be useful in learning about Kerio products. The discussion forums are not intended to provide technical support for any specific product. Any information implied or expressed in the discussion forums is that of the posting member. Kerio is in no way responsible for the information posted in the forums, or its accuracy. Kerio employees may participate in the discussions, but their postings do not represent an offical position of the company on any issues raised or discussed. Kerio reserves the right to monitor and maintain the forums to promote free and accurate exchange of information.

Current Time: Sat Oct 21 10:25:25 CEST 2017

Total time taken to generate the page: 0.00424 seconds
.:: Contact :: Home ::.
Powered by: FUDforum 3.0.4.