How to lock or unlock the account using the API

Have a great idea for extending Zimbra? Share ideas, ask questions, contribute, and get feedback.
Post Reply
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

How to lock or unlock the account using the API

Post by rojoblandino »

Greetings, i have a site for my call center users, and they can remotely check if an account in the zimbra server is locked or not, if they are locked, i want to add a process to generate the new password and change it, then unlocked the account, I am new with these and do not understand so much, I still keep reading but I am reaching an end path, I have not successfully accomplish my objective.

For accomplish that objective i have used the ZIMBRA API https://github.com/zimbra-api/zimbra-api/ and I have been able to change the password of the account, but I have not been able to unlock the account and i do not know what am i doing wrong.

This is a test code, I will later send by parameters the account to be modified and the new password of the function I already have. But only the unlock is not working.

Code: Select all

<?php

// This file is generated by Composer
require_once 'vendor/autoload.php';

$api = \Zimbra\Admin\AdminFactory::instance('https://localhost:7071/service/admin/soap');
$api->auth('admin', 'adminpasswordhere');
$account = new \Zimbra\Struct\AccountSelector(\Zimbra\Enum\AccountBy::NAME(), 'username');
//Process to change password
$id = ((array)$api->getAccountInfo($account)->a[0])["_"];
echo $id."\n";
$passwordInfo = $api->setPassword($id,"mynewpasswordhere");


//I will try to unlock o lock the account
$accountns = new \Zimbra\Struct\AccountNameSelector(\Zimbra\Enum\AccountBy::NAME(), 'username','username');
// I have tried with END() and nothing is done
$req=new \Zimbra\Admin\Request\LockoutMailbox($accountns,\Zimbra\Enum\LockoutOperation::START());

I have been trying to translate to php the example in following url but not success, and there is not any another examples on the web about how to accomplish these steps
http://useof.org/java-open-source/com.z ... xRequest/2

Any tip or example using PHP would be usefull thanks?
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

Re: How to lock or unlock the account using the API

Post by rojoblandino »

I have test following steps:

Code: Select all

<?php
  
// This file is generated by Composer
require_once 'vendor/autoload.php';

$api = \Zimbra\Admin\AdminFactory::instance('https://10.0.10.40:7071/service/admin/soap');
$api->auth('admin', 'adminpasswordhere');
$account = new \Zimbra\Struct\AccountSelector(\Zimbra\Enum\AccountBy::NAME(), 'username');
$id = ((array)$api->getAccountInfo($account)->a[0])["_"];
echo $id."\n";
$passwordInfo = $api->setPassword($id,"mynewpasswordhere");

$accountns = new \Zimbra\Struct\AccountNameSelector(\Zimbra\Enum\AccountBy::NAME(), 'username' ,'username');
$req=new \Zimbra\Admin\Request\LockoutMailbox($accountns,\Zimbra\Enum\LockoutOperation::END());
$req->setOperation(\Zimbra\Enum\LockoutOperation::START())->setAccount($accountns);
Some step i am missing, or maybe it could accomplished using "vendor/zimbra-api/soap-api/src/Zimbra/Admin/Request/ModifyAccount.php" but i do not find any example of how the attrs array in parameter should be used and what attr should be placed for the status of the account.
User avatar
jholder
Ambassador
Ambassador
Posts: 4824
Joined: Fri Sep 12, 2014 10:00 pm

Re: How to lock or unlock the account using the API

Post by jholder »

You need to set the zimbraAccountStatus attribute for a user to locked or active.
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

Re: How to lock or unlock the account using the API

Post by rojoblandino »

I know the attribute, but i have not found a way to change it via the api.

This is what i have tried:

Code: Select all

// This file is generated by Composer
require_once 'vendor/autoload.php';

$api = \Zimbra\Admin\AdminFactory::instance('https://10.0.10.40:7071/service/admin/soap');
$api->[code]
auth('admin', 'adminpasswordhere');
$account = new \Zimbra\Struct\AccountSelector(\Zimbra\Enum\AccountBy::NAME(), 'username');
$id = ((array)$api->getAccountInfo($account)->a[0])["_"];
echo $id."\n";
$passwordInfo = $api->setPassword($id,"mynewpasswordhere");

$api->modifyAccount($id,\Zimbra\Struct\KeyValuePair("zimbraAccountStatus","active"));
[/code]

But i am getting, following output, where i am getting the output of password changed successfully but not the status:

Code: Select all

65668343-2f24-4bfe-a0c1-f86836ca06ed
PHP Fatal error:  Uncaught Error: Call to undefined function Zimbra\Struct\KeyValuePair() in /home/intracsc/public_html/ChangePassword.php:12
Stack trace:
#0 {main}
  thrown in /home/intracsc/public_html/ChangePassword.php on line 12
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

Re: How to lock or unlock the account using the API

Post by rojoblandino »

I have done it:

Successfully by try and failure, thanks to the tip of jholder:
You need to set the zimbraAccountStatus attribute for a user to locked or active.
I tried the function modifyAccount, but i was using it wrongly, after searching and searching any example in google without success and reading the page https://files.zimbra.com/docs/soap_api/ ... index.html

I try following code by chance:

Code: Select all

// This file is generated by Composer
require_once 'vendor/autoload.php';

$api = \Zimbra\Admin\AdminFactory::instance('https://10.0.10.40:7071/service/admin/soap');
$api->[code]
auth('admin', 'adminpasswordhere');
$account = new \Zimbra\Struct\AccountSelector(\Zimbra\Enum\AccountBy::NAME(), 'username');
$id = ((array)$api->getAccountInfo($account)->a[0])["_"];
//echo $id."\n";
$passwordInfo = $api->setPassword($id,"mynewpasswordhere");

$attr=new \Zimbra\Struct\KeyValuePair("zimbraAccountStatus","active");
$api->modifyAccount($id,[$attr]);
[/code]

And Booya! It worked!, by chance i got the tip from here https://hotexamples.com/examples/zimbra ... mples.html. This is the part of the code that caught my attention from that URL:

Code: Select all

...
     $attr = new KeyValuePair($key, $value);
     $dataSource = new DataSourceSpecifier(DataSourceType::POP3(), $name, [$attr]); 
...
the KeyValuePair is pass as parameter inside of "[]", that solve and fixed what i was looking for.

Thanks "jholder".

It can be marked as solved!
User avatar
jholder
Ambassador
Ambassador
Posts: 4824
Joined: Fri Sep 12, 2014 10:00 pm

Re: How to lock or unlock the account using the API

Post by jholder »

Glad it worked!

Here's another tip:
If you want to see the call for an action that you know how to run, then run zmprov -d
This will print the call, and the response, and you can fill in the variables.
Technically, that PHP API isn't supported, so many of us won't be able to help you solve issues with it. But if you know the calls (or how to find them) you can probably write anything.

So:

Code: Select all

zmprov ma jholder@zimbra.com zimbraAccountStatus CLOSED
would be:

Code: Select all

First, it gets an auth token
========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="0"/>
    </context>
  </soap:Header>
  <soap:Body>
    <AuthRequest xmlns="urn:zimbraAdmin">
      <name>zimbra</name>
      <password>[PASSWORD HERE]</password>
    </AuthRequest>
  </soap:Body>
</soap:Envelope>
===============================
======== SOAP RECEIVE =========
<AuthResponse xmlns="urn:zimbraAdmin">
  <authToken>[TOKEN HERE]</authToken>
  <lifetime>43199998</lifetime>
</AuthResponse>
=============================== (123 msecs)
Then it checks the current attributes:

Code: Select all

========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <authToken>[AUTH TOKEN HERE]</authToken>
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="1"/>
    </context>
  </soap:Header>
  <soap:Body>
    <GetAccountRequest applyCos="1" xmlns="urn:zimbraAdmin">
      <account by="name">admin@zimbra.io</account>
    </GetAccountRequest>
  </soap:Body>
</soap:Envelope>
===============================
======== SOAP RECEIVE =========
<GetAccountResponse xmlns="urn:zimbraAdmin">
  <account name="admin@zimbra.io" id="d178a0d3-2f93-48ea-9f17-c72bc79e9786">
[all account attributes here]
  </account>
</GetAccountResponse>
=============================== (58 msecs)
Sets the account status to closed via :<a n="zimbraAccountStatus">closed</a>

Code: Select all

========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <authToken[TOKEN HERE]</authToken>
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="1"/>
    </context>
  </soap:Header>
  <soap:Body>
    <ModifyAccountRequest xmlns="urn:zimbraAdmin">
      <id>d178a0d3-2f93-48ea-9f17-c72bc79e9786</id>
      <a n="zimbraAccountStatus">closed</a>
    </ModifyAccountRequest>
  </soap:Body>
</soap:Envelope>
===============================
Finally it gets the attributes again

Code: Select all

======== SOAP RECEIVE =========
<ModifyAccountResponse xmlns="urn:zimbraAdmin">
  <account name="admin@zimbra.io" id="d178a0d3-2f93-48ea-9f17-c72bc79e9786">
 [All account attributes here]
  </account>
</ModifyAccountResponse>
Will print out the raw soap call used. You can use this for any method to figure out the SOAP call. It works for zmmailbox too.
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

