mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 13:35:54 +00:00
wip
This commit is contained in:
@@ -4,8 +4,10 @@ namespace App\Client;
|
||||
|
||||
use App\Client\Connections\ControlConnection;
|
||||
use App\Logger\CliRequestLogger;
|
||||
use Carbon\Carbon;
|
||||
use Ratchet\Client\WebSocket;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\PromiseInterface;
|
||||
use function Ratchet\Client\connect;
|
||||
|
||||
@@ -20,6 +22,9 @@ class Client
|
||||
/** @var CliRequestLogger */
|
||||
protected $logger;
|
||||
|
||||
/** @var int */
|
||||
protected $timeConnected = 0;
|
||||
|
||||
public static $subdomains = [];
|
||||
|
||||
public function __construct(LoopInterface $loop, Configuration $configuration, CliRequestLogger $logger)
|
||||
@@ -34,20 +39,18 @@ class Client
|
||||
$this->logger->info("Sharing http://{$sharedUrl}");
|
||||
|
||||
foreach ($subdomains as $subdomain) {
|
||||
$this->connectToServer($sharedUrl, $subdomain);
|
||||
$this->connectToServer($sharedUrl, $subdomain, config('expose.auth_token'));
|
||||
}
|
||||
}
|
||||
|
||||
public function connectToServer(string $sharedUrl, $subdomain): PromiseInterface
|
||||
public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''): PromiseInterface
|
||||
{
|
||||
$deferred = new \React\Promise\Deferred();
|
||||
$promise = $deferred->promise();
|
||||
|
||||
$token = config('expose.auth_token');
|
||||
|
||||
$wsProtocol = $this->configuration->port() === 443 ? "wss" : "ws";
|
||||
|
||||
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$token}", [], [
|
||||
connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [
|
||||
'X-Expose-Control' => 'enabled',
|
||||
], $this->loop)
|
||||
->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) {
|
||||
@@ -55,19 +58,32 @@ class Client
|
||||
|
||||
$connection->authenticate($sharedUrl, $subdomain);
|
||||
|
||||
$clientConnection->on('close', function() {
|
||||
$clientConnection->on('close', function() use ($deferred) {
|
||||
$this->logger->error('Connection to server closed.');
|
||||
exit(1);
|
||||
|
||||
$this->exit($deferred);
|
||||
});
|
||||
|
||||
$connection->on('authenticationFailed', function ($data) {
|
||||
$connection->on('authenticationFailed', function ($data) use ($deferred) {
|
||||
$this->logger->error("Authentication failed. Please check your authentication token and try again.");
|
||||
exit(1);
|
||||
|
||||
$this->exit($deferred);
|
||||
});
|
||||
|
||||
$connection->on('subdomainTaken', function ($data) {
|
||||
$connection->on('subdomainTaken', function ($data) use ($deferred) {
|
||||
$this->logger->error("The chosen subdomain \"{$data->data->subdomain}\" is already taken. Please choose a different subdomain.");
|
||||
exit(1);
|
||||
|
||||
$this->exit($deferred);
|
||||
});
|
||||
|
||||
$connection->on('setMaximumConnectionLength', function ($data) {
|
||||
$this->loop->addPeriodicTimer(1, function() use ($data) {
|
||||
$this->timeConnected++;
|
||||
|
||||
$carbon = Carbon::createFromFormat('s', str_pad($data->length * 60 - $this->timeConnected, 2, 0, STR_PAD_LEFT));
|
||||
|
||||
$this->logger->info('Remaining time: '.$carbon->format('H:i:s'));
|
||||
});
|
||||
});
|
||||
|
||||
$connection->on('authenticated', function ($data) use ($deferred) {
|
||||
@@ -89,11 +105,18 @@ class Client
|
||||
$this->logger->error("Could not connect to the server.");
|
||||
$this->logger->error($e->getMessage());
|
||||
|
||||
$deferred->reject();
|
||||
|
||||
exit(1);
|
||||
$this->exit($deferred);
|
||||
});
|
||||
|
||||
return $promise;
|
||||
}
|
||||
|
||||
protected function exit(Deferred $deferred)
|
||||
{
|
||||
$deferred->reject();
|
||||
|
||||
$this->loop->futureTick(function(){
|
||||
exit(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ interface ConnectionManager
|
||||
{
|
||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection;
|
||||
|
||||
public function limitConnectionLength(ControlConnection $connection, int $maximumConnectionLength);
|
||||
|
||||
public function storeHttpConnection(ConnectionInterface $httpConnection, $requestId): HttpConnection;
|
||||
|
||||
public function getHttpConnectionForRequestId(string $requestId): ?HttpConnection;
|
||||
|
||||
18
app/Contracts/UserRepository.php
Normal file
18
app/Contracts/UserRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
use React\Promise\PromiseInterface;
|
||||
|
||||
interface UserRepository
|
||||
{
|
||||
public function getUsers(): PromiseInterface;
|
||||
|
||||
public function getUserById($id): PromiseInterface;
|
||||
|
||||
public function getUserByToken(string $authToken): PromiseInterface;
|
||||
|
||||
public function storeUser(array $data): PromiseInterface;
|
||||
|
||||
public function deleteUser($id): PromiseInterface;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Server\Connections;
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
class ConnectionManager implements ConnectionManagerContract
|
||||
{
|
||||
@@ -17,9 +18,26 @@ class ConnectionManager implements ConnectionManagerContract
|
||||
/** @var SubdomainGenerator */
|
||||
protected $subdomainGenerator;
|
||||
|
||||
public function __construct(SubdomainGenerator $subdomainGenerator)
|
||||
/** @var LoopInterface */
|
||||
protected $loop;
|
||||
|
||||
public function __construct(SubdomainGenerator $subdomainGenerator, LoopInterface $loop)
|
||||
{
|
||||
$this->subdomainGenerator = $subdomainGenerator;
|
||||
$this->loop = $loop;
|
||||
}
|
||||
|
||||
public function limitConnectionLength(ControlConnection $connection, int $maximumConnectionLength)
|
||||
{
|
||||
if ($maximumConnectionLength === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$connection->setMaximumConnectionLength($maximumConnectionLength);
|
||||
|
||||
$this->loop->addTimer($maximumConnectionLength * 60, function() use ($connection) {
|
||||
$connection->socket->close();
|
||||
});
|
||||
}
|
||||
|
||||
public function storeConnection(string $host, ?string $subdomain, ConnectionInterface $connection): ControlConnection
|
||||
|
||||
@@ -33,6 +33,14 @@ class ControlConnection
|
||||
$this->shared_at = now()->toDateTimeString();
|
||||
}
|
||||
|
||||
public function setMaximumConnectionLength(int $maximumConnectionLength)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
'event' => 'setMaximumConnectionLength',
|
||||
'length' => $maximumConnectionLength,
|
||||
]));
|
||||
}
|
||||
|
||||
public function registerProxy($requestId)
|
||||
{
|
||||
$this->socket->send(json_encode([
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Server;
|
||||
|
||||
use App\Contracts\ConnectionManager as ConnectionManagerContract;
|
||||
use App\Contracts\SubdomainGenerator;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Server as HttpServer;
|
||||
use App\Server\Connections\ConnectionManager;
|
||||
use App\Server\Http\Controllers\Admin\DeleteUsersController;
|
||||
@@ -117,7 +118,7 @@ class Factory
|
||||
$this->router->get('/settings', ShowSettingsController::class, $adminCondition);
|
||||
$this->router->post('/settings', SaveSettingsController::class, $adminCondition);
|
||||
$this->router->post('/users', StoreUsersController::class, $adminCondition);
|
||||
$this->router->delete('/users/delete/{id}', DeleteUsersController::class, $adminCondition);
|
||||
$this->router->delete('/users/{id}', DeleteUsersController::class, $adminCondition);
|
||||
$this->router->get('/sites', ListSitesController::class, $adminCondition);
|
||||
}
|
||||
|
||||
@@ -133,7 +134,7 @@ class Factory
|
||||
protected function bindSubdomainGenerator()
|
||||
{
|
||||
app()->singleton(SubdomainGenerator::class, function ($app) {
|
||||
return $app->make(RandomSubdomainGenerator::class);
|
||||
return $app->make(config('expose.admin.subdomain_generator'));
|
||||
});
|
||||
|
||||
return $this;
|
||||
@@ -154,6 +155,7 @@ class Factory
|
||||
|
||||
$this->bindConfiguration()
|
||||
->bindSubdomainGenerator()
|
||||
->bindUserRepository()
|
||||
->bindDatabase()
|
||||
->ensureDatabaseIsInitialized()
|
||||
->bindConnectionManager()
|
||||
@@ -181,6 +183,15 @@ class Factory
|
||||
return $this->socket;
|
||||
}
|
||||
|
||||
protected function bindUserRepository()
|
||||
{
|
||||
app()->singleton(UserRepository::class, function() {
|
||||
return app(config('expose.admin.user_repository'));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function bindDatabase()
|
||||
{
|
||||
app()->singleton(DatabaseInterface::class, function() {
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -19,20 +18,20 @@ class DeleteUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(DatabaseInterface $database)
|
||||
public function __construct(UserRepository $userRepository)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
{
|
||||
$this->database->query("DELETE FROM users WHERE id = :id", ['id' => $request->id])
|
||||
->then(function (Result $result) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['deleted' => true], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
$this->userRepository->deleteUser($request->get('id'))
|
||||
->then(function() use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['deleted' => true], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,6 @@ namespace App\Server\Http\Controllers\Admin;
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Server\Configuration;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -17,26 +17,24 @@ class ListUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(DatabaseInterface $database)
|
||||
public function __construct(UserRepository $userRepository)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
{
|
||||
$this->database->query('SELECT * FROM users ORDER by created_at DESC')->then(function (Result $result) use ($httpConnection) {
|
||||
$httpConnection->send(
|
||||
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $result->rows]))
|
||||
);
|
||||
$this->userRepository
|
||||
->getUsers()
|
||||
->then(function ($users) use ($httpConnection) {
|
||||
$httpConnection->send(
|
||||
respond_html($this->getView($httpConnection, 'server.users.index', ['users' => $users]))
|
||||
);
|
||||
|
||||
$httpConnection->close();
|
||||
}, function (\Exception $exception) use ($httpConnection) {
|
||||
$httpConnection->send(respond_html('Something went wrong: '.$exception->getMessage(), 500));
|
||||
|
||||
$httpConnection->close();
|
||||
});
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Server\Http\Controllers\Admin;
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Server\Configuration;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Server\Configuration;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Server\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -19,12 +19,12 @@ class StoreUsersController extends AdminController
|
||||
{
|
||||
protected $keepConnectionOpen = true;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(DatabaseInterface $database)
|
||||
public function __construct(UserRepository $userRepository)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
public function handle(Request $request, ConnectionInterface $httpConnection)
|
||||
@@ -47,16 +47,11 @@ class StoreUsersController extends AdminController
|
||||
'auth_token' => (string)Str::uuid()
|
||||
];
|
||||
|
||||
$this->database->query("
|
||||
INSERT INTO users (name, auth_token, created_at)
|
||||
VALUES (:name, :auth_token, DATETIME('now'))
|
||||
", $insertData)
|
||||
->then(function (Result $result) use ($httpConnection) {
|
||||
$this->database->query("SELECT * FROM users WHERE id = :id", ['id' => $result->insertId])
|
||||
->then(function (Result $result) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['user' => $result->rows[0]], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
});
|
||||
$this->userRepository
|
||||
->storeUser($insertData)
|
||||
->then(function ($user) use ($httpConnection) {
|
||||
$httpConnection->send(respond_json(['user' => $user], 200));
|
||||
$httpConnection->close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
namespace App\Server\Http\Controllers;
|
||||
|
||||
use App\Contracts\ConnectionManager;
|
||||
use App\Contracts\UserRepository;
|
||||
use App\Http\QueryParameters;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use Ratchet\WebSocket\MessageComponentInterface;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\FulfilledPromise;
|
||||
use React\Promise\PromiseInterface;
|
||||
use stdClass;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
@@ -16,13 +18,13 @@ class ControlMessageController implements MessageComponentInterface
|
||||
/** @var ConnectionManager */
|
||||
protected $connectionManager;
|
||||
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
/** @var UserRepository */
|
||||
protected $userRepository;
|
||||
|
||||
public function __construct(ConnectionManager $connectionManager, DatabaseInterface $database)
|
||||
public function __construct(ConnectionManager $connectionManager, UserRepository $userRepository)
|
||||
{
|
||||
$this->connectionManager = $connectionManager;
|
||||
$this->database = $database;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,21 +77,28 @@ class ControlMessageController implements MessageComponentInterface
|
||||
|
||||
protected function authenticate(ConnectionInterface $connection, $data)
|
||||
{
|
||||
if (config('expose.admin.validate_auth_tokens') === true) {
|
||||
$this->verifyAuthToken($connection);
|
||||
}
|
||||
$this->verifyAuthToken($connection)
|
||||
->then(function () use ($connection, $data) {
|
||||
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->hasValidSubdomain($connection, $data->subdomain)) {
|
||||
return;
|
||||
}
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
|
||||
$connectionInfo = $this->connectionManager->storeConnection($data->host, $data->subdomain, $connection);
|
||||
$this->connectionManager->limitConnectionLength($connectionInfo, config('expose.admin.maximum_session_length'));
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticated',
|
||||
'subdomain' => $connectionInfo->subdomain,
|
||||
'client_id' => $connectionInfo->client_id
|
||||
]));
|
||||
}, function () use ($connection) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
'data' => []
|
||||
]));
|
||||
$connection->close();
|
||||
});
|
||||
}
|
||||
|
||||
protected function registerProxy(ConnectionInterface $connection, $data)
|
||||
@@ -111,28 +120,34 @@ class ControlMessageController implements MessageComponentInterface
|
||||
//
|
||||
}
|
||||
|
||||
protected function verifyAuthToken(ConnectionInterface $connection)
|
||||
protected function verifyAuthToken(ConnectionInterface $connection): PromiseInterface
|
||||
{
|
||||
if (config('expose.admin.validate_auth_tokens') !== true) {
|
||||
return new FulfilledPromise();
|
||||
}
|
||||
|
||||
$deferred = new Deferred();
|
||||
|
||||
$authToken = QueryParameters::create($connection->httpRequest)->get('authToken');
|
||||
|
||||
$this->database
|
||||
->query("SELECT * FROM users WHERE auth_token = :token", ['token' => $authToken])
|
||||
->then(function (Result $result) use ($connection) {
|
||||
if (count($result->rows) === 0) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'authenticationFailed',
|
||||
'data' => []
|
||||
]));
|
||||
$connection->close();
|
||||
$this->userRepository
|
||||
->getUserByToken($authToken)
|
||||
->then(function ($user) use ($connection, $deferred) {
|
||||
if (is_null($user)) {
|
||||
$deferred->reject();
|
||||
} else {
|
||||
$deferred->resolve($user);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
protected function hasValidSubdomain(ConnectionInterface $connection, ?string $subdomain): bool
|
||||
{
|
||||
if (! is_null($subdomain)) {
|
||||
if (!is_null($subdomain)) {
|
||||
$controlConnection = $this->connectionManager->findControlConnectionForSubdomain($subdomain);
|
||||
if (! is_null($controlConnection) || $subdomain === config('expose.admin.subdomain')) {
|
||||
if (!is_null($controlConnection) || $subdomain === config('expose.admin.subdomain')) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'subdomainTaken',
|
||||
'data' => [
|
||||
|
||||
89
app/Server/UserRepository/DatabaseUserRepository.php
Normal file
89
app/Server/UserRepository/DatabaseUserRepository.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Server\UserRepository;
|
||||
|
||||
use App\Contracts\UserRepository;
|
||||
use Clue\React\SQLite\DatabaseInterface;
|
||||
use Clue\React\SQLite\Result;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\PromiseInterface;
|
||||
|
||||
class DatabaseUserRepository implements UserRepository
|
||||
{
|
||||
/** @var DatabaseInterface */
|
||||
protected $database;
|
||||
|
||||
public function __construct(DatabaseInterface $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
public function getUsers(): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query("SELECT * FROM users ORDER by created_at DESC")
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getUserById($id): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query("SELECT * FROM users WHERE id = :id", ['id' => $id])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0] ?? null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function getUserByToken(string $authToken): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database
|
||||
->query("SELECT * FROM users WHERE auth_token = :token", ['token' => $authToken])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0] ?? null);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function storeUser(array $data): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database->query("
|
||||
INSERT INTO users (name, auth_token, created_at)
|
||||
VALUES (:name, :auth_token, DATETIME('now'))
|
||||
", $data)
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$this->database->query("SELECT * FROM users WHERE id = :id", ['id' => $result->insertId])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result->rows[0]);
|
||||
});
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
public function deleteUser($id): PromiseInterface
|
||||
{
|
||||
$deferred = new Deferred();
|
||||
|
||||
$this->database->query("DELETE FROM users WHERE id = :id", ['id' => $id])
|
||||
->then(function (Result $result) use ($deferred) {
|
||||
$deferred->resolve($result);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user