| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\MailAuthCpanel; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | class Module extends \Aurora\System\Module\AbstractModule |
| 20: | { |
| 21: | protected $aRequireModules = array( |
| 22: | 'Mail' |
| 23: | ); |
| 24: | |
| 25: | public $oApiMailManager = null; |
| 26: | public $oApiAccountsManager = null; |
| 27: | public $oApiServersManager = null; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public function init() {} |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | protected function OnLogin($aArgs, &$mResult) |
| 45: | { |
| 46: | $bResult = false; |
| 47: | $oServer = null; |
| 48: | $iUserId = 0; |
| 49: | |
| 50: | $aLoginParts = explode('/', $aArgs['Login']); |
| 51: | if (!is_array($aLoginParts) || $aLoginParts[0] == '') |
| 52: | { |
| 53: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); |
| 54: | } |
| 55: | $aArgs['Email'] = $aLoginParts[0]; |
| 56: | $oAccount = \Aurora\System\Api::getModule('Mail')->getAccountsManager()->getAccountUsedToAuthorize($aArgs['Email']); |
| 57: | |
| 58: | $bNewAccount = false; |
| 59: | $bAutocreateMailAccountOnNewUserFirstLogin = \Aurora\Modules\Mail\Module::Decorator()->getConfig('AutocreateMailAccountOnNewUserFirstLogin', false); |
| 60: | |
| 61: | if ($bAutocreateMailAccountOnNewUserFirstLogin && !$oAccount) |
| 62: | { |
| 63: | $sEmail = $aArgs['Email']; |
| 64: | $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); |
| 65: | $oServer = \Aurora\System\Api::getModule('Mail')->getServersManager()->GetServerByDomain(strtolower($sDomain)); |
| 66: | if (!$oServer) |
| 67: | { |
| 68: | $oServer = \Aurora\System\Api::getModule('Mail')->getServersManager()->GetServerByDomain('*'); |
| 69: | } |
| 70: | if ($oServer) |
| 71: | { |
| 72: | $oAccount = new \Aurora\Modules\Mail\Models\MailAccount(); |
| 73: | $oAccount->Email = $aArgs['Email']; |
| 74: | $oAccount->IncomingLogin = $aArgs['Login']; |
| 75: | $oAccount->setPassword($aArgs['Password']); |
| 76: | $oAccount->ServerId = $oServer->Id; |
| 77: | $bNewAccount = true; |
| 78: | } |
| 79: | } |
| 80: | |
| 81: | if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) |
| 82: | { |
| 83: | try |
| 84: | { |
| 85: | if ($bAutocreateMailAccountOnNewUserFirstLogin || !$bNewAccount) |
| 86: | { |
| 87: | $bNeedToUpdatePasswordOrLogin = $aArgs['Password'] !== $oAccount->getPassword() || $aArgs['Login'] !== $oAccount->IncomingLogin; |
| 88: | $oAccount->IncomingLogin = $aArgs['Login']; |
| 89: | $oAccount->setPassword($aArgs['Password']); |
| 90: | |
| 91: | \Aurora\System\Api::getModule('Mail')->getMailManager()->validateAccountConnection($oAccount); |
| 92: | |
| 93: | if ($bNeedToUpdatePasswordOrLogin) |
| 94: | { |
| 95: | \Aurora\System\Api::getModule('Mail')->getAccountsManager()->updateAccount($oAccount); |
| 96: | } |
| 97: | |
| 98: | $bResult = true; |
| 99: | } |
| 100: | |
| 101: | if ($bAutocreateMailAccountOnNewUserFirstLogin && $bNewAccount) |
| 102: | { |
| 103: | $oUser = null; |
| 104: | $aSubArgs = array( |
| 105: | 'UserName' => $sEmail, |
| 106: | 'Email' => $sEmail, |
| 107: | 'UserId' => $iUserId |
| 108: | ); |
| 109: | $this->broadcastEvent( |
| 110: | 'CreateAccount', |
| 111: | $aSubArgs, |
| 112: | $oUser |
| 113: | ); |
| 114: | if ($oUser instanceof \Aurora\Modules\Core\Models\User) |
| 115: | { |
| 116: | $iUserId = $oUser->Id; |
| 117: | $bPrevState = \Aurora\System\Api::skipCheckUserRole(true); |
| 118: | $oAccount = \Aurora\Modules\Mail\Module::Decorator()->CreateAccount( |
| 119: | $iUserId, |
| 120: | $sEmail, |
| 121: | $sEmail, |
| 122: | $aArgs['Login'], |
| 123: | $aArgs['Password'], |
| 124: | array('ServerId' => $oServer->Id) |
| 125: | ); |
| 126: | \Aurora\System\Api::skipCheckUserRole($bPrevState); |
| 127: | if ($oAccount) |
| 128: | { |
| 129: | $oAccount->UseToAuthorize = true; |
| 130: | $oAccount->UseThreading = $oServer->EnableThreading; |
| 131: | $bResult = \Aurora\System\Api::getModule('Mail')->getAccountsManager()->updateAccount($oAccount); |
| 132: | } |
| 133: | else |
| 134: | { |
| 135: | $bResult = false; |
| 136: | } |
| 137: | } |
| 138: | } |
| 139: | |
| 140: | if ($bResult) |
| 141: | { |
| 142: | $mResult = \Aurora\System\UserSession::getTokenData($oAccount, $aArgs['SignMe']); |
| 143: | } |
| 144: | } |
| 145: | catch (\Aurora\System\Exceptions\ApiException $oException) |
| 146: | { |
| 147: | throw $oException; |
| 148: | } |
| 149: | catch (\Exception $oException) {} |
| 150: | } |
| 151: | |
| 152: | return $bResult; |
| 153: | } |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function Login($Login, $Password, $SignMe = false) |
| 166: | { |
| 167: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
| 168: | |
| 169: | $mResult = false; |
| 170: | |
| 171: | $aArgs = array ( |
| 172: | 'Login' => $Login, |
| 173: | 'Password' => $Password, |
| 174: | 'SignMe' => $SignMe |
| 175: | ); |
| 176: | $this->OnLogin( |
| 177: | $aArgs, |
| 178: | $mResult |
| 179: | ); |
| 180: | |
| 181: | if (is_array($mResult)) |
| 182: | { |
| 183: | $iTime = $SignMe ? 0 : time() + 60 * 60 * 24 * 30; |
| 184: | $sAuthToken = \Aurora\System\Api::UserSession()->Set($mResult, $iTime); |
| 185: | |
| 186: | \Aurora\System\Api::LogEvent('login-success: ' . $Login, self::GetName()); |
| 187: | return array( |
| 188: | 'AuthToken' => $sAuthToken |
| 189: | ); |
| 190: | } |
| 191: | |
| 192: | \Aurora\System\Api::LogEvent('login-failed: ' . $Login, self::GetName()); |
| 193: | if (!is_writable(\Aurora\System\Api::DataPath())) |
| 194: | { |
| 195: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::SystemNotConfigured); |
| 196: | } |
| 197: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AuthError); |
| 198: | } |
| 199: | |
| 200: | } |
| 201: | |