Compare commits
2 commits
f080775701
...
631689b489
Author | SHA1 | Date | |
---|---|---|---|
631689b489 | |||
db153453b5 |
2 changed files with 60 additions and 44 deletions
|
@ -30,7 +30,7 @@
|
||||||
*/
|
*/
|
||||||
public static function usernameExists(string $username): bool
|
public static function usernameExists(string $username): bool
|
||||||
{
|
{
|
||||||
Logger::getLogger()->verbose(sprintf("Checking if username %s already exists", $username));
|
Logger::getLogger()->debug(sprintf("Checking if username %s already exists", $username));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
*/
|
*/
|
||||||
public static function createPeer(PeerAddress $peerAddress, bool $enabled=false): string
|
public static function createPeer(PeerAddress $peerAddress, bool $enabled=false): string
|
||||||
{
|
{
|
||||||
Logger::getLogger()->verbose(sprintf("Registering peer %s", $peerAddress->getAddress()));
|
Logger::getLogger()->debug(sprintf("Registering peer %s", $peerAddress->getAddress()));
|
||||||
$uuid = Uuid::v4()->toRfc4122();
|
$uuid = Uuid::v4()->toRfc4122();
|
||||||
$server = $peerAddress->getDomain();
|
$server = $peerAddress->getDomain();
|
||||||
|
|
||||||
|
@ -88,23 +88,23 @@
|
||||||
* Deletes a peer from the database based on the given UUID or RegisteredPeerRecord.
|
* Deletes a peer from the database based on the given UUID or RegisteredPeerRecord.
|
||||||
* WARNING: This operation is cascading and will delete all associated data.
|
* WARNING: This operation is cascading and will delete all associated data.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer to be deleted.
|
* @param string|PeerDatabaseRecord $ppeerUuid The UUID or RegisteredPeerRecord instance representing the peer to be deleted.
|
||||||
* @return void
|
* @return void
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
*/
|
*/
|
||||||
public static function deletePeer(string|PeerDatabaseRecord $uuid): void
|
public static function deletePeer(string|PeerDatabaseRecord $ppeerUuid): void
|
||||||
{
|
{
|
||||||
if($uuid instanceof PeerDatabaseRecord)
|
if($ppeerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$uuid = $uuid->getUuid();
|
$ppeerUuid = $ppeerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Deleting peer %s", $uuid));
|
Logger::getLogger()->debug(sprintf("Deleting peer %s", $ppeerUuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('DELETE FROM peers WHERE uuid=?');
|
$statement = Database::getConnection()->prepare('DELETE FROM peers WHERE uuid=?');
|
||||||
$statement->bindParam(1, $uuid);
|
$statement->bindParam(1, $ppeerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
|
@ -116,30 +116,30 @@
|
||||||
/**
|
/**
|
||||||
* Retrieves a registered peer record based on the given unique identifier or RegisteredPeerRecord object.
|
* Retrieves a registered peer record based on the given unique identifier or RegisteredPeerRecord object.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $uuid The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
|
* @param string|PeerDatabaseRecord $peerUuid The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
|
||||||
* @return PeerDatabaseRecord Returns a RegisteredPeerRecord object containing the peer's information.
|
* @return PeerDatabaseRecord|null Returns a RegisteredPeerRecord object containing the peer's information. null if the peer does not exist.
|
||||||
* @throws DatabaseOperationException If there is an error during the database operation.
|
* @throws DatabaseOperationException If there is an error during the database operation.
|
||||||
*/
|
*/
|
||||||
public static function getPeer(string|PeerDatabaseRecord $uuid): PeerDatabaseRecord
|
public static function getPeer(string|PeerDatabaseRecord $peerUuid): ?PeerDatabaseRecord
|
||||||
{
|
{
|
||||||
if($uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$uuid = $uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Retrieving peer %s from the database", $uuid));
|
Logger::getLogger()->debug(sprintf("Retrieving peer %s from the database", $peerUuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE uuid=?');
|
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE uuid=?');
|
||||||
$statement->bindParam(1, $uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
|
|
||||||
$result = $statement->fetch(PDO::FETCH_ASSOC);
|
$result = $statement->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
if($result === false)
|
if($result === false)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException(sprintf("The requested peer '%s' does not exist", $uuid));
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PeerDatabaseRecord($result);
|
return new PeerDatabaseRecord($result);
|
||||||
|
@ -153,20 +153,20 @@
|
||||||
/**
|
/**
|
||||||
* Retrieves a peer record by the given username.
|
* Retrieves a peer record by the given username.
|
||||||
*
|
*
|
||||||
* @param PeerAddress $address The address of the peer to be retrieved.
|
* @param PeerAddress $oeerAddress The address of the peer to be retrieved.
|
||||||
* @return PeerDatabaseRecord|null The record of the peer associated with the given username.
|
* @return PeerDatabaseRecord|null The record of the peer associated with the given username.
|
||||||
* @throws DatabaseOperationException If there is an error while querying the database.
|
* @throws DatabaseOperationException If there is an error while querying the database.
|
||||||
*/
|
*/
|
||||||
public static function getPeerByAddress(PeerAddress $address): ?PeerDatabaseRecord
|
public static function getPeerByAddress(PeerAddress $oeerAddress): ?PeerDatabaseRecord
|
||||||
{
|
{
|
||||||
Logger::getLogger()->verbose(sprintf("Retrieving peer %s from the database", $address->getAddress()));
|
Logger::getLogger()->debug(sprintf("Retrieving peer %s from the database", $oeerAddress->getAddress()));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE username=:username AND server=:server');
|
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE username=:username AND server=:server');
|
||||||
$username = $address->getUsername();
|
$username = $oeerAddress->getUsername();
|
||||||
$statement->bindParam(':username', $username);
|
$statement->bindParam(':username', $username);
|
||||||
$server = $address->getDomain();
|
$server = $oeerAddress->getDomain();
|
||||||
|
|
||||||
// Convert to 'host' if the domain is the same as the server's host
|
// Convert to 'host' if the domain is the same as the server's host
|
||||||
if($server === Configuration::getInstanceConfiguration()->getDomain())
|
if($server === Configuration::getInstanceConfiguration()->getDomain())
|
||||||
|
@ -181,6 +181,7 @@
|
||||||
|
|
||||||
if($result === false)
|
if($result === false)
|
||||||
{
|
{
|
||||||
|
Logger::getLogger()->debug(sprintf("Peer %s not found in the database", $oeerAddress->getAddress()));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,23 +292,23 @@
|
||||||
/**
|
/**
|
||||||
* Enables a peer identified by the given UUID or RegisteredPeerRecord.
|
* Enables a peer identified by the given UUID or RegisteredPeerRecord.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer to be enabled.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer to be enabled.
|
||||||
* @return void
|
* @return void
|
||||||
* @throws DatabaseOperationException If there is an error while updating the database.
|
* @throws DatabaseOperationException If there is an error while updating the database.
|
||||||
*/
|
*/
|
||||||
public static function enablePeer(string|PeerDatabaseRecord $uuid): void
|
public static function enablePeer(string|PeerDatabaseRecord $peerUuid): void
|
||||||
{
|
{
|
||||||
if($uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$uuid = $uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Enabling peer %s", $uuid));
|
Logger::getLogger()->verbose(sprintf("Enabling peer %s", $peerUuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=1 WHERE uuid=?');
|
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=1 WHERE uuid=?');
|
||||||
$statement->bindParam(1, $uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
|
@ -319,23 +320,23 @@
|
||||||
/**
|
/**
|
||||||
* Disables the peer identified by the given UUID or RegisteredPeerRecord.
|
* Disables the peer identified by the given UUID or RegisteredPeerRecord.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer.
|
||||||
* @return void
|
* @return void
|
||||||
* @throws DatabaseOperationException If there is an error while updating the peer's status in the database.
|
* @throws DatabaseOperationException If there is an error while updating the peer's status in the database.
|
||||||
*/
|
*/
|
||||||
public static function disablePeer(string|PeerDatabaseRecord $uuid): void
|
public static function disablePeer(string|PeerDatabaseRecord $peerUuid): void
|
||||||
{
|
{
|
||||||
if($uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$uuid = $uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Disabling peer %s", $uuid));
|
Logger::getLogger()->verbose(sprintf("Disabling peer %s", $peerUuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=0 WHERE uuid=?');
|
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=0 WHERE uuid=?');
|
||||||
$statement->bindParam(1, $uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
|
@ -347,25 +348,30 @@
|
||||||
/**
|
/**
|
||||||
* Adds a specific flag to the peer identified by the given UUID or RegisteredPeerRecord.
|
* Adds a specific flag to the peer identified by the given UUID or RegisteredPeerRecord.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer.
|
||||||
* @param PeerFlags|array $flags The flag or array of flags to be added to the peer.
|
* @param PeerFlags|array $peerFlags The flag or array of flags to be added to the peer.
|
||||||
* @return void
|
* @return void
|
||||||
* @throws DatabaseOperationException If there is an error while updating the database.
|
* @throws DatabaseOperationException If there is an error while updating the database.
|
||||||
*/
|
*/
|
||||||
public static function addFlag(string|PeerDatabaseRecord $uuid, PeerFlags|array $flags): void
|
public static function addFlag(string|PeerDatabaseRecord $peerUuid, PeerFlags|array $peerFlags): void
|
||||||
{
|
{
|
||||||
if($uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$uuid = $uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Adding flag(s) %s to peer %s", implode(',', $flags), $uuid));
|
Logger::getLogger()->verbose(sprintf("Adding flag(s) %s to peer %s", implode(',', $peerFlags), $peerUuid));
|
||||||
|
|
||||||
|
$peer = self::getPeer($peerUuid);
|
||||||
|
if($peer === null)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Peer does not exist');
|
||||||
|
}
|
||||||
|
|
||||||
$peer = self::getPeer($uuid);
|
|
||||||
$existingFlags = $peer->getFlags();
|
$existingFlags = $peer->getFlags();
|
||||||
$flags = is_array($flags) ? $flags : [$flags];
|
$peerFlags = is_array($peerFlags) ? $peerFlags : [$peerFlags];
|
||||||
|
|
||||||
foreach($flags as $flag)
|
foreach($peerFlags as $flag)
|
||||||
{
|
{
|
||||||
if(!in_array($flag, $existingFlags))
|
if(!in_array($flag, $existingFlags))
|
||||||
{
|
{
|
||||||
|
@ -378,7 +384,7 @@
|
||||||
$implodedFlags = implode(',', array_map(fn($flag) => $flag->name, $existingFlags));
|
$implodedFlags = implode(',', array_map(fn($flag) => $flag->name, $existingFlags));
|
||||||
$statement = Database::getConnection()->prepare('UPDATE peers SET flags=? WHERE uuid=?');
|
$statement = Database::getConnection()->prepare('UPDATE peers SET flags=? WHERE uuid=?');
|
||||||
$statement->bindParam(1, $implodedFlags);
|
$statement->bindParam(1, $implodedFlags);
|
||||||
$statement->bindParam(2, $uuid);
|
$statement->bindParam(2, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
|
@ -402,6 +408,11 @@
|
||||||
$peer = self::getPeer($peer);
|
$peer = self::getPeer($peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($peer === null)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Peer does not exist');
|
||||||
|
}
|
||||||
|
|
||||||
Logger::getLogger()->verbose(sprintf("Removing flag %s from peer %s", $flag->value, $peer->getUuid()));
|
Logger::getLogger()->verbose(sprintf("Removing flag %s from peer %s", $flag->value, $peer->getUuid()));
|
||||||
|
|
||||||
if(!$peer->flagExists($flag))
|
if(!$peer->flagExists($flag))
|
||||||
|
|
|
@ -281,7 +281,12 @@
|
||||||
*/
|
*/
|
||||||
public function isExternal(): bool
|
public function isExternal(): bool
|
||||||
{
|
{
|
||||||
return RegisteredPeerManager::getPeer($this->peerUuid)->isExternal();
|
$peer = RegisteredPeerManager::getPeer($this->peerUuid);
|
||||||
|
if($peer === null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $peer->isExternal();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue