| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- * Class CreateAccount
- * User: ThurData
- * Date: 2019-10-07
- * Time: 10:41
- * @package ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create
- */
- namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create;
- use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
- use ThurData\Servers\KerioEmail\App\Enums\Size;
- use ThurData\Servers\KerioEmail\App\Enums\Kerio;
- use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
- use ThurData\Servers\KerioEmail\Core\Helper\ConfigOptionsHelper;
- class CreateAccount extends ApiService
- {
- use ProductManagerHandler;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
- /**
- * added more conditions
- * @return bool
- */
- protected function isValid()
- {
- /**
- * check if product manager is set
- */
- if(!$this->productManager)
- {
- $this->setError('Product Manager Not Found');
- return false;
- }
- /**
- * domain name
- */
- if(!$this->formData['domain'])
- {
- $this->setError('Domain name can not be found.');
- return false;
- }
- /**
- * check full usages of domains
- */
- $maxDomainQuota = $this->productManager->get('domainMaxSize') * Size::B_TO_GB;
- $usages = $this->api->repository()->accounts->getFullUsages($this->formData['domain']);
- if($maxDomainQuota !== Size::UNLIMITED && $usages >= $maxDomainQuota)
- {
- $this->setError("Domain size limit has been reached");
- return false;
- }
- /**
- *
- * Check if accounts limit has been reached
- */
- $hosting = $this->productManager->getHosting();
- $configOption = new ConfigOptionsHelper;
- $acc_addObj = $configOption->getConfigurableOption($hosting->id, 'acc_add');
- $acc_add = $acc_addObj->qty ? $acc_addObj->qty : 0;
- $accountLimit = $this->productManager->get('acc_base') + $acc_add;
- $mailBoxes = $this->getMailboxes();
- if(count($mailBoxes) >= $accountLimit && $accountLimit !== ProductParams::SIZE_UNLIMITED && 'editAccountForm' !== $this->getRequestValue('loadData'))
- {
- $this->setError('There are too many mailboxes');
- return false;
- }
- /**
- *
- */
- return parent::isValid();
- }
- /**
- * @return mixed
- */
- protected function getMailboxes()
- {
- $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
- return $mailBoxes;
- }
- /**
- * @return Account
- */
- protected function getModel()
- {
- $accountSize = $this->productManager->get(ProductParams::ACCOUNT_SIZE);
- /**
- * create new account in kerio
- */
- $account = new Account();
- $account->setName($this->formData['username'].'@'.$this->formData['domain']);
- $account->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
- /**
- * set account attributes
- */
- $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
- $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
- $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
- $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
- $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
- $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
- $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
- $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
- $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
- $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
- $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
- $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
- $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
- $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
- $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
- $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);
- $account->setAttr(Account::ATTR_MAIL_QUOTA, $accountSize * Size::B_TO_MB);
- foreach($this->productManager->getKerioConfiguration() as $key => $value)
- {
- $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
- $account->setAttr($key, $value);
- }
- return $account;
- }
- /**
- * @return bool|mixed|Account|void
- */
- protected function process()
- {
- /**
- *
- */
- $model = $this->getModel();
- /**
- * create account in KERIO
- */
- $result = $this->api->account->create($model);
- /**
- * problem with create account
- */
- if(!$result)
- {
- $this->setError($this->api->account->getLastResult()->getLastErrorCode());
- return false;
- }
- return $result;
- }
- }
|