Re: How to lock or unlock the account using the API

Post by rojoblandino »

jholder wrote:Glad it worked!

Here's another tip:
If you want to see the call for an action that you know how to run, then run zmprov -d
This will print the call, and the response, and you can fill in the variables.
Technically, that PHP API isn't supported, so many of us won't be able to help you solve issues with it. But if you know the calls (or how to find them) you can probably write anything.

So:

Code: Select all

zmprov ma jholder@zimbra.com zimbraAccountStatus CLOSED
would be:

Code: Select all

First, it gets an auth token
========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="0"/>
    </context>
  </soap:Header>
  <soap:Body>
    <AuthRequest xmlns="urn:zimbraAdmin">
      <name>zimbra</name>
      <password>[PASSWORD HERE]</password>
    </AuthRequest>
  </soap:Body>
</soap:Envelope>
===============================
======== SOAP RECEIVE =========
<AuthResponse xmlns="urn:zimbraAdmin">
  <authToken>[TOKEN HERE]</authToken>
  <lifetime>43199998</lifetime>
</AuthResponse>
=============================== (123 msecs)
Then it checks the current attributes:

Code: Select all

========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <authToken>[AUTH TOKEN HERE]</authToken>
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="1"/>
    </context>
  </soap:Header>
  <soap:Body>
    <GetAccountRequest applyCos="1" xmlns="urn:zimbraAdmin">
      <account by="name">admin@zimbra.io</account>
    </GetAccountRequest>
  </soap:Body>
</soap:Envelope>
===============================
======== SOAP RECEIVE =========
<GetAccountResponse xmlns="urn:zimbraAdmin">
  <account name="admin@zimbra.io" id="d178a0d3-2f93-48ea-9f17-c72bc79e9786">
[all account attributes here]
  </account>
</GetAccountResponse>
=============================== (58 msecs)
Sets the account status to closed via :<a n="zimbraAccountStatus">closed</a>

Code: Select all

========== SOAP SEND ==========
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <context xmlns="urn:zimbra">
      <authToken[TOKEN HERE]</authToken>
      <nosession/>
      <userAgent name="zmprov" version="8.8.15_GA_4180"/>
      <authTokenControl voidOnExpired="1"/>
    </context>
  </soap:Header>
  <soap:Body>
    <ModifyAccountRequest xmlns="urn:zimbraAdmin">
      <id>d178a0d3-2f93-48ea-9f17-c72bc79e9786</id>
      <a n="zimbraAccountStatus">closed</a>
    </ModifyAccountRequest>
  </soap:Body>
</soap:Envelope>
===============================
Finally it gets the attributes again

Code: Select all

======== SOAP RECEIVE =========
<ModifyAccountResponse xmlns="urn:zimbraAdmin">
  <account name="admin@zimbra.io" id="d178a0d3-2f93-48ea-9f17-c72bc79e9786">
 [All account attributes here]
  </account>
</ModifyAccountResponse>
Will print out the raw soap call used. You can use this for any method to figure out the SOAP call. It works for zmmailbox too.
Thanks but i am using that PHP Wrapper because i have look about SOAP but still do not understand how to make it work.

So i found that using the zimbra-api in the github works easy.

I mean having the soap, how do i send it to the server? I stuck there.

For now i could make it work, i will keep reading about SOAP anyway but i still do not understand what i do with the XML info and how to get the response and work with that.

I have read this url https://files.zimbra.com/docs/soap_api/ ... index.html

I am still reading https://www.php.net/manual/en/class.soapclient.php as example to make it work.

I need to read it carefully. I will write You after the change of the zimbra-api to the soap way.

The closest example i found is in this URL https://stackoverflow.com/questions/342 ... te-account

I have found a simple guide i will test and try for building the xml and for reading https://www.guru99.com/php-and-xml.html, next time i will try to look how to send it to the server and work with response.
Last edited by rojoblandino on Thu Mar 10, 2022 1:48 pm, edited 1 time in total.
User avatar
jholder
Ambassador
Ambassador
Posts: 4824
Joined: Fri Sep 12, 2014 10:00 pm

Re: How to lock or unlock the account using the API

Post by jholder »

The SOAP api is incredibly simple. Once you know how to use curl in PHP, then you just learn how to post. It's very very easy if you give it some time to learn.
rojoblandino
Advanced member
Advanced member
Posts: 52
Joined: Sat Sep 13, 2014 1:36 am

Re: How to lock or unlock the account using the API

Post by rojoblandino »

jholder wrote:The SOAP api is incredibly simple. Once you know how to use curl in PHP, then you just learn how to post. It's very very easy if you give it some time to learn.
Thanks, I will.
Post Reply