add basic api routes to get info on server, users and tactics

pull/97/head
Override-6 2 years ago committed by sam
parent a3e38bded1
commit ec69adab44

@ -33,6 +33,7 @@ function getAccountController(): APIAccountsController {
$con = new Connection(get_database());
$gw = new AccountGateway($con);
return new APIAccountsController(new AuthModel($gw), $gw);
}
function getServerController(): APIServerController {
@ -48,7 +49,6 @@ function getRoutes(): AltoRouter {
$router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize()));
$router->map("POST", "/tactic/[i:id]/edit/name", Action::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc)));
$router->map("POST", "/tactic/[i:id]/save", Action::auth(fn(int $id, Account $acc) => getTacticController()->saveContent($id, $acc)));
$router->map("GET", "/admin/list-users", Action::noAuth(fn() => getAccountController()->listUsers($_GET)));
$router->map("GET", "/admin/user/[i:id]", Action::noAuth(fn(int $id) => getAccountController()->getUser($id)));
$router->map("GET", "/admin/user/[i:id]/space", Action::noAuth(fn(int $id) => getTacticController()->getUserTactics($id)));
@ -57,6 +57,7 @@ function getRoutes(): AltoRouter {
$router->map("POST", "/admin/user/[i:id]/update", Action::noAuth(fn(int $id) => getAccountController()->updateUser($id)));
$router->map("GET", "/admin/server-info", Action::noAuth(fn() => getServerController()->getServerInfo()));
return $router;
}

@ -1,5 +1,9 @@
<?php
use IQBall\Core\Connection;
use IQBall\Core\Gateway\AccountGateway;
use IQBall\Core\Model\AuthModel;
/**
* @return PDO The PDO instance of the configuration's database connexion.
*/
@ -26,3 +30,16 @@ function get_database(): PDO {
return $pdo;
}
function init_database(PDO $pdo): void {
$accounts = new AccountGateway(new Connection($pdo));
$defaultAccounts = ["maxime", "mael", "yanis", "vivien"];
foreach ($defaultAccounts as $name) {
$email = "$name@mail.com";
$id = $accounts->insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT));
$accounts->setIsAdmin($id, true);
}
}

@ -17,7 +17,7 @@ CREATE TABLE Account
username varchar NOT NULL,
token varchar UNIQUE NOT NULL,
hash varchar NOT NULL,
profile_picture varchar NOT NULL
profile_picture varchar NOT NULL,
);
CREATE TABLE Tactic

@ -55,6 +55,7 @@ class API {
}
if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) {
return new JsonHttpResponse([ValidationFail::unauthorized()], HttpCodes::UNAUTHORIZED);
}
}

