2021-08-05 05:20:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware\Api;
|
|
|
|
|
|
|
|
use Closure;
|
2021-08-07 18:15:30 +00:00
|
|
|
use Exception;
|
2021-08-05 05:20:43 +00:00
|
|
|
use Illuminate\Support\Reflector;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Contracts\Routing\UrlRoutable;
|
|
|
|
|
|
|
|
class PreventUnboundModels
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Prevents a request from making it to a controller action if there is a model
|
|
|
|
* injection on the controller that has not been explicitly bound by the request.
|
|
|
|
* This prevents empty models from being valid in scenarios where a new model is
|
|
|
|
* added but not properly defined in the substitution middleware.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
2021-08-07 18:15:30 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2021-08-05 05:20:43 +00:00
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
$route = $request->route();
|
2021-08-07 18:15:30 +00:00
|
|
|
$parameters = $route->parameters() ?? [];
|
2021-08-05 05:20:43 +00:00
|
|
|
|
2021-08-07 18:15:30 +00:00
|
|
|
/** @var \ReflectionParameter[] $signatures */
|
|
|
|
$signatures = $route->signatureParameters(UrlRoutable::class);
|
|
|
|
foreach ($signatures as $signature) {
|
|
|
|
$class = Reflector::getParameterClassName($signature);
|
2021-08-05 05:20:43 +00:00
|
|
|
if (is_null($class) || !is_subclass_of($class, Model::class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-07 18:15:30 +00:00
|
|
|
if (!$parameters[$signature->getName()] instanceof Model) {
|
|
|
|
throw new Exception(
|
|
|
|
sprintf('No parameter binding has been defined for model [%s] using route parameter key "%s".', $class, $signature->getName())
|
2021-08-05 05:20:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|