2020-03-27 23:57:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2020-08-20 03:21:12 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-03-27 23:57:49 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
2020-08-20 03:21:12 +00:00
|
|
|
use Pterodactyl\Services\Servers\GetUserPermissionsService;
|
2020-03-27 23:57:49 +00:00
|
|
|
|
|
|
|
abstract class SubuserRequest extends ClientApiRequest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\Subuser|null
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authorize the request and ensure that a user is not trying to modify themselves.
|
|
|
|
*
|
2020-04-10 19:47:08 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-03-27 23:57:49 +00:00
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!parent::authorize()) {
|
2020-03-27 23:57:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-20 03:21:12 +00:00
|
|
|
$user = $this->route()->parameter('user');
|
|
|
|
// Don't allow a user to edit themselves on the server.
|
|
|
|
if ($user instanceof User) {
|
|
|
|
if ($user->uuid === $this->user()->uuid) {
|
2020-03-27 23:57:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a POST request, validate that the user can even assign the permissions they
|
|
|
|
// have selected to assign.
|
|
|
|
if ($this->method() === Request::METHOD_POST && $this->has('permissions')) {
|
|
|
|
$this->validatePermissionsCanBeAssigned(
|
|
|
|
$this->input('permissions') ?? []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates that the permissions we are trying to assign can actually be assigned
|
|
|
|
* by the user making the request.
|
|
|
|
*
|
2020-04-10 19:47:08 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-03-27 23:57:49 +00:00
|
|
|
*/
|
|
|
|
protected function validatePermissionsCanBeAssigned(array $permissions)
|
|
|
|
{
|
|
|
|
$user = $this->user();
|
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $this->route()->parameter('server');
|
|
|
|
|
|
|
|
// If we are a root admin or the server owner, no need to perform these checks.
|
|
|
|
if ($user->root_admin || $user->id === $server->owner_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, get the current subuser's permission set, and ensure that the
|
|
|
|
// permissions they are trying to assign are not _more_ than the ones they
|
|
|
|
// already have.
|
2020-08-20 03:21:12 +00:00
|
|
|
$service = $this->container->make(GetUserPermissionsService::class);
|
|
|
|
|
|
|
|
if (count(array_diff($permissions, $service->handle($server, $user))) > 0) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new HttpForbiddenException('Cannot assign permissions to a subuser that your account does not actively possess.');
|
2020-03-27 23:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|