Beispiel #1
0
 /**
  * @param string $sEmail
  * @param string $sIncPassword
  * @param string $sIncLogin Default value is empty string.
  * @param string $sLanguage Default value is empty string.
  *
  * @throws CApiManagerException(Errs::WebMailManager_AccountDisabled) 1501
  * @throws CApiManagerException(Errs::Mail_AccountAuthentication) 4002
  * @throws CApiManagerException(Errs::WebMailManager_AccountCreateOnLogin) 1503
  *
  * @return CAccount|null|bool
  */
 public function loginToAccount($sEmail, $sIncPassword, $sIncLogin = '', $sLanguage = '')
 {
     $oResult = null;
     \CApi::AddSecret($sIncPassword);
     /* @var $oApiUsersManager CApiUsersManager */
     $oApiUsersManager = CApi::GetSystemManager('users');
     $bAuthResult = false;
     $oAccount = $oApiUsersManager->getAccountByEmail($sEmail);
     if ($oAccount instanceof CAccount) {
         if ($oAccount->IsDisabled || $oAccount->Domain && $oAccount->Domain->IsDisabled) {
             throw new CApiManagerException(Errs::WebMailManager_AccountDisabled);
         }
         if (0 < $oAccount->IdTenant) {
             $oApiTenantsManager = CApi::GetSystemManager('tenants');
             if ($oApiTenantsManager) {
                 $oTenant = $oApiTenantsManager->getTenantById($oAccount->IdTenant);
                 if ($oTenant && ($oTenant->IsDisabled || 0 < $oTenant->Expared && $oTenant->Expared < \time())) {
                     throw new CApiManagerException(Errs::WebMailManager_AccountDisabled);
                 }
             }
         }
         if (0 < strlen($sLanguage) && $sLanguage !== $oAccount->User->DefaultLanguage) {
             $oAccount->User->DefaultLanguage = $sLanguage;
         }
         if ($oAccount->Domain->AllowWebMail && $oAccount->AllowMail) {
             if ($sIncPassword !== $oAccount->IncomingMailPassword) {
                 $oAccount->IncomingMailPassword = $sIncPassword;
             }
             try {
                 \CApi::ExecuteMethod('Mail::ValidateAccountConnection', array('Account' => $oAccount));
             } catch (Exception $oException) {
                 throw $oException;
             }
         } else {
             if ($sIncPassword !== $oAccount->IncomingMailPassword) {
                 throw new CApiManagerException(Errs::Mail_AccountAuthentication);
             }
         }
         $sObsoleteIncPassword = $oAccount->GetObsoleteValue('IncomingMailPassword');
         $sObsoleteLanguage = $oAccount->User->GetObsoleteValue('DefaultLanguage');
         if (null !== $sObsoleteIncPassword && $sObsoleteIncPassword !== $oAccount->IncomingMailPassword || null !== $sObsoleteLanguage && $sObsoleteLanguage !== $oAccount->User->DefaultLanguage || $oAccount->ForceSaveOnLogin) {
             $oApiUsersManager->updateAccount($oAccount);
         }
         $oApiUsersManager->updateAccountLastLoginAndCount($oAccount->IdUser);
         $oResult = $oAccount;
     } else {
         if (null === $oAccount) {
             $aExtValues = array();
             if (0 < strlen($sIncLogin)) {
                 $aExtValues['Login'] = $sIncLogin;
             }
             $aExtValues['ApiIntegratorLoginToAccountResult'] = $bAuthResult;
             $oAccount = \CApi::ExecuteMethod('Core::CreateAccount', array('Email' => $sEmail, 'Password' => $sIncPassword, 'Language' => $sLanguage, 'ExtValues' => $aExtValues));
             if ($oAccount instanceof CAccount) {
                 $oResult = $oAccount;
             } else {
                 throw new CApiManagerException(Errs::WebMailManager_AccountCreateOnLogin);
             }
         } else {
             $oException = $oApiUsersManager->GetLastException();
             throw is_object($oException) ? $oException : new CApiManagerException(Errs::WebMailManager_AccountCreateOnLogin);
         }
     }
     return $oResult;
 }