| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- /**
- * Class CreateAccountCosQuota
- * User: Nessandro
- * Date: 2019-10-07
- * Time: 14:05
- * @package ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Create
- */
- namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Create;
- use ModulesGarden\Servers\ZimbraEmail\App\Enums\ProductParams;
- use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
- use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
- use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
- use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
- class CreateAccountCosQuota extends CreateAccount
- {
- /**
- * array with class of service models
- * @var array
- */
- protected $cosModels = [];
- /**
- * configure service before will run
- */
- protected function config()
- {
- /**
- *
- * load class of service models
- */
- $this->cosModels = $this->api->repository()->cos->all();
- /**
- * parent configuration
- */
- parent::config();
- }
- /**
- *
- * @return bool
- */
- public function isValid()
- {
- /**
- *
- * if parent service is not valid, return false
- */
- if (!$result = parent::isValid())
- {
- return $result;
- }
- /**
- * check limits for dedicated class of service
- */
- $groupedByCos = $this->api->repository()->accounts->getGroupedByCos($this->formData['domain']);
- $cosLimits = $this->productManager->getSettingCos();
- /**
- *
- * if is configured one quote in product configuration and provided by client is not the same
- */
- if(is_string($cosLimits) && $cosLimits !== $this->formData['cosId'])
- {
- $this->setError('Quota id is not valid.');
- return false;
- }
- /**
- *
- * if is configured one quote
- */
- if(is_string($cosLimits) && $cosLimits === $this->formData['cosId']){
- return true;
- }
- /**
- * check if type of quote is availabel for client
- */
- if(!array_key_exists($this->formData['cosId'], $cosLimits))
- {
- $this->setError('There is no quota available to use');
- return false;
- }
- /**
- * check if limit has been reached
- */
- if ($cosLimits[$this->formData['cosId']] !== Size::UNLIMITED && count($groupedByCos[$this->formData['cosId']]) >= $cosLimits[$this->formData['cosId']] && $this->formData['cosId'] !== $this->formData['currentCosId'])
- {
- $this->setError('There are too many mailboxes with selected quota limit');
- return false;
- }
- return true;
- }
- /**
- * @return Account
- */
- protected function getModel()
- {
- /**
- * create new account in zimbra
- */
- $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_POSTAL_CODE, $this->formData['post_code']);
- $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
- $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
- $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']);
- /* @var $cos ClassOfService*/
- $cos = $this->cosModels[$this->formData['cosId']];
- /**
- * set quota by class of service
- */
- $account->setAttr(Account::ATTR_MAIL_QUOTA, $cos->getDataResourceA(Account::ATTR_MAIL_QUOTA));
- /**
- * define class of services attribute for account
- */
- $cosAttrs = $cos->getAllDataResourcesAAttributes();
- foreach(Zimbra::BASE_ACCOUNT_CONFIG as $key)
- {
- $value = $cosAttrs[$key] ? $cosAttrs[$key] : Zimbra::ATTR_DISABLED;
- $account->setAttr($key, $value);
- }
- /**
- *
- * set class of service id as account attribute
- */
- $account->setAttr(Account::ATTR_CLASS_OF_SERVICE_ID, $cos->getId());
- return $account;
- }
- /**
- * @return mixed
- */
- protected function getMailboxes()
- {
- $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
- $filterbyCOS = $this->productManager->get('filterAccountsByCOS');
- /**
- * todo should be taken from option or from configurable options, is depend if from config option or dedicated class of service has been selected
- */
- $getConfigOptCos = $this->formData['cosId'];
- /**
- * filter mailboxes if required
- */
- if($getConfigOptCos && $filterbyCOS === ProductParams::SWITCHER_ENABLED)
- {
- $mailBoxes = array_filter($mailBoxes, function($row) use ($getConfigOptCos)
- {
- /* @var $row Account*/
- return $row->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID) == $getConfigOptCos;
- });
- }
- return $mailBoxes;
- }
- }
|