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
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Expression\Count;
|
2022-11-18 18:06:46 +01:00
|
|
|
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();
|
2023-01-05 22:47:17 +01:00
|
|
|
$res = $sql->select(new Count())
|
2020-06-24 01:09:08 +02:00
|
|
|
->from("Group")
|
2022-11-26 23:57:28 +01:00
|
|
|
->whereEq("name", $name)
|
2020-06-24 01:09:08 +02:00
|
|
|
->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;
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\API\Traits\Pagination;
|
|
|
|
use Core\Driver\SQL\Column\Column;
|
2023-01-09 14:21:11 +01:00
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Expression\Alias;
|
|
|
|
use Core\Driver\SQL\Expression\Count;
|
2023-01-09 14:21:11 +01:00
|
|
|
use Core\Driver\SQL\Join\InnerJoin;
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Driver\SQL\Query\Insert;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\Context;
|
|
|
|
use Core\Objects\DatabaseEntity\Group;
|
2023-01-09 14:21:11 +01:00
|
|
|
use Core\Objects\DatabaseEntity\User;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
|
|
|
class Fetch extends GroupsAPI {
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Pagination;
|
|
|
|
|
2020-06-20 20:13:51 +02:00
|
|
|
private int $groupCount;
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
2023-01-05 22:47:17 +01:00
|
|
|
parent::__construct($context, $externalCall,
|
|
|
|
self::getPaginationParameters(['id', 'name', 'member_count'])
|
|
|
|
);
|
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-23 15:31:09 +02:00
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
$sql = $this->context->getSQL();
|
2023-01-05 22:47:17 +01:00
|
|
|
if (!$this->initPagination($sql, Group::class)) {
|
|
|
|
return false;
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-10 22:12:05 +01:00
|
|
|
$nmTable = User::getHandler($sql)->getNMRelation("groups")->getTableName();
|
2023-01-05 22:47:17 +01:00
|
|
|
$memberCount = new Alias($sql->select(new Count())
|
2023-01-10 22:12:05 +01:00
|
|
|
->from($nmTable)
|
2023-01-05 22:47:17 +01:00
|
|
|
->whereEq("group_id", new Column("Group.id")), "memberCount");
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
$groupsQuery = $this->createPaginationQuery($sql, [$memberCount]);
|
|
|
|
$groups = $groupsQuery->execute();
|
|
|
|
if ($groups !== false && $groups !== null) {
|
2022-11-20 17:13:53 +01:00
|
|
|
$this->result["groups"] = [];
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
foreach ($groups as $group) {
|
|
|
|
$groupData = $group->jsonSerialize();
|
|
|
|
$groupData["memberCount"] = $group["memberCount"];
|
|
|
|
$this->result["groups"][] = $groupData;
|
2022-11-20 17:13:53 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
} else {
|
|
|
|
return $this->createError("Error fetching groups: " . $sql->getLastError());
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN, Group::SUPPORT], "Allows users to fetch available groups");
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
class Get extends GroupsAPI {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, [
|
|
|
|
"id" => new Parameter("id", Parameter::TYPE_INT)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$groupId = $this->getParam("id");
|
|
|
|
$group = Group::find($sql, $groupId);
|
|
|
|
if ($group === false) {
|
|
|
|
return $this->createError("Error fetching group: " . $sql->getLastError());
|
|
|
|
} else if ($group === null) {
|
|
|
|
return $this->createError("Group not found");
|
|
|
|
} else {
|
|
|
|
$this->result["group"] = $group->jsonSerialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN, Group::SUPPORT], "Allows users to get details about a group");
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-09 14:21:11 +01:00
|
|
|
class GetMembers extends GroupsAPI {
|
|
|
|
|
|
|
|
use Pagination;
|
|
|
|
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
$paginationParams = self::getPaginationParameters(["id", "name", "fullName"]);
|
|
|
|
$paginationParams["id"] = new Parameter("id", Parameter::TYPE_INT);
|
|
|
|
parent::__construct($context, $externalCall, $paginationParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$sql = $this->context->getSQL();
|
2023-01-10 22:12:05 +01:00
|
|
|
$nmTable = User::getHandler($sql)->getNMRelation("groups")->getTableName();
|
2023-01-09 14:21:11 +01:00
|
|
|
$condition = new Compare("group_id", $this->getParam("id"));
|
|
|
|
$nmJoin = new InnerJoin($nmTable, "$nmTable.user_id", "User.id");
|
|
|
|
if (!$this->initPagination($sql, User::class, $condition, 100, [$nmJoin])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userQuery = $this->createPaginationQuery($sql, null, [$nmJoin]);
|
|
|
|
$users = $userQuery->execute();
|
|
|
|
if ($users !== false && $users !== null) {
|
|
|
|
$this->result["members"] = [];
|
|
|
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->result["users"][] = $user->jsonSerialize(["id", "name", "fullName", "profilePicture"]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->createError("Error fetching group members: " . $sql->getLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN, Group::SUPPORT], "Allows users to fetch members of a group");
|
|
|
|
}
|
2023-01-09 14:21:11 +01:00
|
|
|
}
|
|
|
|
|
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-11-29 14:17:11 +01:00
|
|
|
$group = new Group(null, $name, $color);
|
2022-06-20 19:52:31 +02:00
|
|
|
$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;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to create a new group");
|
|
|
|
}
|
2020-06-24 01:09:08 +02:00
|
|
|
}
|
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);
|
2023-01-09 14:21:11 +01:00
|
|
|
if ($group === false) {
|
|
|
|
return $this->createError("Error fetching group: " . $sql->getLastError());
|
|
|
|
} else if ($group === null) {
|
|
|
|
return $this->createError("This group does not exist.");
|
|
|
|
} else {
|
|
|
|
$this->success = ($group->delete($sql) !== FALSE);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to delete a group");
|
|
|
|
}
|
2023-01-09 14:21:11 +01:00
|
|
|
}
|
2020-06-24 01:23:37 +02:00
|
|
|
|
2023-01-09 14:21:11 +01:00
|
|
|
class AddMember extends GroupsAPI {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, [
|
|
|
|
new Parameter("id", Parameter::TYPE_INT),
|
|
|
|
new Parameter("userId", Parameter::TYPE_INT)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$groupId = $this->getParam("id");
|
|
|
|
$userId = $this->getParam("userId");
|
|
|
|
$group = Group::find($sql, $groupId);
|
|
|
|
if ($group === false) {
|
|
|
|
return $this->createError("Error fetching group: " . $sql->getLastError());
|
|
|
|
} else if ($group === null) {
|
|
|
|
return $this->createError("This group does not exist.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::find($sql, $userId, true);
|
|
|
|
if ($user === false) {
|
|
|
|
return $this->createError("Error fetching user: " . $sql->getLastError());
|
|
|
|
} else if ($user === null) {
|
|
|
|
return $this->createError("This user does not exist.");
|
|
|
|
} else if (isset($user->getGroups()[$groupId])) {
|
|
|
|
return $this->createError("This user is already member of this group.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->groups[$groupId] = $group;
|
|
|
|
$this->success = $user->save($sql, ["groups"], true);
|
|
|
|
if (!$this->success) {
|
|
|
|
return $this->createError("Error saving user: " . $sql->getLastError());
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to add members to a group");
|
|
|
|
}
|
2023-01-09 14:21:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class RemoveMember extends GroupsAPI {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, [
|
|
|
|
new Parameter("id", Parameter::TYPE_INT),
|
|
|
|
new Parameter("userId", Parameter::TYPE_INT)
|
|
|
|
]);
|
|
|
|
}
|
2020-06-24 01:23:37 +02:00
|
|
|
|
2023-01-09 14:21:11 +01:00
|
|
|
protected function _execute(): bool {
|
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$groupId = $this->getParam("id");
|
|
|
|
$userId = $this->getParam("userId");
|
|
|
|
$group = Group::find($sql, $groupId);
|
|
|
|
if ($group === false) {
|
|
|
|
return $this->createError("Error fetching group: " . $sql->getLastError());
|
|
|
|
} else if ($group === null) {
|
2020-06-24 01:23:37 +02:00
|
|
|
return $this->createError("This group does not exist.");
|
|
|
|
}
|
|
|
|
|
2023-01-09 14:21:11 +01:00
|
|
|
$user = User::find($sql, $userId, true);
|
|
|
|
if ($user === false) {
|
|
|
|
return $this->createError("Error fetching user: " . $sql->getLastError());
|
|
|
|
} else if ($user === null) {
|
|
|
|
return $this->createError("This user does not exist.");
|
|
|
|
} else if (!isset($user->getGroups()[$groupId])) {
|
|
|
|
return $this->createError("This user is not member of this group.");
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($user->groups[$groupId]);
|
|
|
|
$this->success = $user->save($sql, ["groups"], true);
|
|
|
|
if (!$this->success) {
|
|
|
|
return $this->createError("Error saving user: " . $sql->getLastError());
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-24 01:23:37 +02:00
|
|
|
}
|
2023-01-09 14:21:11 +01:00
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to remove members from a group");
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|