ulogger-server/utils/handleuser.php

80 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
2017-04-07 00:05:28 +02:00
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2017-04-07 00:05:28 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
2017-04-09 23:35:55 +02:00
2020-02-20 17:08:47 +01:00
require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/utils.php");
2020-02-20 17:08:47 +01:00
$auth = new uAuth();
$config = uConfig::getInstance();
2017-08-25 13:59:19 +02:00
2020-02-20 17:08:47 +01:00
$action = uUtils::postString('action');
$login = uUtils::postString('login');
$pass = uUtils::postPass('pass');
$admin = uUtils::postBool('admin', false);
2019-02-25 10:04:09 +01:00
2020-02-20 17:08:47 +01:00
$lang = (new uLang($config))->getStrings();
2019-02-25 10:04:09 +01:00
2020-02-20 17:08:47 +01:00
if ($auth->user->login === $login || empty($action) || empty($login) || !$auth->isAuthenticated() || !$auth->isAdmin()) {
uUtils::exitWithError($lang["servererror"]);
}
2020-02-20 17:08:47 +01:00
if ($admin && !$auth->isAdmin()) {
uUtils::exitWithError($lang["notauthorized"]);
}
2017-04-12 20:16:39 +02:00
2020-02-20 17:08:47 +01:00
$aUser = new uUser($login);
$data = NULL;
2017-04-12 20:16:39 +02:00
2020-02-20 17:08:47 +01:00
switch ($action) {
case 'add':
if ($aUser->isValid) {
uUtils::exitWithError($lang["userexists"]);
}
if (empty($pass) || !$config->validPassStrength($pass) || ($userId = uUser::add($login, $pass, $admin)) === false) {
uUtils::exitWithError($lang["servererror"]);
} else {
$data = [ 'id' => $userId ];
}
break;
2017-04-12 20:16:39 +02:00
2020-02-20 17:08:47 +01:00
case 'update':
if ($aUser->setAdmin($admin) === false) {
uUtils::exitWithError($lang["servererror"]);
}
if (!empty($pass) && (!$config->validPassStrength($pass) || $aUser->setPass($pass) === false)) {
uUtils::exitWithError($lang["servererror"]);
}
break;
2017-04-12 20:16:39 +02:00
2020-02-20 17:08:47 +01:00
case 'delete':
if ($aUser->delete() === false) {
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($lang["servererror"]);
2020-02-20 17:08:47 +01:00
}
break;
default:
uUtils::exitWithError($lang["servererror"]);
break;
}
2017-04-12 20:16:39 +02:00
2020-02-20 17:08:47 +01:00
uUtils::exitWithSuccess($data);
?>