2022-05-22 14:10:01 -04:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Http\Middleware\Api\Client;
|
2022-05-22 14:10:01 -04:00
|
|
|
|
2024-03-19 21:12:27 -04:00
|
|
|
use Illuminate\Http\Request;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Server;
|
2024-03-16 15:11:10 -04:00
|
|
|
use Illuminate\Contracts\Routing\Registrar;
|
2022-05-22 14:10:01 -04:00
|
|
|
use Illuminate\Routing\Middleware\SubstituteBindings;
|
|
|
|
|
|
|
|
|
|
class SubstituteClientBindings extends SubstituteBindings
|
|
|
|
|
{
|
2024-03-16 15:11:10 -04:00
|
|
|
public function __construct(Registrar $router, private Server $server)
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($router);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 21:12:27 -04:00
|
|
|
public function handle(Request $request, \Closure $next): mixed
|
2022-05-22 14:10:01 -04:00
|
|
|
{
|
|
|
|
|
// Override default behavior of the model binding to use a specific table
|
|
|
|
|
// column rather than the default 'id'.
|
|
|
|
|
$this->router->bind('server', function ($value) {
|
2024-03-16 15:11:10 -04:00
|
|
|
return $this->server->query()->where(strlen($value) === 8 ? 'uuidShort' : 'uuid', $value)->firstOrFail();
|
2022-05-22 14:10:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->router->bind('user', function ($value, $route) {
|
2024-03-12 22:39:16 -04:00
|
|
|
/** @var \App\Models\Subuser $match */
|
2022-05-22 14:10:01 -04:00
|
|
|
$match = $route->parameter('server')
|
|
|
|
|
->subusers()
|
|
|
|
|
->whereRelation('user', 'uuid', '=', $value)
|
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
|
|
return $match->user;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return parent::handle($request, $next);
|
|
|
|
|
}
|
|
|
|
|
}
|