@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\Api;
|
||||||
|
|
||||||
|
use App\Controller\Control;
|
||||||
|
use App\Http\HttpRequest;
|
||||||
|
use App\Http\HttpResponse;
|
||||||
|
use App\Http\JsonHttpResponse;
|
||||||
|
use App\Model\AuthModel;
|
||||||
|
use App\Validation\Validators;
|
||||||
|
|
||||||
|
class APIAuthController {
|
||||||
|
private AuthModel $model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AuthModel $model
|
||||||
|
*/
|
||||||
|
public function __construct(AuthModel $model) {
|
||||||
|
$this->model = $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function authorize(): HttpResponse {
|
||||||
|
return Control::runChecked([
|
||||||
|
"email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"), Validators::lenBetween(5, 256)],
|
||||||
|
"password" => [Validators::lenBetween(6, 256)],
|
||||||
|
], function (HttpRequest $req) {
|
||||||
|
$failures = [];
|
||||||
|
$account = $this->model->login($req["email"], $req["password"], $failures);
|
||||||
|
|
||||||
|
if (!empty($failures)) {
|
||||||
|
return new JsonHttpResponse($failures);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JsonHttpResponse(["authorization" => $account->getToken()]);
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Connexion;
|
||||||
|
use App\Gateway\AccountGateway;
|
||||||
|
use App\Http\HttpResponse;
|
||||||
|
use App\Model\AuthModel;
|
||||||
|
use App\Session\MutableSessionHandle;
|
||||||
|
|
||||||
|
class VisitorController {
|
||||||
|
final public function register(MutableSessionHandle $session): HttpResponse {
|
||||||
|
$model = new AuthModel(new AccountGateway(new Connexion(get_database())));
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||||
|
return (new Sub\AuthController($model))->displayRegister();
|
||||||
|
}
|
||||||
|
return (new Sub\AuthController($model))->confirmRegister($_POST, $session);
|
||||||
|
}
|
||||||
|
|
||||||
|
final public function login(MutableSessionHandle $session): HttpResponse {
|
||||||
|
$model = new AuthModel(new AccountGateway(new Connexion(get_database())));
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||||
|
return (new Sub\AuthController($model))->displayLogin();
|
||||||
|
}
|
||||||
|
return (new Sub\AuthController($model))->confirmLogin($_POST, $session);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Gateway;
|
||||||
|
|
||||||
|
use App\Connexion;
|
||||||
|
use App\Data\Account;
|
||||||
|
use PDO;
|
||||||
|
|
||||||
|
class AccountGateway {
|
||||||
|
private Connexion $con;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Connexion $con
|
||||||
|
*/
|
||||||
|
public function __construct(Connexion $con) {
|
||||||
|
$this->con = $con;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function insertAccount(string $name, string $email, string $token, string $hash): int {
|
||||||
|
$this->con->exec("INSERT INTO Account(username, hash, email, token) VALUES (:username,:hash,:email,:token)", [
|
||||||
|
':username' => [$name, PDO::PARAM_STR],
|
||||||
|
':hash' => [$hash, PDO::PARAM_STR],
|
||||||
|
':email' => [$email, PDO::PARAM_STR],
|
||||||
|
':token' => [$token, PDO::PARAM_STR],
|
||||||
|
]);
|
||||||
|
return intval($this->con->lastInsertId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $email
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
private function getRowsFromMail(string $email): ?array {
|
||||||
|
return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getHash(string $email): ?string {
|
||||||
|
$results = $this->getRowsFromMail($email);
|
||||||
|
if ($results == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return $results['hash'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists(string $email): bool {
|
||||||
|
return $this->getRowsFromMail($email) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $email
|
||||||
|
* @return Account|null
|
||||||
|
*/
|
||||||
|
public function getAccountFromMail(string $email): ?Account {
|
||||||
|
$acc = $this->getRowsFromMail($email);
|
||||||
|
if (empty($acc)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Account($email, $acc["username"], $acc["token"], $acc["id"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAccountFromToken(string $token): ?Account {
|
||||||
|
$acc = $this->con->fetch("SELECT * FROM Account WHERE token = :token", [':token' => [$token, PDO::PARAM_STR]])[0] ?? null;
|
||||||
|
if (empty($acc)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Account($acc["email"], $acc["username"], $acc["token"], $acc["id"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,47 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Gateway;
|
|
||||||
|
|
||||||
use App\Connexion;
|
|
||||||
use PDO;
|
|
||||||
|
|
||||||
class AuthGateway {
|
|
||||||
private Connexion $con;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connexion $con
|
|
||||||
*/
|
|
||||||
public function __construct(Connexion $con) {
|
|
||||||
$this->con = $con;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function mailExist(string $email): bool {
|
|
||||||
return $this->getUserFields($email) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function insertAccount(string $username, string $hash, string $email): void {
|
|
||||||
$this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash' => [$hash, PDO::PARAM_STR],':email' => [$email, PDO::PARAM_STR]]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getUserHash(string $email): string {
|
|
||||||
$results = $this->con->fetch("SELECT hash FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]);
|
|
||||||
return $results[0]['hash'];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $email
|
|
||||||
* @return array<string,string>|null
|
|
||||||
*/
|
|
||||||
public function getUserFields(string $email): ?array {
|
|
||||||
$results = $this->con->fetch("SELECT username,email FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]);
|
|
||||||
$firstRow = $results[0] ?? null;
|
|
||||||
return $firstRow;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Gateway;
|
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use App\Connexion;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A sample gateway, that stores the sample form's result.
|
|
||||||
*/
|
|
||||||
class FormResultGateway {
|
|
||||||
private Connexion $con;
|
|
||||||
|
|
||||||
public function __construct(Connexion $con) {
|
|
||||||
$this->con = $con;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function insert(string $username, string $description): void {
|
|
||||||
$this->con->exec(
|
|
||||||
"INSERT INTO FormEntries VALUES (:name, :description)",
|
|
||||||
[
|
|
||||||
":name" => [$username, PDO::PARAM_STR],
|
|
||||||
"description" => [$description, PDO::PARAM_STR],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function listResults(): array {
|
|
||||||
return $this->con->fetch("SELECT * FROM FormEntries", []);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Session;
|
||||||
|
|
||||||
|
use App\Data\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The mutable side of a session handle
|
||||||
|
*/
|
||||||
|
interface MutableSessionHandle extends SessionHandle {
|
||||||
|
/**
|
||||||
|
* @param string|null $url the url to redirect the user to after authentication.
|
||||||
|
*/
|
||||||
|
public function setInitialTarget(?string $url): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Account $account update the session's account
|
||||||
|
*/
|
||||||
|
public function setAccount(Account $account): void;
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Session;
|
||||||
|
|
||||||
|
use App\Data\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A PHP session handle
|
||||||
|
*/
|
||||||
|
class PhpSessionHandle implements MutableSessionHandle {
|
||||||
|
public static function init(): self {
|
||||||
|
if (session_status() !== PHP_SESSION_NONE) {
|
||||||
|
throw new \Exception("A php session is already started !");
|
||||||
|
}
|
||||||
|
session_start();
|
||||||
|
return new PhpSessionHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAccount(): ?Account {
|
||||||
|
return $_SESSION["account"] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInitialTarget(): ?string {
|
||||||
|
return $_SESSION["target"] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAccount(Account $account): void {
|
||||||
|
$_SESSION["account"] = $account;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setInitialTarget(?string $url): void {
|
||||||
|
$_SESSION["target"] = $url;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Session;
|
||||||
|
|
||||||
|
use App\Data\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An immutable session handle
|
||||||
|
*/
|
||||||
|
interface SessionHandle {
|
||||||
|
/**
|
||||||
|
* The initial target url if the user wanted to perform an action that requires authentication
|
||||||
|
* but has been required to login first in the application.
|
||||||
|
* @return string|null Get the initial targeted URL
|
||||||
|
*/
|
||||||
|
public function getInitialTarget(): ?string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The session account if the user is logged in.
|
||||||
|
* @return Account|null
|
||||||
|
*/
|
||||||
|
public function getAccount(): ?Account;
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator;
|
||||||
|
|
||||||
|
use App\Data\TacticInfo;
|
||||||
|
use App\Validation\ValidationFail;
|
||||||
|
|
||||||
|
class TacticValidator {
|
||||||
|
public static function validateAccess(?TacticInfo $tactic, int $tacticId, int $ownerId): ?ValidationFail {
|
||||||
|
if ($tactic == null) {
|
||||||
|
return ValidationFail::notFound("La tactique $tacticId n'existe pas");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tactic->getOwnerId() != $ownerId) {
|
||||||
|
return ValidationFail::unauthorized("Vous ne pouvez pas accéder à cette tactique.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue