Refactor PeerDatabaseRecord and SessionManager to handle ReservedUsernames and improve date handling
This commit is contained in:
parent
6a1aa6b353
commit
a92391445a
3 changed files with 35 additions and 14 deletions
|
@ -54,13 +54,14 @@
|
||||||
// TODO: Update this to support `host` peers
|
// TODO: Update this to support `host` peers
|
||||||
if($peer->isExternal())
|
if($peer->isExternal())
|
||||||
{
|
{
|
||||||
|
Logger::getLogger()->debug('Creating a session for an external peer');
|
||||||
$flags[] = SessionFlags::AUTHENTICATION_REQUIRED;
|
$flags[] = SessionFlags::AUTHENTICATION_REQUIRED;
|
||||||
$flags[] = SessionFlags::VER_AUTHENTICATION;
|
$flags[] = SessionFlags::VER_AUTHENTICATION;
|
||||||
}
|
}
|
||||||
else if($peer->isEnabled())
|
else if($peer->isEnabled())
|
||||||
{
|
{
|
||||||
|
Logger::getLogger()->debug('Creating a session for an enabled peer');
|
||||||
$flags[] = SessionFlags::AUTHENTICATION_REQUIRED;
|
$flags[] = SessionFlags::AUTHENTICATION_REQUIRED;
|
||||||
|
|
||||||
if(PasswordManager::usesPassword($peer->getUuid()))
|
if(PasswordManager::usesPassword($peer->getUuid()))
|
||||||
{
|
{
|
||||||
$flags[] = SessionFlags::VER_PASSWORD;
|
$flags[] = SessionFlags::VER_PASSWORD;
|
||||||
|
@ -73,6 +74,7 @@
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Logger::getLogger()->debug('Creating a session for a disabled peer');
|
||||||
$flags[] = SessionFlags::REGISTRATION_REQUIRED;
|
$flags[] = SessionFlags::REGISTRATION_REQUIRED;
|
||||||
|
|
||||||
if(Configuration::getRegistrationConfiguration()->isDisplayNameRequired())
|
if(Configuration::getRegistrationConfiguration()->isDisplayNameRequired())
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Socialbox\Classes\Configuration;
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Enums\Flags\PeerFlags;
|
use Socialbox\Enums\Flags\PeerFlags;
|
||||||
|
use Socialbox\Enums\ReservedUsernames;
|
||||||
use Socialbox\Interfaces\SerializableInterface;
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
use Socialbox\Objects\Standard\InformationFieldState;
|
use Socialbox\Objects\Standard\InformationFieldState;
|
||||||
use Socialbox\Objects\Standard\Peer;
|
use Socialbox\Objects\Standard\Peer;
|
||||||
|
@ -32,7 +33,15 @@
|
||||||
{
|
{
|
||||||
$this->uuid = $data['uuid'];
|
$this->uuid = $data['uuid'];
|
||||||
$this->username = $data['username'];
|
$this->username = $data['username'];
|
||||||
|
|
||||||
|
if($data['server'] === ReservedUsernames::HOST->value)
|
||||||
|
{
|
||||||
|
$this->server = Configuration::getInstanceConfiguration()->getDomain();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$this->server = $data['server'];
|
$this->server = $data['server'];
|
||||||
|
}
|
||||||
|
|
||||||
if($data['flags'])
|
if($data['flags'])
|
||||||
{
|
{
|
||||||
|
@ -54,9 +63,16 @@
|
||||||
$this->created = (new DateTime())->setTimestamp($data['created']);
|
$this->created = (new DateTime())->setTimestamp($data['created']);
|
||||||
}
|
}
|
||||||
elseif(is_string($data['created']))
|
elseif(is_string($data['created']))
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$this->created = new DateTime($data['created']);
|
$this->created = new DateTime($data['created']);
|
||||||
}
|
}
|
||||||
|
catch (\DateMalformedStringException $e)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidArgumentException("The created field must be a valid timestamp or date string.");
|
throw new InvalidArgumentException("The created field must be a valid timestamp or date string.");
|
||||||
|
@ -71,9 +87,16 @@
|
||||||
$this->updated = (new DateTime())->setTimestamp($data['updated']);
|
$this->updated = (new DateTime())->setTimestamp($data['updated']);
|
||||||
}
|
}
|
||||||
elseif(is_string($data['updated']))
|
elseif(is_string($data['updated']))
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$this->updated = new DateTime($data['updated']);
|
$this->updated = new DateTime($data['updated']);
|
||||||
}
|
}
|
||||||
|
catch (\DateMalformedStringException $e)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidArgumentException("The updated field must be a valid timestamp or date string.");
|
throw new InvalidArgumentException("The updated field must be a valid timestamp or date string.");
|
||||||
|
@ -192,7 +215,7 @@
|
||||||
*/
|
*/
|
||||||
public function isExternal(): bool
|
public function isExternal(): bool
|
||||||
{
|
{
|
||||||
return $this->username === 'host' || $this->server !== Configuration::getInstanceConfiguration()->getDomain();
|
return $this->username === ReservedUsernames::HOST->value || $this->server !== Configuration::getInstanceConfiguration()->getDomain();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -251,16 +251,12 @@
|
||||||
|
|
||||||
// If-clause for handling the host peer, host peers are always enabled unless the fist clause is true
|
// If-clause for handling the host peer, host peers are always enabled unless the fist clause is true
|
||||||
// in which case the host was blocked by this server.
|
// in which case the host was blocked by this server.
|
||||||
if($clientRequest->getIdentifyAs()->getUsername() === ReservedUsernames::HOST->value)
|
if($clientRequest->getIdentifyAs()->getUsername() === ReservedUsernames::HOST->value && $registeredPeer === null)
|
||||||
{
|
{
|
||||||
// If the host is not registered, register it
|
$peerUuid = RegisteredPeerManager::createPeer($clientRequest->getIdentifyAs());
|
||||||
if($registeredPeer === null)
|
|
||||||
{
|
|
||||||
$peerUuid = RegisteredPeerManager::createPeer(PeerAddress::fromAddress($clientRequest->getHeader(StandardHeaders::IDENTIFY_AS)));
|
|
||||||
RegisteredPeerManager::enablePeer($peerUuid);
|
RegisteredPeerManager::enablePeer($peerUuid);
|
||||||
$registeredPeer = RegisteredPeerManager::getPeer($peerUuid);
|
$registeredPeer = RegisteredPeerManager::getPeer($peerUuid);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if($registeredPeer === null)
|
if($registeredPeer === null)
|
||||||
{
|
{
|
||||||
|
@ -272,7 +268,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the peer if it is not already registered
|
// Register the peer if it is not already registered
|
||||||
$peerUuid = RegisteredPeerManager::createPeer(PeerAddress::fromAddress($clientRequest->getHeader(StandardHeaders::IDENTIFY_AS)));
|
$peerUuid = RegisteredPeerManager::createPeer($clientRequest->getIdentifyAs());
|
||||||
// Retrieve the peer object
|
// Retrieve the peer object
|
||||||
$registeredPeer = RegisteredPeerManager::getPeer($peerUuid);
|
$registeredPeer = RegisteredPeerManager::getPeer($peerUuid);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue