This commit is contained in:
Colin FRIZOT
2022-11-30 11:43:29 +01:00
14 changed files with 145 additions and 34 deletions

View File

@@ -6,6 +6,7 @@ namespace Silex\Controller;
use Silex\DI\DI;
use Silex\Http\HttpResponse;
use Silex\Model\User;
class SecurityController
{
@@ -19,9 +20,23 @@ class SecurityController
header('Location: ' . $di->getRouter()->url(''));
exit();
}
var_dump($success);
$fail = !$success;
}
return HttpResponse::found('login', ['fail' => $fail]);
}
}
public function register(DI $di): HttpResponse
{
$fail = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
if ($user !== null) {
http_response_code(303);
header('Location: ' . $di->getRouter()->url(''));
exit();
}
$fail = $user === null;
}
return HttpResponse::found('register', ['fail' => $fail]);
}
}

View File

@@ -28,7 +28,7 @@ class UserController
} else {
$nbCommentsByUser = 0;
}
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'router' => $di->getRouter(), 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]);
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]);
}
public function viewPost(DI $di, array $params): HttpResponse

View File

@@ -24,7 +24,7 @@ class NewsGateway
*/
public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array
{
$req = $this->pdo->prepare('SELECT title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
$req = $this->pdo->prepare('SELECT id_news, title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
$req->bindValue('limit', $limit, PDO::PARAM_INT);
$req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT);
if (!$req->execute()) {
@@ -61,6 +61,6 @@ class NewsGateway
private function createNews(array $data): News
{
return new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
return new News(intval($data['id_news']), $data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
}
}

View File

@@ -33,4 +33,12 @@ class UserGateway
$user = $req->fetch();
return $user === false ? null : $user;
}
public function insert(User $user): bool
{
$req = $this->pdo->prepare('INSERT INTO registered_user (login, password, role) VALUES (:login, :password, :role);');
$req->execute(['login' => $user->getLogin(), 'password' => $user->getPasswordHash(), 'role' => $user->getRole()]);
$user->setId(intval($this->pdo->lastInsertId()));
return true;
}
}

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Silex\Http;
use Silex\Router\Router;
class HttpResponse
{
private int $status;
@@ -24,7 +26,7 @@ class HttpResponse
return new HttpResponse(200, $viewPath, $viewParams);
}
public function render(string $viewBasePath)
public function render(Router $router, string $viewBasePath)
{
$params = $this->viewParams;
ob_start();

View File

@@ -8,17 +8,24 @@ use DateTime;
class News
{
private int $id;
private string $title;
private string $content;
private DateTime $publicationDate;
public function __construct(string $title, string $content, DateTime $publicationDate)
public function __construct(int $id, string $title, string $content, DateTime $publicationDate)
{
$this->id = $id;
$this->title = $title;
$this->content = $content;
$this->publicationDate = $publicationDate;
}
public function getId(): int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;

View File

@@ -11,11 +11,12 @@ class User
private string $password;
private int $role;
public static function fromRawPassword(string $login, string $password): User
public static function fromRawPassword(string $login, string $password, int $role = 0): User
{
$user = new User();
$user->login = $login;
$user->password = password_hash($password, PASSWORD_DEFAULT);
$user->role = $role;
return $user;
}
@@ -38,4 +39,9 @@ class User
{
return $this->role;
}
public function setId(int $id)
{
$this->id_user = $id;
}
}

View File

@@ -56,7 +56,12 @@ class Router
public function url(string $url): string
{
return $this->basePath . '/' . $url;
if ($this->basePath !== '') {
return "/" . $this->basePath . '/' . $url;
} else {
return $this->basePath . '/' . $url;
}
}
public function run(DI $di): HttpResponse

View File

@@ -45,4 +45,14 @@ class Security
}
return $this->user;
}
public function register(User $user): ?User
{
if (!$this->userGateway->insert($user)) {
return null;
}
$this->session[USER] = $user->getId();
$this->user = $user;
return $user;
}
}