2016-01-24 16:58:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once '../config/config-default.php';
|
|
|
|
require_once '../lib/database.php';
|
|
|
|
require_once '../lib/session.php';
|
|
|
|
|
|
|
|
$input = json_decode(file_get_contents('php://input'));
|
|
|
|
|
2016-01-29 15:52:23 +00:00
|
|
|
if(!isset($input->csrfToken) || $input->csrfToken !== $_SESSION['csrfToken']) {
|
|
|
|
echo "Permission denied!";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2016-01-24 16:58:04 +00:00
|
|
|
if(!isset($_SESSION['type']) || $_SESSION['type'] != "admin") {
|
|
|
|
echo "Permission denied!";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "addUser") {
|
|
|
|
$passwordHash = password_hash($input->password, PASSWORD_DEFAULT);
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
$db->beginTransaction();
|
2016-01-24 16:58:04 +00:00
|
|
|
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("INSERT INTO users(name,password,type) VALUES (:name,:password,:type)");
|
2017-01-07 10:18:39 +00:00
|
|
|
|
|
|
|
$stmt->bindValue(':name', $input->name, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':password', $passwordHash, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
2016-01-24 16:58:04 +00:00
|
|
|
$stmt->execute();
|
|
|
|
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("SELECT MAX(id) FROM users WHERE name=:name AND password=:password AND type=:type");
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':name', $input->name, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':password', $passwordHash, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
2016-01-24 16:58:04 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
$newUserId = $stmt->fetchColumn();
|
2016-01-24 16:58:04 +00:00
|
|
|
|
|
|
|
$db->commit();
|
|
|
|
|
|
|
|
$retval = Array();
|
|
|
|
$retval['newId'] = $newUserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "getUserData") {
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("SELECT name,type FROM users WHERE id=:id LIMIT 1");
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':id', $input->id, PDO::PARAM_INT);
|
2016-01-24 16:58:04 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindColumn('name', $userName);
|
|
|
|
$stmt->bindColumn('type', $userType);
|
|
|
|
$stmt->fetch(PDO::FETCH_BOUND);
|
2016-01-24 16:58:04 +00:00
|
|
|
|
|
|
|
$retval = Array();
|
|
|
|
$retval['name'] = $userName;
|
|
|
|
$retval['type'] = $userType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "saveUserChanges") {
|
|
|
|
if(isset($input->password)) {
|
|
|
|
$passwordHash = password_hash($input->password, PASSWORD_DEFAULT);
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("UPDATE users SET name=:name,password=:password,type=:type WHERE id=:id");
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':name', $input->name, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':password', $passwordHash, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':id', $input->id, PDO::PARAM_INT);
|
2016-01-24 16:58:04 +00:00
|
|
|
$stmt->execute();
|
|
|
|
} else {
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("UPDATE users SET name=:name,type=:type WHERE id=:id");
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':name', $input->name, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':id', $input->id, PDO::PARAM_INT);
|
2016-01-24 16:58:04 +00:00
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 19:23:20 +00:00
|
|
|
if(isset($input->action) && $input->action == "getPermissions") {
|
|
|
|
|
|
|
|
$stmt = $db->prepare("
|
|
|
|
SELECT D.id,D.name
|
|
|
|
FROM permissions P
|
|
|
|
JOIN domains D ON P.domain=D.id
|
2017-01-08 04:15:31 +00:00
|
|
|
WHERE P.userid=:user
|
2016-01-24 19:23:20 +00:00
|
|
|
");
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':user', $input->id, PDO::PARAM_INT);
|
2016-01-24 19:23:20 +00:00
|
|
|
$stmt->execute();
|
|
|
|
|
|
|
|
$retval = Array();
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
while($obj = $stmt->fetchObject()) {
|
2016-01-24 19:23:20 +00:00
|
|
|
$retval[] = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "removePermission") {
|
|
|
|
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("DELETE FROM permissions WHERE userid=:user AND domain=:domain");
|
2016-01-24 19:23:20 +00:00
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':user', $input->userId, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':domain', $input->domainId, PDO::PARAM_INT);
|
2016-01-24 19:23:20 +00:00
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "searchDomains" && isset($input->term)) {
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("SELECT id,name AS text FROM domains WHERE name LIKE :name AND id NOT IN(SELECT domain FROM permissions WHERE userid=:user)");
|
2016-01-24 19:23:20 +00:00
|
|
|
|
|
|
|
$searchTerm = "%" . $input->term . "%";
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':name', $searchTerm, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':user', $input->userId, PDO::PARAM_INT);
|
|
|
|
$stmt->execute();
|
2016-01-24 19:23:20 +00:00
|
|
|
|
|
|
|
$retval = Array();
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
while($obj = $stmt->fetchObject()) {
|
2016-01-24 19:23:20 +00:00
|
|
|
$retval[] = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($input->action) && $input->action == "addPermissions") {
|
2017-01-08 04:15:31 +00:00
|
|
|
$stmt = $db->prepare("INSERT INTO permissions(userid,domain) VALUES (:user,:domain)");
|
2016-01-24 19:23:20 +00:00
|
|
|
|
|
|
|
foreach($input->domains as $domain) {
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt->bindValue(':user', $input->userId, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':domain', $domain, PDO::PARAM_INT);
|
2016-01-24 19:23:20 +00:00
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 16:58:04 +00:00
|
|
|
if(isset($retval)) {
|
|
|
|
echo json_encode($retval);
|
|
|
|
} else {
|
|
|
|
echo "{}";
|
|
|
|
}
|