diff --git a/public/index.php b/public/index.php index 3389671..abf0d72 100644 --- a/public/index.php +++ b/public/index.php @@ -16,4 +16,5 @@ $router->get('/^recent\/(?\d+)$/', [$user, 'index']); $router->get('/^news\/(?\d+)$/', [$user, 'viewPost']); $router->get('/^comments\/(?[\w-]+)$/', [$user, 'viewPostComments']); $router->match('/^login$/', [$security, 'login']); +$router->match('/^register$/', [$security, 'register']); $router->run(new \Silex\DI\DI($router))->render($router, __DIR__ . '/../' . VIEW_PATH); diff --git a/src/Silex/Controller/SecurityController.php b/src/Silex/Controller/SecurityController.php index 177008b..a2f8ba4 100644 --- a/src/Silex/Controller/SecurityController.php +++ b/src/Silex/Controller/SecurityController.php @@ -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]); } -} \ No newline at end of file + + 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]); + } +} diff --git a/src/Silex/Gateway/UserGateway.php b/src/Silex/Gateway/UserGateway.php index 1bb3551..0467f21 100644 --- a/src/Silex/Gateway/UserGateway.php +++ b/src/Silex/Gateway/UserGateway.php @@ -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; + } } diff --git a/src/Silex/Model/User.php b/src/Silex/Model/User.php index c89f60a..a5d1d58 100644 --- a/src/Silex/Model/User.php +++ b/src/Silex/Model/User.php @@ -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; + } } diff --git a/src/Silex/Security/Security.php b/src/Silex/Security/Security.php index eabca7c..fcf44cf 100644 --- a/src/Silex/Security/Security.php +++ b/src/Silex/Security/Security.php @@ -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; + } } diff --git a/views/login.php b/views/login.php index 71e625a..6c881e2 100644 --- a/views/login.php +++ b/views/login.php @@ -12,13 +12,13 @@
- +
- +
diff --git a/views/register.php b/views/register.php new file mode 100644 index 0000000..4452f38 --- /dev/null +++ b/views/register.php @@ -0,0 +1,36 @@ + +
+
+

Registration failed

+
+
+ Login is already taken. +
+
+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+
+ +
+
+