Skip to content

Commit bdd4b9f

Browse files
authored
Merge pull request #6622 from WoltLab/63-http-helper-fetch-from-query
Add a helper method to fetch a dbo from the `id` query parameter
2 parents 6d1f90d + feecc8d commit bdd4b9f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

wcfsetup/install/files/lib/http/Helper.class.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Psr\Http\Message\ServerRequestInterface;
1212
use Psr\Http\Message\UriInterface;
1313
use wcf\data\DatabaseObject;
14+
use wcf\system\exception\IllegalLinkException;
1415
use wcf\system\exception\ParentClassException;
1516
use wcf\system\exception\UserInputException;
1617
use wcf\util\StringUtil;
@@ -197,6 +198,48 @@ public static function fetchObjectFromRequestParameter(int|string $objectID, str
197198
return $dbo;
198199
}
199200

201+
/**
202+
* Fetches a database object using the `id` parameter from GET parameters.
203+
*
204+
* If the value does not resolve to an object, i.e. its object id is not
205+
* truthy, a IllegalLinkException is thrown.
206+
*
207+
* @template T of object
208+
* @param class-string<T> $className
209+
* @return T
210+
* @throws IllegalLinkException
211+
* @throws ParentClassException
212+
* @since 6.3
213+
*/
214+
public static function fetchObjectFromQueryParameter(string $className): object
215+
{
216+
if (!\is_subclass_of($className, DatabaseObject::class)) {
217+
throw new ParentClassException($className, DatabaseObject::class);
218+
}
219+
220+
try {
221+
$queryParameters = self::mapQueryParameters(
222+
$_GET,
223+
<<<'EOT'
224+
array {
225+
id: positive-int
226+
}
227+
EOT
228+
);
229+
} catch (MappingError) {
230+
throw new IllegalLinkException();
231+
}
232+
233+
/** @var DatabaseObject $dbo */
234+
$dbo = new $className($queryParameters['id']);
235+
236+
if (!$dbo->getObjectID()) {
237+
throw new IllegalLinkException();
238+
}
239+
240+
return $dbo;
241+
}
242+
200243
/**
201244
* Forbid creation of Helper objects.
202245
*/

0 commit comments

Comments
 (0)