@ -2,6 +2,7 @@
namespace IQBall\Api\Controller;
use IQBall\Api\APIControl;
use IQBall\App\Control;
use IQBall\Core\Data\Account;
@ -25,6 +26,7 @@ class APIAccountsController {
public function __construct(AuthModel $model, AccountGateway $accounts) {
$this->accounts = $accounts;
$this->authModel = $model;
}
@ -47,6 +49,7 @@ class APIAccountsController {
});
}
/**
* @param int $userId
* @return HttpResponse given user information.
@ -106,3 +109,6 @@ class APIAccountsController {
});
}
}
}

@ -39,6 +39,6 @@ class APIAuthController {
}
return new JsonHttpResponse(["authorization" => $account->getToken()]);
});
}, true);
}
}

@ -6,6 +6,7 @@ use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Http\JsonHttpResponse;
class APIServerController {
private string $basePath;
private \PDO $pdo;
@ -43,3 +44,4 @@ class APIServerController {
}
}

@ -15,13 +15,15 @@ use IQBall\Core\Validation\DefaultValidators;
/**
* API endpoint related to tactics
*/
class APITacticController {
class APITacticController
{
private TacticModel $model;
/**
* @param TacticModel $model
*/
public function __construct(TacticModel $model) {
public function __construct(TacticModel $model)
{
$this->model = $model;
}
@ -31,7 +33,8 @@ class APITacticController {
* @param Account $account
* @return HttpResponse
*/
public function updateName(int $tactic_id, Account $account): HttpResponse {
public function updateName(int $tactic_id, Account $account): HttpResponse
{
return APIControl::runChecked([
"name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()],
], function (HttpRequest $request) use ($tactic_id, $account) {
@ -44,14 +47,15 @@ class APITacticController {
}
return HttpResponse::fromCode(HttpCodes::OK);
});
}, true);
}
/**
* @param int $id
* @return HttpResponse
*/
public function saveContent(int $id, Account $account): HttpResponse {
public function saveContent(int $id, Account $account): HttpResponse
{
return APIControl::runChecked([
"content" => [],
], function (HttpRequest $req) use ($id) {
@ -60,7 +64,7 @@ class APITacticController {
return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST);
}
return HttpResponse::fromCode(HttpCodes::OK);
});
}, true);
}
@ -68,7 +72,8 @@ class APITacticController {
* @param int $userId
* @return HttpResponse given user information.
*/
public function getUserTactics(int $userId): HttpResponse {
public function getUserTactics(int $userId): HttpResponse
{
$tactics = $this->model->listAllOf($userId);
$response = array_map(fn(TacticInfo $t) => [
@ -76,6 +81,7 @@ class APITacticController {
'name' => $t->getName(),
'court' => $t->getCourtType(),
'creation_date' => $t->getCreationDate(),
], $tactics);
return new JsonHttpResponse($response);

@ -12,7 +12,6 @@ use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
class App {
/**
@ -90,6 +89,7 @@ class App {
if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) {
return new JsonHttpResponse([ValidationFail::unauthorized()], HttpCodes::UNAUTHORIZED);
}
}
return $action->run($params, $session);

@ -15,6 +15,7 @@ class Control {
* @param array<string, Validator[]> $schema an array of `fieldName => DefaultValidators` which represents the request object schema
* @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema.
* The callback must accept an HttpRequest, and return an HttpResponse object.
<<<<<<< HEAD:src/Core/Control.php
* @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema
* @return HttpResponse
*/
@ -35,6 +36,7 @@ class Control {
* @param array<string, Validator[]> $schema an array of `fieldName => DefaultValidators` which represents the request object schema
* @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema.
* The callback must accept an HttpRequest, and return an HttpResponse object.
<<<<<<< HEAD:src/Core/Control.php
* @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema
* @return HttpResponse
*/

@ -24,7 +24,7 @@ class User implements \JsonSerializable {
private string $profilePicture;
/**
* @var bool isAdmin
* @var bool true if the user is an administrator
*/
private bool $isAdmin;

@ -47,7 +47,6 @@ class AccountGateway {
return !empty($result);
}
/**
* promote or demote a user to server administrator
* @param int $id
@ -60,6 +59,7 @@ class AccountGateway {
} else {
$stmnt = $this->con->prepare("DELETE FROM Admins WHERE id = :id");
}
$stmnt->bindValue(':id', $id);
$stmnt->execute();
@ -155,6 +155,7 @@ class AccountGateway {
]
);
return array_map(fn(array $acc) => new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profile_picture"], $this->isAdmin($acc["id"]))), $res);
}
/**

@ -47,6 +47,7 @@ class MemberGateway {
]
);
return array_map(fn($row) => new Member(new User($row['email'], $row['username'], $row['id'], $row['profile_picture'], $row['is_admin']), $teamId, $row['role']), $rows);
}
/**

@ -28,11 +28,16 @@ class AuthModel {
* @param string $email
* @return Account|null the registered account or null if the account already exists for the given email address
*/
public function register(
string $username,
string $password,
string $email
): ?Account {
public function register(string $username,
string $password,
string $confirmPassword,
string $email,
array &$failures): ?Account {
if ($password != $confirmPassword) {
$failures[] = new FieldValidationFail("confirmpassword", "Le mot de passe et la confirmation ne sont pas les mêmes.");
}
if ($this->gateway->exists($email)) {
return null;
}
@ -53,6 +58,7 @@ class AuthModel {
} catch (Exception $e) {
throw new \RuntimeException($e);
}
}
/**

Loading…
Cancel
Save