From a68b385f099ff522d33472791fda0e0dd21866a8 Mon Sep 17 00:00:00 2001 From: Maurits van der Schee Date: Sun, 30 Oct 2022 10:49:41 +0100 Subject: [PATCH] build for #901 and #906 --- api.include.php | 19 +++++++++++++++++-- api.php | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/api.include.php b/api.include.php index 9996331e..da413d3a 100644 --- a/api.include.php +++ b/api.include.php @@ -8320,6 +8320,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $passwordLength = $this->getProperty('passwordLength', '12'); $pkName = $table->getPk()->getName(); $registerUser = $this->getProperty('registerUser', ''); + $loginAfterRegistration = $this->getProperty('loginAfterRegistration', ''); $condition = new ColumnCondition($usernameColumn, 'eq', $username); $returnedColumns = $this->getProperty('returnedColumns', ''); if (!$returnedColumns) { @@ -8334,6 +8335,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if (!$registerUser) { return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username); } + if(strlen(trim($username)) == 0){ + return $this->responder->error(ErrorCode::USERNAME_EMPTY, $username); + } if (strlen($password) < $passwordLength) { return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength); } @@ -8348,8 +8352,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $this->db->createSingle($table, $data); $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1); foreach ($users as $user) { - unset($user[$passwordColumnName]); - return $this->responder->success($user); + if($loginAfterRegistration){ + if (!headers_sent()) { + session_regenerate_id(true); + } + unset($user[$passwordColumnName]); + $_SESSION['user'] = $user; + return $this->responder->success($user); + } else { + unset($user[$passwordColumnName]); + return $this->responder->success($user); + } } return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username); } @@ -11070,6 +11083,7 @@ class ErrorCode const PAGINATION_FORBIDDEN = 1019; const USER_ALREADY_EXIST = 1020; const PASSWORD_TOO_SHORT = 1021; + const USERNAME_EMPTY = 1022; private $values = [ 0000 => ["Success", ResponseFactory::OK], @@ -11095,6 +11109,7 @@ class ErrorCode 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN], 1020 => ["User '%s' already exists", ResponseFactory::CONFLICT], 1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY], + 1022 => ["Username is empty or only whitespaces", ResponseFactory::UNPROCESSABLE_ENTITY], 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR], ]; diff --git a/api.php b/api.php index 54e6f599..6a9f1c08 100644 --- a/api.php +++ b/api.php @@ -8320,6 +8320,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $passwordLength = $this->getProperty('passwordLength', '12'); $pkName = $table->getPk()->getName(); $registerUser = $this->getProperty('registerUser', ''); + $loginAfterRegistration = $this->getProperty('loginAfterRegistration', ''); $condition = new ColumnCondition($usernameColumn, 'eq', $username); $returnedColumns = $this->getProperty('returnedColumns', ''); if (!$returnedColumns) { @@ -8334,6 +8335,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if (!$registerUser) { return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username); } + if(strlen(trim($username)) == 0){ + return $this->responder->error(ErrorCode::USERNAME_EMPTY, $username); + } if (strlen($password) < $passwordLength) { return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength); } @@ -8348,8 +8352,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $this->db->createSingle($table, $data); $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1); foreach ($users as $user) { - unset($user[$passwordColumnName]); - return $this->responder->success($user); + if($loginAfterRegistration){ + if (!headers_sent()) { + session_regenerate_id(true); + } + unset($user[$passwordColumnName]); + $_SESSION['user'] = $user; + return $this->responder->success($user); + } else { + unset($user[$passwordColumnName]); + return $this->responder->success($user); + } } return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username); } @@ -11070,6 +11083,7 @@ class ErrorCode const PAGINATION_FORBIDDEN = 1019; const USER_ALREADY_EXIST = 1020; const PASSWORD_TOO_SHORT = 1021; + const USERNAME_EMPTY = 1022; private $values = [ 0000 => ["Success", ResponseFactory::OK], @@ -11095,6 +11109,7 @@ class ErrorCode 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN], 1020 => ["User '%s' already exists", ResponseFactory::CONFLICT], 1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY], + 1022 => ["Username is empty or only whitespaces", ResponseFactory::UNPROCESSABLE_ENTITY], 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR], ];