| 1 |
<?php |
| 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 |
namespace Liuch\DmarcSrg; |
| 53 |
|
| 54 |
use Liuch\DmarcSrg\Users\User; |
| 55 |
use Liuch\DmarcSrg\Users\DbUser; |
| 56 |
use Liuch\DmarcSrg\Users\UserList; |
| 57 |
use Liuch\DmarcSrg\Domains\DomainList; |
| 58 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 59 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 60 |
use Liuch\DmarcSrg\Exception\ForbiddenException; |
| 61 |
|
| 62 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 63 |
|
| 64 |
if (Core::isJson()) { |
| 65 |
try { |
| 66 |
$core = Core::instance(); |
| 67 |
|
| 68 |
if (Core::requestMethod() == 'GET') { |
| 69 |
if (!isset($_GET['user'])) { |
| 70 |
|
| 71 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 72 |
|
| 73 |
$list = array_map(function ($user) { |
| 74 |
$ua = $user->toArray(); |
| 75 |
$ua['level'] = User::levelToString($ua['level']); |
| 76 |
return $ua; |
| 77 |
}, (new UserList())->getList()['users']); |
| 78 |
|
| 79 |
Core::sendJson([ |
| 80 |
'users' => $list, |
| 81 |
'more' => false |
| 82 |
]); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$uname = $_GET['user']; |
| 87 |
if (empty($uname)) { |
| 88 |
|
| 89 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 90 |
|
| 91 |
Core::sendJson([ |
| 92 |
'domains' => [ 'available' => (new DomainList(UserList::getUserByName('admin')))->names() ] |
| 93 |
]); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$core->auth()->isAllowed(User::LEVEL_USER); |
| 98 |
if ($core->getCurrentUser()->name() === $uname && $core->getCurrentUser()->level() < User::LEVEL_ADMIN) { |
| 99 |
|
| 100 |
$udata = (new DbUser($uname))->toArray(); |
| 101 |
Core::sendJson([ |
| 102 |
'name' => $udata['name'], |
| 103 |
'level' => User::levelToString($udata['level']), |
| 104 |
'password' => $udata['password'] |
| 105 |
]); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 111 |
|
| 112 |
$user = new DbUser($uname); |
| 113 |
$res = $user->toArray(); |
| 114 |
$res['level'] = User::levelToString($res['level']); |
| 115 |
$res['domains'] = [ |
| 116 |
'available' => (new DomainList(UserList::getUserByName('admin')))->names(), |
| 117 |
'assigned' => (new DomainList($user))->names() |
| 118 |
]; |
| 119 |
Core::sendJson($res); |
| 120 |
return; |
| 121 |
} elseif (Core::requestMethod() == 'POST') { |
| 122 |
$data = Core::getJsonData(); |
| 123 |
if ($data) { |
| 124 |
$uname = $data['name'] ?? null; |
| 125 |
$action = $data['action'] ?? ''; |
| 126 |
if ($action === 'set_password') { |
| 127 |
$core->auth()->isAllowed(User::LEVEL_USER); |
| 128 |
|
| 129 |
$user = new DbUser($uname); |
| 130 |
$c_user = $core->getCurrentUser(); |
| 131 |
if ($c_user->name() === $user->name()) { |
| 132 |
if (!$c_user->verifyPassword($data['password'] ?? '')) { |
| 133 |
throw new SoftException('The current password is incorrect'); |
| 134 |
} |
| 135 |
} else { |
| 136 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 137 |
if ($c_user->level() <= $user->level()) { |
| 138 |
throw new ForbiddenException('Forbidden'); |
| 139 |
} |
| 140 |
} |
| 141 |
if (empty($data['new_password'])) { |
| 142 |
throw new SoftException('New password must not be empty'); |
| 143 |
} |
| 144 |
$user->setPassword($data['new_password']); |
| 145 |
Core::sendJson([ |
| 146 |
'error_code' => 0, |
| 147 |
'message' => 'The password has been successfully updated' |
| 148 |
]); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 153 |
|
| 154 |
if (!empty($data['level']) && gettype($data['level']) == 'string') { |
| 155 |
$data['level'] = User::stringToLevel($data['level']); |
| 156 |
} |
| 157 |
$user = new DbUser([ |
| 158 |
'name' => $uname, |
| 159 |
'level' => $data['level'] ?? null, |
| 160 |
'enabled' => $data['enabled'] ?? null |
| 161 |
]); |
| 162 |
$check_level = function () use ($core, $user, $action) { |
| 163 |
if ($core->getCurrentUser()->level() <= $user->level()) { |
| 164 |
throw new SoftException("Insufficient access level to {$action} this user"); |
| 165 |
} |
| 166 |
}; |
| 167 |
switch ($action) { |
| 168 |
case 'add': |
| 169 |
$check_level(); |
| 170 |
$user->ensure('nonexist'); |
| 171 |
$user->save(); |
| 172 |
break; |
| 173 |
case 'update': |
| 174 |
$check_level(); |
| 175 |
$user->ensure('exist'); |
| 176 |
$user->save(); |
| 177 |
break; |
| 178 |
case 'delete': |
| 179 |
$check_level(); |
| 180 |
$user->delete(); |
| 181 |
$user = null; |
| 182 |
break; |
| 183 |
default: |
| 184 |
throw new SoftException( |
| 185 |
'Unknown action. Valid values are "add", "update", "delete", "set_password".' |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
$domains = $data['domains'] ?? null; |
| 190 |
if ($user && is_array($domains)) { |
| 191 |
$domains = array_values(array_unique(array_filter($domains, function ($val) { |
| 192 |
return is_string($val); |
| 193 |
}))); |
| 194 |
$user->assignDomains($domains); |
| 195 |
} |
| 196 |
|
| 197 |
$res = [ |
| 198 |
'error_code' => 0, |
| 199 |
'message' => 'Successfully' |
| 200 |
]; |
| 201 |
if (isset($user)) { |
| 202 |
$ua = $user->toArray(); |
| 203 |
$ua['level'] = User::levelToString($ua['level']); |
| 204 |
$res['user'] = $ua; |
| 205 |
} |
| 206 |
Core::sendJson($res); |
| 207 |
return; |
| 208 |
} |
| 209 |
} |
| 210 |
} catch (RuntimeException $e) { |
| 211 |
Core::sendJson(ErrorHandler::exceptionResult($e)); |
| 212 |
return; |
| 213 |
} |
| 214 |
} elseif (Core::requestMethod() == 'GET') { |
| 215 |
Core::instance()->sendHtml(); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
Core::sendBad(); |