| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Delete;
- use ThurData\Servers\KerioEmail\App\Enums\Kerio;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioApiException;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioServiceException;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Enums\ApiErrorCodes;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
- /**
- *
- * Created by PhpStorm.
- * User: Tomasz Bielecki ( tomasz.bi@thurdata.com )
- * Date: 10.10.19
- * Time: 14:41
- * Class DeleteDomain
- */
- class DeleteDomain extends ApiService
- {
- protected $domain;
- /**
- * @return bool
- */
- public function isValid()
- {
- if(!$this->formData['domain'])
- {
- $this->setError('Domain not found');
- return false;
- }
- return true;
- }
- /**
- * @return mixed|void
- */
- public function process()
- {
- $this->setDomainAsActiveIfRequired();
- $this->removeLists();
- $this->removeAccounts();
- $this->removeDomain();
- if ($this->getError())
- {
- return false;
- }
- return true;
- }
- protected function setDomainAsActiveIfRequired()
- {
- $domain = $this->getOrLoadDomain();
- $domain->setAttrs([
- Domain::ATTR_DOMAIN_STATUS => Kerio::ACC_STATUS_ACTIVE,
- Domain::ATTR_MAIL_STATUS => Kerio::ENABLED,
- ]);
- $response = $this->api->domain->update($domain);
- return $this;
- }
- /**
- * remove distribution list
- *
- * @return bool
- */
- protected function removeLists()
- {
- /**
- * get lists
- */
- $lists = $this->api->repository()->lists->getAllDistributionListsByDomain($this->formData['domain']);
- /**
- * remove each list
- */
- foreach($lists as $list)
- {
- /* @var $list DistributionList */
- $result = $this->api->distributionList->delete($list);
- if($this->api->distributionList->getError())
- {
- $this->throwError($this->api->distributionList->getError());
- }
- }
- return true;
- }
- /**
- *
- * @return bool
- *
- */
- protected function removeAccounts()
- {
- /**
- * get accounts from Kerio
- */
- $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);
- /**
- * remove each account
- */
- foreach($accounts as $account)
- {
- $result = $this->api->account->delete($account);
- if($this->api->account->getError())
- {
- $this->throwError($this->api->account->getError());
- }
- }
- return true;
- }
- /**
- * remove domain from API
- *
- * @return bool
- */
- protected function removeDomain()
- {
- /**
- *
- * get domain from kerio
- */
- $domain = $this->getOrLoadDomain();
- /**
- * remove domain
- */
- $result = $this->api->domain->delete($domain);
- if(!$result && ApiErrorCodes::DOMAIN_DOES_NOT_EXISTS !== $this->api->domain->getLastResult()->getLastErrorCode())
- {
- $this->setError($this->api->domain->getLastResult()->getLastErrorCode());
- return false;
- }
- return true;
- }
- /**
- *
- * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain
- */
- protected function getOrLoadDomain()
- {
- if(!$this->domain)
- {
- $this->domain = $this->api->repository()->domains->getByName($this->formData['domain']);
- }
- if ($this->api->repository()->domains->getError())
- {
- $this->throwError($this->api->repository()->domains->getError());
- }
- return $this->domain;
- }
- }
|