diff --git a/src/FederationServer/Classes/Enums/Method.php b/src/FederationServer/Classes/Enums/Method.php index 003d022..c6afba8 100644 --- a/src/FederationServer/Classes/Enums/Method.php +++ b/src/FederationServer/Classes/Enums/Method.php @@ -8,6 +8,9 @@ use FederationServer\Methods\Attachments\UploadAttachment; use FederationServer\Methods\Audit\ListAuditLogs; use FederationServer\Methods\Audit\ViewAuditEntry; + use FederationServer\Methods\Entities\GetEntity; + use FederationServer\Methods\Entities\ListEntities; + use FederationServer\Methods\Entities\QueryEntity; use FederationServer\Methods\Operators\CreateOperator; use FederationServer\Methods\Operators\DeleteOperator; use FederationServer\Methods\Operators\DisableOperator; @@ -37,7 +40,9 @@ case MANAGE_BLACKLIST_PERMISSION; case MANAGE_CLIENT_PERMISSION; + case GET_ENTITY; case LIST_ENTITIES; + case QUERY_ENTITY; case PUSH_ENTITY; case UPLOAD_ATTACHMENT; @@ -74,6 +79,12 @@ case self::LIST_ENTITIES: ListEntities::handleRequest(); break; + case self::QUERY_ENTITY: + QueryEntity::handleRequest(); + break; + case self::GET_ENTITY: + GetEntity::handleRequest(); + break; case self::PUSH_ENTITY: PushEntity::handleRequest(); break; @@ -134,6 +145,8 @@ $path === '/entities' && $requestMethod === 'GET' => Method::LIST_ENTITIES, $path === '/entities' && $requestMethod === 'POST' => Method::PUSH_ENTITY, + preg_match('#^/entities/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'GET' => Method::GET_ENTITY, + $path === '/entities/query' && $requestMethod === 'POST' => Method::QUERY_ENTITY, $path === '/operators' && $requestMethod === 'GET' => Method::LIST_OPERATORS, $path === '/operators' && $requestMethod === 'POST' => Method::CREATE_OPERATOR, diff --git a/src/FederationServer/Classes/Managers/EntitiesManager.php b/src/FederationServer/Classes/Managers/EntitiesManager.php index 4b36307..65c73e9 100644 --- a/src/FederationServer/Classes/Managers/EntitiesManager.php +++ b/src/FederationServer/Classes/Managers/EntitiesManager.php @@ -51,30 +51,44 @@ * Retrieves an entity by its ID and domain. * * @param string $id The ID of the entity. - * @param string $domain The domain of the entity. + * @param string|null $domain The domain of the entity. * @return EntityRecord|null The EntityRecord object if found, null otherwise. - * @throws InvalidArgumentException If the ID or domain is not provided or is invalid. * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. */ - public static function getEntityByDomain(string $id, string $domain): ?EntityRecord + public static function getEntity(string $id, ?string $domain): ?EntityRecord { - if(strlen($id) < 1 || strlen($domain) < 1) + if(strlen($id) < 1) { throw new InvalidArgumentException("Entity ID and domain must be provided."); } + if(!is_null($domain) && !filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) + { + throw new InvalidArgumentException("Invalid domain format."); + } + try { - $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM entities WHERE id = :id AND domain = :domain"); - $stmt->bindParam(':id', $id); - $stmt->bindParam(':domain', $domain); - $stmt->execute(); + if(is_null($domain)) + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM entities WHERE id = :id"); + $stmt->bindParam(':id', $id); + } + else + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM entities WHERE id = :id AND domain = :domain"); + $stmt->bindParam(':id', $id); + $stmt->bindParam(':domain', $domain); + } + $stmt->execute(); $data = $stmt->fetch(PDO::FETCH_ASSOC); + if($data) { return new EntityRecord($data); } + return null; } catch (PDOException $e) diff --git a/src/FederationServer/Methods/Entities/GetEntity.php b/src/FederationServer/Methods/Entities/GetEntity.php new file mode 100644 index 0000000..95b84c9 --- /dev/null +++ b/src/FederationServer/Methods/Entities/GetEntity.php @@ -0,0 +1,44 @@ +toArray()); + } + } + diff --git a/src/FederationServer/Methods/Entities/ListEntities.php b/src/FederationServer/Methods/Entities/ListEntities.php new file mode 100644 index 0000000..39f0518 --- /dev/null +++ b/src/FederationServer/Methods/Entities/ListEntities.php @@ -0,0 +1,44 @@ + $op->toArray(), $operators); + self::successResponse($result); + } + } + diff --git a/src/FederationServer/Methods/Entities/QueryEntity.php b/src/FederationServer/Methods/Entities/QueryEntity.php new file mode 100644 index 0000000..cb5c061 --- /dev/null +++ b/src/FederationServer/Methods/Entities/QueryEntity.php @@ -0,0 +1,44 @@ +toArray()); + } + } +