2020-03-27 16:57:49 -07:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2020-03-27 16:57:49 -07:00
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers ;
use Illuminate\Http\Request ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Http\ClientPermissionsRequest ;
use Pterodactyl\Enum\Permissions ;
2020-03-27 16:57:49 -07:00
use Pterodactyl\Exceptions\Http\HttpForbiddenException ;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
use Pterodactyl\Models\Subuser ;
use Pterodactyl\Models\User ;
use Pterodactyl\Services\Servers\SubuserPermissionCatalog ;
use UnexpectedValueException ;
2020-03-27 16:57:49 -07:00
2026-10-05 18:10:18 -04:00
abstract class SubuserRequest extends ClientApiRequest implements ClientPermissionsRequest
2020-03-27 16:57:49 -07:00
{
2026-10-05 18:10:18 -04:00
protected ? Subuser $model = null ;
2020-03-27 16:57:49 -07:00
/**
* Authorize the request and ensure that a user is not trying to modify themselves.
*/
public function authorize () : bool
{
2026-10-05 18:10:18 -04:00
if ( ! parent :: authorize ()) {
2020-03-27 16:57:49 -07:00
return false ;
}
2026-10-05 18:10:18 -04:00
$route = $this -> route ();
throw_if ( $route === null , UnexpectedValueException :: class , 'The subuser request does not have an active route.' );
$user = $route -> parameter ( 'user' );
2020-08-19 20:21:12 -07:00
// Don't allow a user to edit themselves on the server.
if ( $user instanceof User ) {
if ( $user -> uuid === $this -> user () -> uuid ) {
2020-03-27 16:57:49 -07: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' )) {
2026-10-05 18:10:18 -04:00
$this -> validatePermissionsCanBeAssigned ( $this -> permissionInput ());
2020-03-27 16:57:49 -07:00
}
return true ;
}
2026-10-05 18:10:18 -04:00
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
2026-10-11 08:00:46 -04:00
* extension knows about, always including websocket access. The API drops any other key
* it is sent, where the action would refuse it.
2026-10-05 18:10:18 -04:00
*
* @return list<string>
*/
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 ])));
}
2020-03-27 16:57:49 -07:00
/**
* Validates that the permissions we are trying to assign can actually be assigned
* by the user making the request.
*
2026-10-05 18:10:18 -04:00
* @param list<string> $permissions unvalidated input, this runs before validation
2020-03-27 16:57:49 -07:00
*/
2026-10-05 18:10:18 -04:00
protected function validatePermissionsCanBeAssigned ( array $permissions ) : void
2020-03-27 16:57:49 -07:00
{
// If we are a root admin or the server owner, no need to perform these checks.
2026-10-06 01:17:42 +01:00
if ( $this -> requesterHasFullAccess ()) {
2020-03-27 16:57:49 -07:00
return ;
}
2026-10-06 01:17:42 +01:00
$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<string>
*/
protected function requesterPermissions () : array
{
2026-10-12 00:21:31 -04:00
return array_values ( $this -> user () -> permissionsOn ( $this -> parameter ( 'server' , Server :: class )));
2026-10-05 18:10:18 -04:00
}
/** @return list<string> */
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 ;
}
2020-03-27 16:57:49 -07:00
}
2026-10-05 18:10:18 -04:00
return $permissions ;
2020-03-27 16:57:49 -07:00
}
}