web-base/Core/API/GroupsAPI.class.php

174 lines
5.0 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2020-06-20 20:13:51 +02:00
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Condition\Compare;
use Core\Objects\Context;
2020-06-24 01:09:08 +02:00
2020-07-01 21:10:25 +02:00
abstract class GroupsAPI extends Request {
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
}
protected function groupExists($name): bool {
$sql = $this->context->getSQL();
2020-06-24 01:09:08 +02:00
$res = $sql->select($sql->count())
->from("Group")
->where(new Compare("name", $name))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
return $this->success && $res[0]["count"] > 0;
}
2020-06-20 20:13:51 +02:00
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Groups {
2020-06-20 20:13:51 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\GroupsAPI;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\Objects\Context;
2022-11-20 17:13:53 +01:00
use Core\Objects\DatabaseEntity\Controller\NMRelation;
2022-11-18 18:06:46 +01:00
use Core\Objects\DatabaseEntity\Group;
2020-06-20 20:13:51 +02:00
class Fetch extends GroupsAPI {
private int $groupCount;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1),
'count' => new Parameter('count', Parameter::TYPE_INT, true, 20)
2020-06-20 20:13:51 +02:00
));
$this->groupCount = 0;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-20 20:13:51 +02:00
$page = $this->getParam("page");
if($page < 1) {
return $this->createError("Invalid page count");
}
$count = $this->getParam("count");
if($count < 1 || $count > 50) {
return $this->createError("Invalid fetch count");
}
2022-11-20 17:13:53 +01:00
$sql = $this->context->getSQL();
$groupCount = Group::count($sql);
if ($groupCount === false) {
return $this->createError("Error fetching group count: " . $sql->getLastError());
2020-06-20 20:13:51 +02:00
}
2022-11-20 17:13:53 +01:00
$groups = Group::findBy(Group::createBuilder($sql, false)
->orderBy("id")
2020-06-20 20:13:51 +02:00
->ascending()
->limit($count)
2022-11-20 17:13:53 +01:00
->offset(($page - 1) * $count));
2020-06-20 20:13:51 +02:00
2022-11-20 17:13:53 +01:00
if ($groups !== false) {
$this->result["groups"] = [];
$this->result["pageCount"] = intval(ceil($this->groupCount / $count));
2020-06-20 20:13:51 +02:00
$this->result["totalCount"] = $this->groupCount;
2022-11-20 17:13:53 +01:00
foreach ($groups as $groupId => $group) {
$this->result["groups"][$groupId] = $group->jsonSerialize();
$this->result["groups"][$groupId]["memberCount"] = 0;
}
$nmTable = NMRelation::buildTableName("User", "Group");
$res = $sql->select("group_id", $sql->count("user_id"))
->from($nmTable)
->groupBy("group_id")
->execute();
if (is_array($res)) {
foreach ($res as $row) {
list ($groupId, $memberCount) = [$row["group_id"], $row["user_id_count"]];
if (isset($this->result["groups"][$groupId])) {
$this->result["groups"][$groupId]["memberCount"] = $memberCount;
}
}
}
2020-06-20 20:13:51 +02:00
}
return $this->success;
}
}
2020-06-24 01:09:08 +02:00
class Create extends GroupsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-06-24 01:09:08 +02:00
'name' => new StringType('name', 32),
'color' => new StringType('color', 10),
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-24 01:09:08 +02:00
$name = $this->getParam("name");
if (preg_match("/^[a-zA-Z][a-zA-Z0-9_-]*$/", $name) !== 1) {
return $this->createError("Invalid name");
}
$color = $this->getParam("color");
if (preg_match("/^#[a-fA-F0-9]{3,6}$/", $color) !== 1) {
return $this->createError("Invalid color");
}
$exists = $this->groupExists($name);
if (!$this->success) {
return false;
} else if ($exists) {
return $this->createError("A group with this name already exists");
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-24 01:09:08 +02:00
2022-06-20 19:52:31 +02:00
$group = new Group();
$group->name = $name;
$group->color = $color;
$this->success = ($group->save($sql) !== FALSE);
2020-06-24 01:09:08 +02:00
$this->lastError = $sql->getLastError();
if ($this->success) {
2022-06-20 19:52:31 +02:00
$this->result["id"] = $group->getId();
2020-06-24 01:09:08 +02:00
}
return $this->success;
}
}
2020-06-24 01:23:37 +02:00
class Delete extends GroupsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
'id' => new Parameter('id', Parameter::TYPE_INT)
2020-06-24 01:23:37 +02:00
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$id = $this->getParam("id");
2022-11-20 17:13:53 +01:00
if (in_array($id, array_keys(Group::GROUPS))) {
2020-06-24 16:09:04 +02:00
return $this->createError("You cannot delete a default group.");
}
2020-06-24 01:23:37 +02:00
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$group = Group::find($sql, $id);
2020-06-24 01:23:37 +02:00
2022-06-20 19:52:31 +02:00
$this->success = ($group !== FALSE);
2020-06-24 01:23:37 +02:00
$this->lastError = $sql->getLastError();
2022-06-20 19:52:31 +02:00
if ($this->success && $group === null) {
2020-06-24 01:23:37 +02:00
return $this->createError("This group does not exist.");
}
2022-06-20 19:52:31 +02:00
$this->success = ($group->delete($sql) !== FALSE);
2020-06-24 01:23:37 +02:00
$this->lastError = $sql->getLastError();
return $this->success;
}
}
2020-06-20 20:13:51 +02:00
}