From 5b58276ae96e11a1b127fcfec060331b44788ada Mon Sep 17 00:00:00 2001 From: gemul Date: Mon, 26 Aug 2024 10:23:03 +0700 Subject: [PATCH] Set maximum records limit per-page in config. --- src/QueryDataTable.php | 2 +- src/Utilities/Config.php | 10 ++++++++++ src/config/datatables.php | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 946f1ccf..beae74e4 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -569,7 +569,7 @@ public function paging(): void $start = $this->request->start(); $length = $this->request->length(); - $limit = $length > 0 ? $length : 10; + $limit = $length > 0 ? min($length, $this->config->maxLimit()) : 10; if (is_callable($this->limitCallback)) { $this->query->limit($limit); diff --git a/src/Utilities/Config.php b/src/Utilities/Config.php index ae474368..5b6b473d 100644 --- a/src/Utilities/Config.php +++ b/src/Utilities/Config.php @@ -93,4 +93,14 @@ public function jsonHeaders(): array { return (array) $this->repository->get('datatables.json.header', []); } + + /** + * Get the maximum record limit for one page. + * + * @return int The maximum limit value. + */ + public function maxLimit(): int + { + return (int) $this->repository->get('datatables.max_limit', 100); + } } diff --git a/src/config/datatables.php b/src/config/datatables.php index 08912643..59989c65 100644 --- a/src/config/datatables.php +++ b/src/config/datatables.php @@ -124,4 +124,10 @@ * Callbacks needs to start by those terms, or they will be cast to string. */ 'callback' => ['$', '$.', 'function'], + + /* + * Maximum record limit per-page. To prevent user from arbitrarily request large record set. + * If 'limit' query parameter in the request exceed this limit, it will be automatically set to this value. + */ + 'max_limit' => 100, ];