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\MailChangePasswordPoppassdPlugin;
9:
10: use Aurora\Modules\Mail\Models\MailAccount;
11:
12: /**
13: * Allows users to change passwords on their email accounts using POPPASSD protocol.
14: *
15: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
16: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
17: * @copyright Copyright (c) 2023, Afterlogic Corp.
18: *
19: * @property Settings $oModuleSettings
20: *
21: * @package Modules
22: */
23: class Module extends \Aurora\System\Module\AbstractModule
24: {
25: /**
26: * @var Poppassd
27: */
28: protected $oPopPassD;
29:
30: public function init()
31: {
32: $this->oPopPassD = null;
33:
34: $this->subscribeEvent('Mail::Account::ToResponseArray', array($this, 'onMailAccountToResponseArray'));
35: $this->subscribeEvent('ChangeAccountPassword', array($this, 'onChangeAccountPassword'));
36: }
37:
38: /**
39: * @return Module
40: */
41: public static function getInstance()
42: {
43: return parent::getInstance();
44: }
45:
46: /**
47: * @return Module
48: */
49: public static function Decorator()
50: {
51: return parent::Decorator();
52: }
53:
54: /**
55: * @return Settings
56: */
57: public function getModuleSettings()
58: {
59: return $this->oModuleSettings;
60: }
61:
62: /**
63: * Adds to account response array information about if allowed to change the password for this account.
64: * @param array $aArguments
65: * @param mixed $mResult
66: */
67: public function onMailAccountToResponseArray($aArguments, &$mResult)
68: {
69: $oAccount = $aArguments['Account'];
70:
71: if ($oAccount && $this->checkCanChangePassword($oAccount)) {
72: if (!isset($mResult['Extend']) || !is_array($mResult['Extend'])) {
73: $mResult['Extend'] = [];
74: }
75: $mResult['Extend']['AllowChangePasswordOnMailServer'] = true;
76: }
77: }
78:
79: /**
80: * Tries to change password for account if allowed.
81: * @param array $aArguments
82: * @param mixed $mResult
83: */
84: public function onChangeAccountPassword($aArguments, &$mResult)
85: {
86: $bPasswordChanged = false;
87: $bBreakSubscriptions = false;
88:
89: $oAccount = $aArguments['Account'] instanceof MailAccount ? $aArguments['Account'] : false;
90: if ($oAccount && $this->checkCanChangePassword($oAccount) && $oAccount->getPassword() === $aArguments['CurrentPassword']) {
91: $bPasswordChanged = $this->changePassword($oAccount, $aArguments['NewPassword']);
92: $bBreakSubscriptions = true; // break if Poppassd plugin tries to change password in this account.
93: }
94:
95: if (is_array($mResult)) {
96: $mResult['AccountPasswordChanged'] = $mResult['AccountPasswordChanged'] || $bPasswordChanged;
97: }
98:
99: return $bBreakSubscriptions;
100: }
101:
102: /**
103: * Checks if allowed to change password for account.
104: * @param \Aurora\Modules\Mail\Models\MailAccount $oAccount
105: * @return bool
106: */
107: protected function checkCanChangePassword($oAccount)
108: {
109: $bFound = in_array('*', $this->oModuleSettings->SupportedServers);
110:
111: if (!$bFound) {
112: $oServer = $oAccount->getServer();
113:
114: if ($oServer && in_array($oServer->IncomingServer, $this->oModuleSettings->SupportedServers)) {
115: $bFound = true;
116: }
117: }
118:
119: return $bFound;
120: }
121:
122: /**
123: * Tries to change password for account.
124: * @param \Aurora\Modules\Mail\Models\MailAccount $oAccount
125: * @param string $sPassword
126: * @return boolean
127: * @throws \Aurora\System\Exceptions\ApiException
128: */
129: protected function changePassword($oAccount, $sPassword)
130: {
131: $bResult = false;
132:
133: if (0 < strlen($oAccount->getPassword()) && $oAccount->getPassword() !== $sPassword) {
134: if (null === $this->oPopPassD) {
135: $this->oPopPassD = new Poppassd(
136: $this->oModuleSettings->Host,
137: $this->oModuleSettings->Port
138: );
139: }
140:
141: if ($this->oPopPassD && $this->oPopPassD->Connect()) {
142: try {
143: if ($this->oPopPassD->Login($oAccount->IncomingLogin, $oAccount->getPassword())) {
144: $aNewPasswordResult = $this->oPopPassD->NewPass($sPassword);
145: if (!$aNewPasswordResult[0]) {
146: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountNewPasswordRejected, null, $aNewPasswordResult[1]);
147: } else {
148: $bResult = true;
149: }
150: } else {
151: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountOldPasswordNotCorrect);
152: }
153: } catch (\Exception $oException) {
154: $this->oPopPassD->Disconnect();
155: throw $oException;
156: }
157: } else {
158: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Exceptions\Errs::UserManager_AccountNewPasswordUpdateError);
159: }
160: }
161:
162: return $bResult;
163: }
164:
165: /**
166: * Obtains list of module settings for super admin.
167: * @return array
168: */
169: public function GetSettings()
170: {
171: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin);
172:
173: $sSupportedServers = implode("\n", $this->oModuleSettings->SupportedServers);
174:
175: $aAppData = array(
176: 'SupportedServers' => $sSupportedServers,
177: 'Host' => $this->oModuleSettings->Host,
178: 'Port' => $this->oModuleSettings->Port,
179: );
180:
181: return $aAppData;
182: }
183:
184: /**
185: * Updates module's super admin settings.
186: * @param string $SupportedServers
187: * @param string $Host
188: * @param int $Port
189: * @return boolean
190: */
191: public function UpdateSettings($SupportedServers, $Host, $Port)
192: {
193: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin);
194:
195: $aSupportedServers = preg_split('/\r\n|[\r\n]/', $SupportedServers);
196:
197: $this->setConfig('SupportedServers', $aSupportedServers);
198: $this->setConfig('Host', $Host);
199: $this->setConfig('Port', $Port);
200:
201: return $this->saveModuleConfig();
202: }
203: }
204: