Path: blob/1.0-develop/app/Http/Requests/Api/Client/Servers/Subusers/SubuserRequest.php
10284 views
<?php12namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers;34use Illuminate\Http\Request;5use Pterodactyl\Models\User;6use Pterodactyl\Models\Subuser;7use Pterodactyl\Exceptions\Http\HttpForbiddenException;8use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;9use Pterodactyl\Services\Servers\GetUserPermissionsService;1011abstract class SubuserRequest extends ClientApiRequest12{13protected ?Subuser $model;1415/**16* Authorize the request and ensure that a user is not trying to modify themselves.17*18* @throws \Illuminate\Contracts\Container\BindingResolutionException19*/20public function authorize(): bool21{22if (!parent::authorize()) {23return false;24}2526$user = $this->route()->parameter('user');27// Don't allow a user to edit themselves on the server.28if ($user instanceof User) {29if ($user->uuid === $this->user()->uuid) {30return false;31}32}3334// If this is a POST request, validate that the user can even assign the permissions they35// have selected to assign.36if ($this->method() === Request::METHOD_POST && $this->has('permissions')) {37$this->validatePermissionsCanBeAssigned(38$this->input('permissions') ?? []39);40}4142return true;43}4445/**46* Validates that the permissions we are trying to assign can actually be assigned47* by the user making the request.48*49* @throws \Illuminate\Contracts\Container\BindingResolutionException50*/51protected function validatePermissionsCanBeAssigned(array $permissions)52{53$user = $this->user();54/** @var \Pterodactyl\Models\Server $server */55$server = $this->route()->parameter('server');5657// If we are a root admin or the server owner, no need to perform these checks.58if ($user->root_admin || $user->id === $server->owner_id) {59return;60}6162// Otherwise, get the current subuser's permission set, and ensure that the63// permissions they are trying to assign are not _more_ than the ones they64// already have.65$service = $this->container->make(GetUserPermissionsService::class);6667if (count(array_diff($permissions, $service->handle($server, $user))) > 0) {68throw new HttpForbiddenException('Cannot assign permissions to a subuser that your account does not actively possess.');69}70}71}727374