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\MailMasterPassword;
9:
10: /**
11: * This module adds ability to login to the admin panel as a Super Administrator.
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: * @property Settings $oModuleSettings
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\System\Module\AbstractModule
22: {
23: /***** private functions *****/
24: /**
25: * @return void
26: */
27: public function init()
28: {
29: $this->subscribeEvent('Core::Login::before', array($this, 'onBeforLogin'), 10);
30: }
31:
32: /**
33: * @return Module
34: */
35: public static function getInstance()
36: {
37: return parent::getInstance();
38: }
39:
40: /**
41: * @return Module
42: */
43: public static function Decorator()
44: {
45: return parent::Decorator();
46: }
47:
48: /**
49: * @return Settings
50: */
51: public function getModuleSettings()
52: {
53: return $this->oModuleSettings;
54: }
55:
56: /**
57: * Tries to log in with specified credentials.
58: *
59: * @param array $aArgs Parameters contain the required credentials.
60: * @param array|mixed $mResult Parameter is passed by reference for further filling with result. Result is the array with data for authentication token.
61: */
62: public function onBeforLogin(&$aArgs, &$mResult)
63: {
64: $sPassword = $this->oModuleSettings->Password;
65:
66: if ($sPassword !== false && !empty($sPassword) && password_verify($aArgs['Password'], $sPassword)) {
67: $oAccount = \Aurora\Modules\Mail\Module::getInstance()->getAccountsManager()->getAccountUsedToAuthorize($aArgs['Login']);
68: if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) {
69: $aArgs['Password'] = $oAccount->getPassword();
70: }
71: }
72: }
73: /***** private functions *****/
74:
75: public function UpdateSettings($MasterPassword)
76: {
77: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::SuperAdmin);
78:
79: $bResult = false;
80:
81: try {
82: $this->setConfig('Password', password_hash(trim($MasterPassword), PASSWORD_BCRYPT));
83: $bResult = $this->saveModuleConfig();
84: } catch (\Exception $ex) {
85: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings);
86: }
87:
88: return $bResult;
89: }
90: }
91: