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