2020-06-20 20:13:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
|
2020-06-24 01:09:08 +02:00
|
|
|
use Driver\SQL\Condition\Compare;
|
|
|
|
|
2020-06-20 20:13:51 +02:00
|
|
|
class GroupsAPI extends Request {
|
|
|
|
|
2020-06-24 01:09:08 +02:00
|
|
|
protected function groupExists($name) {
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Api\Groups {
|
|
|
|
|
|
|
|
use Api\GroupsAPI;
|
|
|
|
use Api\Parameter\Parameter;
|
2020-06-24 01:09:08 +02:00
|
|
|
use Api\Parameter\StringType;
|
2020-06-24 01:23:37 +02:00
|
|
|
use Driver\SQL\Condition\Compare;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
|
|
|
class Fetch extends GroupsAPI {
|
|
|
|
|
|
|
|
private int $groupCount;
|
|
|
|
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
2020-06-23 15:31:09 +02:00
|
|
|
'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->loginRequired = true;
|
2020-06-23 15:31:09 +02:00
|
|
|
$this->requiredGroup = array(USER_GROUP_SUPPORT, USER_GROUP_ADMIN);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->groupCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getGroupCount() {
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->select($sql->count())->from("Group")->execute();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
$this->groupCount = $res[0]["count"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($values = array()) {
|
|
|
|
if(!parent::execute($values)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = $this->getParam("page");
|
|
|
|
if($page < 1) {
|
|
|
|
return $this->createError("Invalid page count");
|
|
|
|
}
|
|
|
|
|
2020-06-23 15:31:09 +02:00
|
|
|
$count = $this->getParam("count");
|
|
|
|
if($count < 1 || $count > 50) {
|
|
|
|
return $this->createError("Invalid fetch count");
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:13:51 +02:00
|
|
|
if (!$this->getGroupCount()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
2020-06-23 15:31:09 +02:00
|
|
|
$res = $sql->select("Group.uid as groupId", "Group.name as groupName", "Group.color as groupColor", $sql->count("UserGroup.user_id"))
|
2020-06-20 20:13:51 +02:00
|
|
|
->from("Group")
|
2020-06-23 15:31:09 +02:00
|
|
|
->leftJoin("UserGroup", "UserGroup.group_id", "Group.uid")
|
2020-06-20 20:13:51 +02:00
|
|
|
->groupBy("Group.uid")
|
|
|
|
->orderBy("Group.uid")
|
|
|
|
->ascending()
|
2020-06-23 15:31:09 +02:00
|
|
|
->limit($count)
|
|
|
|
->offset(($page - 1) * $count)
|
2020-06-20 20:13:51 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if($this->success) {
|
|
|
|
$this->result["groups"] = array();
|
|
|
|
foreach($res as $row) {
|
|
|
|
$groupId = intval($row["groupId"]);
|
|
|
|
$groupName = $row["groupName"];
|
2020-06-23 15:31:09 +02:00
|
|
|
$groupColor = $row["groupColor"];
|
2020-06-20 20:13:51 +02:00
|
|
|
$memberCount = $row["usergroup_user_id_count"];
|
|
|
|
$this->result["groups"][$groupId] = array(
|
|
|
|
"name" => $groupName,
|
2020-06-23 15:31:09 +02:00
|
|
|
"memberCount" => $memberCount,
|
|
|
|
"color" => $groupColor,
|
2020-06-20 20:13:51 +02:00
|
|
|
);
|
|
|
|
}
|
2020-06-23 15:31:09 +02:00
|
|
|
$this->result["pageCount"] = intval(ceil($this->groupCount / $count));
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->result["totalCount"] = $this->groupCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 01:09:08 +02:00
|
|
|
class Create extends GroupsAPI {
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'name' => new StringType('name', 32),
|
|
|
|
'color' => new StringType('color', 10),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->requiredGroup = array(USER_GROUP_ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($values = array()) {
|
|
|
|
if (!parent::execute($values)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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");
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$res = $sql->insert("Group", array("name", "color"))
|
|
|
|
->addRow($name, $color)
|
|
|
|
->returning("uid")
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
$this->result["uid"] = $sql->getLastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 01:23:37 +02:00
|
|
|
|
|
|
|
class Delete extends GroupsAPI {
|
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
|
|
|
'uid' => new Parameter('uid', Parameter::TYPE_INT)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->loginRequired = true;
|
|
|
|
$this->requiredGroup = array(USER_GROUP_ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($values = array()) {
|
|
|
|
if (!parent::execute($values)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->getParam("uid");
|
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
|
|
|
|
$res = $sql->select($sql->count())
|
|
|
|
->from("Group")
|
|
|
|
->where(new Compare("uid", $id))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success && $res[0]["count"] === 0) {
|
|
|
|
return $this->createError("This group does not exist.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $sql->delete("Group")->where(new Compare("uid", $id))->execute();
|
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|