route(); throw_if($route === null, UnexpectedValueException::class, 'The subuser request does not have an active route.'); $user = $route->parameter('user'); // Don't allow a user to edit themselves on the server. if ($user instanceof User) { if ($user->uuid === $this->user()->uuid) { 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->permissionInput()); } return true; } public function subuser(): Subuser { $subuser = $this->attributes->get('subuser'); throw_unless($subuser instanceof Subuser, UnexpectedValueException::class, 'The subuser request is missing its subuser attribute.'); return $subuser; } /** * The permissions to store for the subuser: the submitted keys the panel or an enabled * extension knows about, always including websocket access. The API drops any other key * it is sent, where the action would refuse it. * * @return list */ public function permissions(): array { $allowed = $this->container->make(SubuserPermissionCatalog::class)->keys(); $cleaned = array_intersect($this->permissionInput(), $allowed); return array_values(array_unique(array_merge($cleaned, [Permissions::WebsocketConnect->value]))); } /** * Validates that the permissions we are trying to assign can actually be assigned * by the user making the request. * * @param list $permissions unvalidated input, this runs before validation */ protected function validatePermissionsCanBeAssigned(array $permissions): void { // If we are a root admin or the server owner, no need to perform these checks. if ($this->requesterHasFullAccess()) { return; } $allowed = $this->requesterPermissions(); $target = $this->attributes->get('subuser'); if ($target instanceof Subuser) { $allowed = array_merge($allowed, $target->permissions); } throw_if(count(array_diff($permissions, $allowed)) > 0, HttpForbiddenException::class, 'Cannot assign permissions to a subuser that your account does not actively possess.'); } /** * Whether the requester is a root admin or the server owner, who may manage any subuser. */ protected function requesterHasFullAccess(): bool { $user = $this->user(); return $user->root_admin || $user->id === $this->parameter('server', Server::class)->owner_id; } /** * The permissions the requester holds on this server as a subuser. * * @return list */ protected function requesterPermissions(): array { return array_values($this->user()->permissionsOn($this->parameter('server', Server::class))); } /** @return list */ private function permissionInput(): array { $value = $this->input('permissions', []); if (! is_array($value) || ! array_is_list($value)) { return []; } $permissions = []; foreach ($value as $permission) { if (is_string($permission)) { $permissions[] = $permission; } } return $permissions; } }