Merge branch 'main' of https://codefirst.iut.uca.fr/git/clement.freville2/silex
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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']));
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user