diff --git a/config.php b/config.php index fdf02a4..54cfa88 100644 --- a/config.php +++ b/config.php @@ -3,6 +3,7 @@ // `dev-config-profile.php` by default. // on production server the included profile is `prod-config-profile.php`. // Please do not touch. + require /*PROFILE_FILE*/ "profiles/dev-config-profile.php"; const SUPPORTS_FAST_REFRESH = _SUPPORTS_FAST_REFRESH; @@ -21,3 +22,9 @@ global $_data_source_name; $data_source_name = $_data_source_name; const DATABASE_USER = _DATABASE_USER; const DATABASE_PASSWORD = _DATABASE_PASSWORD; + + +function init_database(PDO $pdo): void { + _init_database($pdo); +} + diff --git a/profiles/dev-config-profile.php b/profiles/dev-config-profile.php index bd87f1d..bb510e7 100644 --- a/profiles/dev-config-profile.php +++ b/profiles/dev-config-profile.php @@ -1,5 +1,9 @@ insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT), "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"); + $accounts->setIsAdmin($id, true); + } +} \ No newline at end of file diff --git a/profiles/prod-config-profile.php b/profiles/prod-config-profile.php index e9bb12c..185541a 100644 --- a/profiles/prod-config-profile.php +++ b/profiles/prod-config-profile.php @@ -20,3 +20,6 @@ function _asset(string $assetURI): string { // fallback to the uri itself. return $basePath . "/" . (ASSETS[$assetURI] ?? $assetURI); } + + +function _init_database(PDO $pdo): void {} diff --git a/sql/database.php b/sql/database.php index af94ef2..54ee7de 100644 --- a/sql/database.php +++ b/sql/database.php @@ -1,9 +1,5 @@ insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT), "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"); - $accounts->setIsAdmin($id, true); - } -} diff --git a/src/Api/APIControl.php b/src/Api/APIControl.php index 260575a..751fbfb 100644 --- a/src/Api/APIControl.php +++ b/src/Api/APIControl.php @@ -4,6 +4,7 @@ namespace IQBall\Api; use IQBall\Core\Control; use IQBall\Core\ControlSchemaErrorResponseFactory; +use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; @@ -13,7 +14,7 @@ class APIControl { private static function errorFactory(): ControlSchemaErrorResponseFactory { return new class () implements ControlSchemaErrorResponseFactory { public function apply(array $failures): HttpResponse { - return new JsonHttpResponse($failures); + return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST); } }; } diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index 13d9db4..32fd956 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -35,7 +35,7 @@ class APIAccountsController { public function listUsers(array $request): HttpResponse { return APIControl::runCheckedFrom($request, [ 'start' => [DefaultValidators::isUnsignedInteger()], - 'n' => [DefaultValidators::isUnsignedInteger()], + 'n' => [DefaultValidators::isIntInRange(0, 250)], 'search' => [DefaultValidators::lenBetween(0, 256)], ], function (HttpRequest $req) { $accounts = $this->accounts->searchAccounts(intval($req['start']), intval($req['n']), $req["search"]); diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index d6b5686..9d36f14 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -145,7 +145,7 @@ class AccountGateway { * @param int $start starting index of the list content * @return Account[]|null */ - public function searchAccounts(int $start, int $n, ?string $searchString): ?array { + public function searchAccounts(int $start, int $n, ?string $searchString): array { $res = $this->con->fetch( "SELECT * FROM Account WHERE username LIKE '%' || :search || '%' OR email LIKE '%' || :search || '%' ORDER BY username, email LIMIT :offset, :n", [ diff --git a/src/Core/Model/TacticModel.php b/src/Core/Model/TacticModel.php index 590a106..920075b 100644 --- a/src/Core/Model/TacticModel.php +++ b/src/Core/Model/TacticModel.php @@ -68,9 +68,9 @@ class TacticModel { * NOTE: if given user id does not match any user, this function returns an empty array * * @param integer $user_id - * @return TacticInfo[] | null + * @return TacticInfo[] */ - public function listAllOf(int $user_id): ?array { + public function listAllOf(int $user_id): array { return$this->tactics->listAllOf($user_id); } diff --git a/src/Core/Validation/DefaultValidators.php b/src/Core/Validation/DefaultValidators.php index 6e64e30..c898170 100644 --- a/src/Core/Validation/DefaultValidators.php +++ b/src/Core/Validation/DefaultValidators.php @@ -72,7 +72,7 @@ class DefaultValidators { public static function isInteger(): Validator { - return self::regex("/^-[0-9]+$/", "field is not an integer"); + return self::regex("/^[-+]?[0-9]+$/", "field is not an integer"); } public static function isUnsignedInteger(): Validator { @@ -135,10 +135,8 @@ class DefaultValidators { public function validate(string $name, $val): array { $failures = []; - $idx = 0; - foreach ($val as $item) { + foreach ($val as $idx => $item) { $failures = array_merge($failures, $this->validator->validate($name . "[$idx]", $item)); - $idx += 1; } return $failures;