Skip to content

Commit d35c595

Browse files
committed
Implemented #504
1 parent 2edf81f commit d35c595

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/Tqdev/PhpCrudApi/Middleware/PageLimitsMiddleware.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tqdev\PhpCrudApi\Controller\Responder;
66
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
77
use Tqdev\PhpCrudApi\Middleware\Router\Router;
8+
use Tqdev\PhpCrudApi\Record\ErrorCode;
89
use Tqdev\PhpCrudApi\Record\RequestUtils;
910
use Tqdev\PhpCrudApi\Request;
1011
use Tqdev\PhpCrudApi\Response;
@@ -28,10 +29,12 @@ public function handle(Request $request): Response
2829
$maxPage = (int) $this->getProperty('pages', '100');
2930
if (isset($params['page']) && $params['page'] && $maxPage > 0) {
3031
if (strpos($params['page'][0], ',') === false) {
31-
$params['page'] = array(min($params['page'][0], $maxPage));
32+
$page = $params['page'][0];
3233
} else {
3334
list($page, $size) = explode(',', $params['page'][0], 2);
34-
$params['page'] = array(min($page, $maxPage) . ',' . $size);
35+
}
36+
if ($page > $maxPage) {
37+
return $this->responder->error(ErrorCode::PAGINATION_FORBIDDEN, '');
3538
}
3639
}
3740
$maxSize = (int) $this->getProperty('records', '1000');

src/Tqdev/PhpCrudApi/Record/ErrorCode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ErrorCode
3030
const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
3131
const BAD_OR_MISSING_XSRF_TOKEN = 1017;
3232
const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
33-
const FILE_UPLOAD_FAILED = 1019;
33+
const PAGINATION_FORBIDDEN = 1019;
3434

3535
private $values = [
3636
9999 => ["%s", Response::INTERNAL_SERVER_ERROR],
@@ -53,7 +53,7 @@ class ErrorCode
5353
1016 => ["Temporary or permanently blocked", Response::FORBIDDEN],
5454
1017 => ["Bad or missing XSRF token", Response::FORBIDDEN],
5555
1018 => ["Only AJAX requests allowed for '%s'", Response::FORBIDDEN],
56-
1019 => ["File upload failed for '%s'", Response::UNPROCESSABLE_ENTITY],
56+
1019 => ["Pagination forbidden", Response::FORBIDDEN],
5757
];
5858

5959
public function __construct(int $code)

tests/functional/001_records/077_list_posts_with_page_limits.log

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Content-Length: 78
2424
===
2525
GET /records/posts?page=6,1
2626
===
27-
200
27+
403
2828
Content-Type: application/json
29-
Content-Length: 78
29+
Content-Length: 46
3030

31-
{"records":[{"id":7,"user_id":1,"category_id":1,"content":"#3"}],"results":12}
31+
{"code":1019,"message":"Pagination forbidden"}

0 commit comments

Comments
 (0)