zimbra-api / soap-api

Zimbra SOAP client in PHP language
BSD 3-Clause "New" or "Revised" License
62 stars 48 forks source link

createAccount example #17

Open paulz1 opened 7 years ago

paulz1 commented 7 years ago

Is it possible to get a little createAccount example.

I read "CreateAccount SOAP Command" doc in zimbra docs, but still have some trouble to apllicate it.

Thank you very much.

paulz1 commented 7 years ago

Ok, finally I found how to did it : $create_account_res = $api->createAccount("testuser@domain.com", "secret");

Now the problems are : 1) How to check if user already exists? Or how to intercept an Exception that will be thrown because the user exists. 2) How to print (or analyse) the return of command, i.e. $create_account_res

I saw that $create_account_res has a type of object. It was an Xml that was converted into Object (there is a method toObject() for this). But I have somedifficulties to work with this object... how to do it? Or is it possible to get directly an Xml and not an object ?

kazimc commented 7 years ago

I will try to help for you with your first question.

You can try to add, if it's exists, already exists will be return. Or if you want to only check, you can try to get account details with GetAccount. if it's exists account details will return. if it's not, not found will be return.

I hope understand your question.

paulz1 commented 7 years ago

Thank you kazimc. But my issue is more concern about lack of documentation and/or examples for this project. And it's complicated by fact that I have not experience/knowledge in php development. Probably for php developers it should seem obvious how to use this library, but not for me.

So my question was most about code example, not the algorithm. Now, to get the things more concrete. This code is very ugly, but is it correct on the idea level? I know I do twice almost the same check. But it better check twice than zero...

$account_name='testuser@domain.com';
$account = new \Zimbra\Struct\AccountSelector(\Zimbra\Enum\AccountBy::NAME(), $account_name);
try {
     $accountInfo = $api->getAccountInfo($account);

     echo "Some problem!<br/>";
     echo "This account is probably exist already!<br/>";
     echo $accountInfo->name."<br/>";
     print_r($accountInfo);

     exit(1);
} catch (GuzzleHttp\Exception\ServerException $e) {
/*  
    Ok, server throw an Exception, so account probably does not exist.  
    echo print_r($e);
*/
}
echo "Trying to create account : ".$account_name."<br/>";

try {
     $createAccountRes = $api->createAccount($account_name, "secret");
} catch (GuzzleHttp\Exception\ServerException $e) {
    echo "Some problem!<br/>";
    echo "Account was NOT created!<br/>";
    echo "Server thrown an Error, so may be this account already exist?<br/>";
    echo "<br/>";
//    echo print_r($e);
}

There are some things that make me unhappy...

1) Solution with exit(-1) seems be VERY ugly. But I don't know how to do it otherwise. 2) If server thrown an Exception for some other reason, not because account exists already... I do not know, for example if the server is under maintenance mode. I will not know this reason. I will still think that the problem is that the account exists.