Accounts.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractApiClient;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\AbstractRepository;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain;
  9. /**
  10. *
  11. * Created by PhpStorm.
  12. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  13. * Date: 10.09.19
  14. * Time: 08:28
  15. * Class Accounts
  16. */
  17. class Accounts extends AbstractRepository
  18. {
  19. const NO_COS_INDEX = 'default';
  20. /**
  21. * get all account for domain
  22. * @param $name
  23. * @return mixed
  24. */
  25. public function getByDomainName($name)
  26. {
  27. $domain = new Domain();
  28. $domain->setName($name);
  29. $result = $this->getClient()->account->getAllByDomain($domain);
  30. $accounts = $result->getResponseBody()['GETALLACCOUNTSRESPONSE']['ACCOUNT'];
  31. /**
  32. * API return one or araay with accounts
  33. */
  34. $tmp = [];
  35. if(isset($accounts['NAME']))
  36. {
  37. $tmpAccount = new Account($accounts);
  38. /**
  39. * skip ZiImbra default account
  40. */
  41. if (strpos($tmpAccount->getName(), 'galsync@') !== false)
  42. {
  43. return [];
  44. }
  45. $tmp[$tmpAccount->getId()] = $tmpAccount;
  46. }else{
  47. foreach($accounts as $account)
  48. {
  49. $tmpAccount = new Account($account);
  50. /**
  51. * skip ZiImbra default account
  52. */
  53. if (strpos($tmpAccount->getName(), 'galsync@') !== false)
  54. {
  55. continue;
  56. }
  57. $tmp[$tmpAccount->getId()] = $tmpAccount;
  58. }
  59. }
  60. return $tmp;
  61. }
  62. /**
  63. * @param $name
  64. * @return mixed
  65. */
  66. public function getGroupedByCos($name)
  67. {
  68. $accounts = $this->getByDomainName($name);
  69. foreach($accounts as $account)
  70. {
  71. /* @var $account Account*/
  72. $cosId = $account->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  73. $key = $cosId ? $cosId : self::NO_COS_INDEX;
  74. $tmp[$key][] = $account;
  75. }
  76. return $tmp;
  77. }
  78. /**
  79. * @param $name
  80. * @return mixed
  81. */
  82. public function getMailboxes($name)
  83. {
  84. $accounts = $this->getByDomainName($name);
  85. foreach($accounts as $key => $account)
  86. {
  87. /* @var $account Account*/
  88. if (strpos($account->getName(), 'galsync@') !== false)
  89. {
  90. unset($accounts[$key]);
  91. continue;
  92. }
  93. }
  94. return $accounts;
  95. }
  96. /**
  97. * @param $id
  98. * @return Account
  99. */
  100. public function getAccountInfoById($id)
  101. {
  102. $account = new Account();
  103. $account->setId($id);
  104. $result = $this->getClient()->account->getAccountInfo($account);
  105. if(!$result->getLastError())
  106. {
  107. $body = $result->getResponseBody();
  108. $result = $account->fill($body['GETACCOUNTINFORESPONSE']);
  109. $result->setName($body['GETACCOUNTINFORESPONSE']['NAME']['DATA']);
  110. return $result;
  111. }
  112. return $result;
  113. }
  114. /**
  115. * @param $id
  116. * @return Account
  117. */
  118. public function getAccountOptionsById($id)
  119. {
  120. $account = new Account();
  121. $account->setId($id);
  122. $result = $this->getClient()->account->getAccountOptions($account);
  123. if(!$result->getLastError())
  124. {
  125. $body = $result->getResponseBody();
  126. return $account->fill($body['GETACCOUNTRESPONSE']['ACCOUNT']);
  127. }
  128. return $result;
  129. }
  130. /**
  131. * @param $name
  132. * @return mixed
  133. */
  134. public function getUsages($name)
  135. {
  136. $domain = new Domain();
  137. $domain->setName($name);
  138. $result = $this->getClient()->domain->getDomainUsages($domain);
  139. $accounts = $result->getResponseBody()['GETQUOTAUSAGERESPONSE']['ACCOUNT'];
  140. /**
  141. * API return one or araay with accounts
  142. */
  143. if(isset($accounts['NAME']))
  144. {
  145. $tmpAccount = new Account($accounts);
  146. $tmp[$tmpAccount->getId()] = $tmpAccount;
  147. }else{
  148. foreach($accounts as $account)
  149. {
  150. $tmpAccount = new Account($account);
  151. $tmp[$tmpAccount->getId()] = $tmpAccount;
  152. }
  153. }
  154. return $tmp;
  155. }
  156. /**
  157. * @description return usages of all accounts
  158. * @param $name
  159. * @return float
  160. */
  161. public function getFullUsages($name)
  162. {
  163. /**
  164. * count Usages
  165. */
  166. foreach($this->getUsages($name) as $acc)
  167. {
  168. $used += (float) $acc->getUsed();
  169. }
  170. $used = round($used / Size::B_TO_MB, 2);
  171. return $used;
  172. }
  173. /**
  174. * @param $domain
  175. * @return array
  176. */
  177. public function getAccountAliasesByDomainName($domain)
  178. {
  179. $accounts = $this->getByDomainName($domain);
  180. foreach($accounts as $account)
  181. {
  182. /* @var $account Account */
  183. foreach($account->getAliases() as $al)
  184. {
  185. $alias = new AccountAlias();
  186. $alias->setAccountId($account->getId());
  187. $alias->setAccountName($account->getName());
  188. $alias->setAlias($al);
  189. $data[] = $alias;
  190. }
  191. }
  192. return $data;
  193. }
  194. }