-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.php
More file actions
177 lines (154 loc) · 5.45 KB
/
Module.php
File metadata and controls
177 lines (154 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora\Modules\AdminAuth;
use Aurora\Modules\Core\Module as CoreModule;
/**
* This module adds ability to login to the admin panel as a Super Administrator.
*
* @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
* @license https://afterlogic.com/products/common-licensing Afterlogic Software License
* @copyright Copyright (c) 2023, Afterlogic Corp.
*
* @property Settings $oModuleSettings
*
* @package Modules
*/
class Module extends \Aurora\System\Module\AbstractModule
{
/**
* @return Module
*/
public static function getInstance()
{
return parent::getInstance();
}
/**
* @return Module
*/
public static function Decorator()
{
return parent::Decorator();
}
/**
* @return Settings
*/
public function getModuleSettings()
{
return $this->oModuleSettings;
}
/***** private functions *****/
/**
* @return void
*/
public function init()
{
$this->subscribeEvent('Login', array($this, 'onLogin'), 10);
$this->subscribeEvent('CheckAccountExists', array($this, 'onCheckAccountExists'));
$this->subscribeEvent('System::RunEntry::before', array($this, 'onBeforeRunEntry'));
$this->denyMethodCallByWebApi('Login');
}
public function LoginAsSuperadmin($Login, $Password)
{
\Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
$aAuthData = self::Decorator()->Login($Login, $Password);
$mResult = CoreModule::Decorator()->SetAuthDataAndGetAuthToken($aAuthData);
return $mResult;
}
public function Login($Login, $Password)
{
$sIp = \Aurora\System\Utils::getClientIp();
CoreModule::Decorator()->IsBlockedUser($Login, $sIp);
$mResult = false;
$oSettings = &\Aurora\System\Api::GetSettings();
if ($Login === $oSettings->AdminLogin) {
if ($this->isClientIpInWhitelist()) {
$sAdminPassword = $oSettings->AdminPassword;
$bCorrectEmptyPass = empty($Password) && empty($sAdminPassword);
$bCorrectPass = password_verify(trim($Password), $sAdminPassword);
if ($bCorrectEmptyPass || $bCorrectPass) {
$mResult = [
'token' => 'admin',
'id' => '-1'
];
}
}
}
if (!$mResult) {
CoreModule::Decorator()->BlockUser($Login, $sIp);
CoreModule::Decorator()->IsBlockedUser($Login, $sIp);
} else {
$oBlockedUser = CoreModule::Decorator()->GetBlockedUser($Login, $sIp);
if ($oBlockedUser) {
$oBlockedUser->delete();
}
}
return $mResult;
}
/**
* Checks if superadmin has specified login.
*
* @param array $aArgs
*
* @throws \Aurora\System\Exceptions\ApiException
*/
public function onCheckAccountExists($aArgs)
{
$oSettings = &\Aurora\System\Api::GetSettings();
if ($aArgs['Login'] === $oSettings->AdminLogin) {
throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccountExists);
}
}
/**
* Tries to log in with specified credentials.
*
* @param array $aArgs Parameters contain the required credentials.
* @param array|mixed $mResult Parameter is passed by reference for further filling with result. Result is the array with data for authentication token.
*/
public function onLogin(&$aArgs, &$mResult)
{
$bAllowLoginFromCoreModule = $this->oModuleSettings->AllowLoginFromCoreModule;
$oSettings = &\Aurora\System\Api::GetSettings();
if ($bAllowLoginFromCoreModule && $aArgs['Login'] === $oSettings->AdminLogin) {
$mResult = $this->Login($aArgs['Login'], $aArgs['Password']);
return true;
}
}
protected function isClientIpInWhitelist()
{
$mResult = true;
$aWhitelistIp = $this->oModuleSettings->SuperadminWhitelistIp;
$ip = \Aurora\System\Utils::getClientIp();
if (!empty($ip) && count($aWhitelistIp) > 0 && !in_array($ip, $aWhitelistIp)) {
$mResult = false;
}
return $mResult;
}
public function onBeforeRunEntry(&$aArgs, &$mResult)
{
$oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser();
if (
$oAuthenticatedUser instanceof \Aurora\Modules\Core\Models\User
&& $oAuthenticatedUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin
&& !$this->isClientIpInWhitelist()
) {
if (isset($aArgs['EntryName']) && strtolower($aArgs['EntryName']) === 'default') {
CoreModule::Decorator()->Logout();
} else {
throw new \Aurora\System\Exceptions\ApiException(
\Aurora\System\Notifications::AccessDenied,
null,
$this->i18N('ERROR_USER_ACCESS_DENIED'),
[],
$this
);
// aborting the RunEntry execution stack
return true;
}
}
}
/***** private functions *****/
}