| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\ActiveServer; |
| 9: | |
| 10: | use Aurora\Api; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | class Module extends \Aurora\System\Module\AbstractModule |
| 22: | { |
| 23: | protected $aRequireModules = array( |
| 24: | 'Licensing' |
| 25: | ); |
| 26: | |
| 27: | public function init() |
| 28: | { |
| 29: | $this->subscribeEvent('Login::after', array($this, 'onAfterLogin'), 10); |
| 30: | $this->subscribeEvent('Core::CreateUser::after', array($this, 'onAfterCreateUser'), 10); |
| 31: | $this->subscribeEvent('Autodiscover::GetAutodiscover::after', array($this, 'onAfterGetAutodiscover')); |
| 32: | $this->subscribeEvent('Licensing::UpdateSettings::after', array($this, 'onAfterUpdateLicensingSettings')); |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | public static function getInstance() |
| 39: | { |
| 40: | return parent::getInstance(); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public static function Decorator() |
| 47: | { |
| 48: | return parent::Decorator(); |
| 49: | } |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function getModuleSettings() |
| 55: | { |
| 56: | return $this->oModuleSettings; |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | protected function getFreeUsersSlots() |
| 62: | { |
| 63: | $mResult = 0; |
| 64: | |
| 65: | |
| 66: | $oLicensing = \Aurora\System\Api::GetModule('Licensing'); |
| 67: | if ($oLicensing->IsTrial('ActiveServer') || $oLicensing->IsUnlim('ActiveServer')) { |
| 68: | $mResult = 1; |
| 69: | } else { |
| 70: | $iLicensedUsersCount = (int) $oLicensing->GetUsersCount('ActiveServer'); |
| 71: | $iUsersCount = $this->GetUsersCount(); |
| 72: | $mResult = $iLicensedUsersCount - $iUsersCount; |
| 73: | } |
| 74: | return $mResult; |
| 75: | } |
| 76: | |
| 77: | public function onAfterLogin(&$aArgs, &$mResult) |
| 78: | { |
| 79: | $sAgent = $this->oHttp->GetHeader('X-User-Agent'); |
| 80: | if ($sAgent === 'Afterlogic ActiveServer') { |
| 81: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
| 82: | |
| 83: | |
| 84: | $oLicensing = \Aurora\System\Api::GetModule('Licensing'); |
| 85: | |
| 86: | if (!$oLicensing->ValidatePeriod('ActiveServer')) { |
| 87: | $mResult = false; |
| 88: | Api::Log('Auth error: ActiveServer is invalid'); |
| 89: | } elseif ($this->getFreeUsersSlots() < 0) { |
| 90: | $mResult = false; |
| 91: | Api::Log('Auth error: User limit exceeded, ActiveServer is disabled'); |
| 92: | } elseif (!($oUser && $oUser->getExtendedProp(self::GetName() . '::Enabled'))) { |
| 93: | $mResult = false; |
| 94: | Api::Log('Auth error: ActiveServer is not enabled for the user'); |
| 95: | } |
| 96: | } |
| 97: | } |
| 98: | |
| 99: | public function onAfterCreateUser(&$aArgs, &$mResult) |
| 100: | { |
| 101: | $iUserId = isset($mResult) && (int) $mResult > 0 ? (int) $mResult : 0; |
| 102: | if ($iUserId > 0) { |
| 103: | $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck($iUserId); |
| 104: | |
| 105: | if ($oUser) { |
| 106: | if ($this->getFreeUsersSlots() < 1) { |
| 107: | if ($oUser->getExtendedProp(self::GetName() . '::Enabled')) { |
| 108: | $oUser->setExtendedProp(self::GetName() . '::Enabled', false); |
| 109: | \Aurora\Modules\Core\Module::Decorator()->UpdateUserObject($oUser); |
| 110: | } |
| 111: | } elseif ($oUser->getExtendedProp(self::GetName() . '::Enabled') !== $this->oModuleSettings->EnableForNewUsers) { |
| 112: | $oUser->setExtendedProp(self::GetName() . '::Enabled', $this->oModuleSettings->EnableForNewUsers); |
| 113: | \Aurora\Modules\Core\Module::Decorator()->UpdateUserObject($oUser); |
| 114: | } |
| 115: | } |
| 116: | } |
| 117: | } |
| 118: | |
| 119: | public function onAfterGetAutodiscover(&$aArgs, &$mResult) |
| 120: | { |
| 121: | $sEmail = $aArgs['Email']; |
| 122: | |
| 123: | $sResult = \implode("\n", array( |
| 124: | ' <Culture>en:us</Culture>', |
| 125: | ' <User>', |
| 126: | ' <DisplayName>' . $sEmail . '</DisplayName>', |
| 127: | ' <EMailAddress>' . $sEmail . '</EMailAddress>', |
| 128: | ' </User>', |
| 129: | ' <Action>', |
| 130: | ' <Settings>', |
| 131: | ' <Server>', |
| 132: | ' <Type>MobileSync</Type>', |
| 133: | ' <Url>https://' . $this->oModuleSettings->Server . '/Microsoft-Server-ActiveSync</Url>', |
| 134: | ' <Name>https://' . $this->oModuleSettings->Server . '/Microsoft-Server-ActiveSync</Name>', |
| 135: | ' </Server>', |
| 136: | ' </Settings>', |
| 137: | ' </Action>' |
| 138: | )); |
| 139: | |
| 140: | $mResult = $mResult . $sResult; |
| 141: | } |
| 142: | |
| 143: | public function onAfterUpdateLicensingSettings(&$aArgs, &$mResult, &$mSubscriptionsResult) |
| 144: | { |
| 145: | if ($this->getFreeUsersSlots() < 0) { |
| 146: | $mSubscriptionsResult = [ |
| 147: | 'Result' => false, |
| 148: | 'ErrorCode' => 1, |
| 149: | 'ErrorMessage' => 'User limit exceeded, ActiveServer is disabled.' |
| 150: | ]; |
| 151: | } |
| 152: | } |
| 153: | |
| 154: | protected function GetUsersCount() |
| 155: | { |
| 156: | return \Aurora\Modules\Core\Models\User::where('Properties->' . self::GetName() . '::Enabled', true)->count(); |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | public function GetEnableModuleForCurrentUser() |
| 167: | { |
| 168: | $bResult = false; |
| 169: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 170: | |
| 171: | $iUserId = \Aurora\System\Api::getAuthenticatedUserId(); |
| 172: | if ($iUserId) { |
| 173: | $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck($iUserId); |
| 174: | if ($oUser) { |
| 175: | $bResult = $oUser->getExtendedProp(self::GetName() . '::Enabled'); |
| 176: | } |
| 177: | } |
| 178: | |
| 179: | return $bResult; |
| 180: | } |
| 181: | |
| 182: | |
| 183: | public function GetPerUserSettings($UserId) |
| 184: | { |
| 185: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
| 186: | |
| 187: | $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck($UserId); |
| 188: | if ($oUser) { |
| 189: | return array( |
| 190: | 'EnableModule' => $oUser->getExtendedProp(self::GetName() . '::Enabled') |
| 191: | ); |
| 192: | } |
| 193: | |
| 194: | return null; |
| 195: | } |
| 196: | |
| 197: | public function UpdatePerUserSettings($UserId, $EnableModule) |
| 198: | { |
| 199: | $bResult = false; |
| 200: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
| 201: | |
| 202: | $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck($UserId); |
| 203: | |
| 204: | |
| 205: | $oLicensing = \Aurora\System\Api::GetModule('Licensing'); |
| 206: | $iLicensedUsersCount = (int) $oLicensing->GetUsersCount('ActiveServer'); |
| 207: | $iUsersCount = $this->GetUsersCount(); |
| 208: | if (!$oLicensing->IsTrial('ActiveServer') && !$oLicensing->IsUnlim('ActiveServer') && $iUsersCount >= $iLicensedUsersCount && $EnableModule && !$oUser->getExtendedProp(self::GetName() . '::Enabled')) { |
| 209: | throw new Exceptions\UserLimitExceeded(1, null, 'ActiveSync user limit exceeded.'); |
| 210: | } |
| 211: | |
| 212: | if ($oUser) { |
| 213: | $oUser->setExtendedProp(self::GetName() . '::Enabled', $EnableModule); |
| 214: | $bResult = \Aurora\Modules\Core\Module::Decorator()->UpdateUserObject($oUser); |
| 215: | } |
| 216: | |
| 217: | return $bResult; |
| 218: | } |
| 219: | |
| 220: | public function GetSettings() |
| 221: | { |
| 222: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 223: | |
| 224: | |
| 225: | $oLicensing = \Aurora\System\Api::GetModule('Licensing'); |
| 226: | |
| 227: | $bEnableModuleForUser = false; |
| 228: | |
| 229: | $iUserId = \Aurora\System\Api::getAuthenticatedUserId(); |
| 230: | if ($iUserId) { |
| 231: | $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck($iUserId); |
| 232: | if ($oUser) { |
| 233: | $bEnableModuleForUser = $oUser->getExtendedProp(self::GetName() . '::Enabled'); |
| 234: | } |
| 235: | } |
| 236: | |
| 237: | $iFreeSlots = $this->getFreeUsersSlots(); |
| 238: | if ($iFreeSlots < 0) { |
| 239: | $iFreeSlots = 'User limit exceeded, ActiveSync is disabled'; |
| 240: | } |
| 241: | $mLicensedUsersCount = $oLicensing->IsTrial('ActiveServer') || $oLicensing->IsUnlim('ActiveServer') ? 'Unlim' : $oLicensing->GetUsersCount('ActiveServer'); |
| 242: | $mUsersFreeSlots = $oLicensing->IsTrial('ActiveServer') || $oLicensing->IsUnlim('ActiveServer') ? 'Unlim' : $iFreeSlots; |
| 243: | |
| 244: | return array( |
| 245: | 'EnableModule' => !$this->oModuleSettings->Disabled, |
| 246: | 'EnableModuleForUser' => $bEnableModuleForUser, |
| 247: | 'EnableForNewUsers' => $this->oModuleSettings->EnableForNewUsers, |
| 248: | 'UsersCount' => $this->GetUsersCount(), |
| 249: | 'LicensedUsersCount' => (int) $mLicensedUsersCount, |
| 250: | 'UsersFreeSlots' => $mUsersFreeSlots, |
| 251: | 'Server' => $this->oModuleSettings->Server, |
| 252: | 'LinkToManual' => $this->oModuleSettings->LinkToManual |
| 253: | ); |
| 254: | } |
| 255: | |
| 256: | public function UpdateSettings($EnableModule, $EnableForNewUsers, $Server, $LinkToManual) |
| 257: | { |
| 258: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); |
| 259: | |
| 260: | $bResult = false; |
| 261: | |
| 262: | try { |
| 263: | $this->setConfig('Disabled', !$EnableModule); |
| 264: | $this->setConfig('EnableForNewUsers', $EnableForNewUsers); |
| 265: | $this->setConfig('Server', $Server); |
| 266: | $this->setConfig('LinkToManual', $LinkToManual); |
| 267: | $bResult = $this->saveModuleConfig(); |
| 268: | } catch (\Exception $ex) { |
| 269: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings); |
| 270: | } |
| 271: | |
| 272: | return $bResult; |
| 273: | } |
| 274: | |
| 275: | public function GetLicenseInfo() |
| 276: | { |
| 277: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin); |
| 278: | |
| 279: | $mResult = false; |
| 280: | |
| 281: | |
| 282: | $oLicensing = \Aurora\System\Api::GetModule('Licensing'); |
| 283: | if ($oLicensing) { |
| 284: | $mResult = $oLicensing->GetLicenseInfo('ActiveServer'); |
| 285: | } |
| 286: | |
| 287: | return $mResult; |
| 288: | } |
| 289: | } |
| 290: | |