web-base/core/Api/PermissionAPI.class.php

209 lines
6.2 KiB
PHP
Raw Normal View History

2020-06-27 01:18:10 +02:00
<?php
namespace Api {
2020-07-01 21:10:25 +02:00
abstract class PermissionAPI extends Request {
2020-06-27 01:18:10 +02:00
protected function checkStaticPermission() {
if (!$this->user->isLoggedIn() || !$this->user->hasGroup(USER_GROUP_ADMIN)) {
return $this->createError("Permission denied.");
}
return true;
}
}
}
namespace Api\Permission {
use Api\Parameter\Parameter;
use Api\Parameter\StringType;
use Api\PermissionAPI;
2020-06-27 22:47:12 +02:00
use Driver\SQL\Column\Column;
2020-06-27 01:18:10 +02:00
use Driver\SQL\Condition\Compare;
2020-06-27 22:47:12 +02:00
use Driver\SQL\Condition\CondIn;
use Driver\SQL\Condition\CondNot;
use Driver\SQL\Strategy\UpdateStrategy;
2020-06-27 01:18:10 +02:00
use Objects\User;
class Check extends PermissionAPI {
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, array(
'method' => new StringType('method', 323)
));
$this->isPublic = false;
}
2021-04-02 21:58:06 +02:00
public function execute($values = array()): bool {
2020-06-27 01:18:10 +02:00
if (!parent::execute($values)) {
return false;
}
$method = $this->getParam("method");
$sql = $this->user->getSQL();
$res = $sql->select("groups")
->from("ApiPermission")
->where(new Compare("method", $method))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
if (empty($res)) {
return true;
}
$groups = json_decode($res[0]["groups"]);
if (empty($groups)) {
return true;
}
if (!$this->user->isLoggedIn() || empty(array_intersect($groups, array_keys($this->user->getGroups())))) {
2020-06-27 22:47:12 +02:00
header('HTTP 1.1 401 Unauthorized');
2020-06-27 01:18:10 +02:00
return $this->createError("Permission denied.");
}
}
return $this->success;
}
}
class Fetch extends PermissionAPI {
private array $groups;
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, array());
}
private function fetchGroups() {
$sql = $this->user->getSQL();
2020-06-27 22:47:12 +02:00
$res = $sql->select("uid", "name", "color")
2020-06-27 01:18:10 +02:00
->from("Group")
->orderBy("uid")
->ascending()
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->groups = array();
foreach($res as $row) {
$groupId = $row["uid"];
$groupName = $row["name"];
2020-06-27 22:47:12 +02:00
$groupColor = $row["color"];
$this->groups[$groupId] = array("name" => $groupName, "color" => $groupColor);
2020-06-27 01:18:10 +02:00
}
}
return $this->success;
}
2021-04-02 21:58:06 +02:00
public function execute($values = array()): bool {
2020-06-27 01:18:10 +02:00
if (!parent::execute($values)) {
return false;
}
if (!$this->fetchGroups()) {
return false;
}
$sql = $this->user->getSQL();
2020-06-27 22:47:12 +02:00
$res = $sql->select("method", "groups", "description")
2020-06-27 01:18:10 +02:00
->from("ApiPermission")
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$permissions = array();
foreach ($res as $row) {
$method = $row["method"];
2020-06-27 22:47:12 +02:00
$description = $row["description"];
2020-06-27 01:18:10 +02:00
$groups = json_decode($row["groups"]);
2020-06-27 22:47:12 +02:00
$permissions[] = array(
"method" => $method,
"groups" => $groups,
"description" => $description
);
2020-06-27 01:18:10 +02:00
}
$this->result["permissions"] = $permissions;
$this->result["groups"] = $this->groups;
}
return $this->success;
}
}
class Save extends PermissionAPI {
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, array(
'permissions' => new Parameter('permissions', Parameter::TYPE_ARRAY)
));
}
2021-04-02 21:58:06 +02:00
public function execute($values = array()): bool {
2020-06-27 01:18:10 +02:00
if (!parent::execute($values)) {
return false;
}
if (!$this->checkStaticPermission()) {
return false;
}
2020-06-27 22:47:12 +02:00
$permissions = $this->getParam("permissions");
$sql = $this->user->getSQL();
$methodParam = new StringType('method', 32);
$groupsParam = new Parameter('groups', Parameter::TYPE_ARRAY);
$updateQuery = $sql->insert("ApiPermission", array("method", "groups"))
->onDuplicateKeyStrategy(new UpdateStrategy(array("method"), array( "groups" => new Column("groups") )));
$insertedMethods = array();
foreach($permissions as $permission) {
if (!is_array($permission)) {
return $this->createError("Invalid data type found in parameter: permissions, expected: object");
} else if(!isset($permission["method"]) || !array_key_exists("groups", $permission)) {
return $this->createError("Invalid object found in parameter: permissions, expected keys 'method' and 'groups'");
} else if (!$methodParam->parseParam($permission["method"])) {
$expectedType = $methodParam->getTypeName();
return $this->createError("Invalid data type found for attribute 'method', expected: $expectedType");
} else if(!$groupsParam->parseParam($permission["groups"])) {
$expectedType = $groupsParam->getTypeName();
return $this->createError("Invalid data type found for attribute 'groups', expected: $expectedType");
} else if(empty(trim($methodParam->value))) {
return $this->createError("Method cannot be empty.");
} else {
$method = $methodParam->value;
$groups = $groupsParam->value;
$updateQuery->addRow($method, $groups);
$insertedMethods[] = $method;
}
}
if (!empty($permissions)) {
$res = $updateQuery->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
}
if ($this->success) {
$res = $sql->delete("ApiPermission")
->where(new Compare("description", "")) // only delete non default permissions
->where(new CondNot(new CondIn("method", $insertedMethods)))
->execute();
2020-06-27 01:18:10 +02:00
2020-06-27 22:47:12 +02:00
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
}
2020-06-27 01:18:10 +02:00
return $this->success;
}
}
}