Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/SubuserController.php
14056 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client\Servers;34use Illuminate\Http\Request;5use Pterodactyl\Models\Server;6use Illuminate\Http\JsonResponse;7use Pterodactyl\Facades\Activity;8use Pterodactyl\Models\Permission;9use Pterodactyl\Jobs\RevokeSftpAccessJob;10use Pterodactyl\Repositories\Eloquent\SubuserRepository;11use Pterodactyl\Services\Subusers\SubuserCreationService;12use Pterodactyl\Transformers\Api\Client\SubuserTransformer;13use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;14use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;15use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest;16use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\StoreSubuserRequest;17use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest;18use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest;1920class SubuserController extends ClientApiController21{22/**23* SubuserController constructor.24*/25public function __construct(26private SubuserRepository $repository,27private SubuserCreationService $creationService,28private DaemonRevocationRepository $revocationRepository,29) {30parent::__construct();31}3233/**34* Return the users associated with this server instance.35*/36public function index(GetSubuserRequest $request, Server $server): array37{38return $this->fractal->collection($server->subusers)39->transformWith($this->getTransformer(SubuserTransformer::class))40->toArray();41}4243/**44* Returns a single subuser associated with this server instance.45*/46public function view(GetSubuserRequest $request): array47{48$subuser = $request->attributes->get('subuser');4950return $this->fractal->item($subuser)51->transformWith($this->getTransformer(SubuserTransformer::class))52->toArray();53}5455/**56* Create a new subuser for the given server.57*58* @throws \Pterodactyl\Exceptions\Model\DataValidationException59* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException60* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException61* @throws \Throwable62*/63public function store(StoreSubuserRequest $request, Server $server): array64{65$response = $this->creationService->handle(66$server,67$request->input('email'),68$this->getDefaultPermissions($request)69);7071Activity::event('server:subuser.create')72->subject($response->user)73->property(['email' => $request->input('email'), 'permissions' => $this->getDefaultPermissions($request)])74->log();7576return $this->fractal->item($response)77->transformWith($this->getTransformer(SubuserTransformer::class))78->toArray();79}8081/**82* Update a given subuser in the system for the server.83*84* @throws \Pterodactyl\Exceptions\Model\DataValidationException85* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException86*/87public function update(UpdateSubuserRequest $request, Server $server): array88{89/** @var \Pterodactyl\Models\Subuser $subuser */90$subuser = $request->attributes->get('subuser');9192$permissions = $this->getDefaultPermissions($request);93$current = $subuser->permissions;9495sort($permissions);96sort($current);9798$log = Activity::event('server:subuser.update')99->subject($subuser->user)100->property([101'email' => $subuser->user->email,102'old' => $current,103'new' => $permissions,104'revoked' => true,105]);106107// Only update the database and hit up the Wings instance to invalidate JTI's if the permissions108// have actually changed for the user.109if ($permissions !== $current) {110$log->transaction(function () use ($request, $subuser, $server) {111$this->repository->update($subuser->id, [112'permissions' => $this->getDefaultPermissions($request),113]);114115RevokeSftpAccessJob::dispatch($subuser->user->uuid, $server);116});117}118119$log->reset();120121return $this->fractal->item($subuser->refresh())122->transformWith($this->getTransformer(SubuserTransformer::class))123->toArray();124}125126/**127* Removes a subusers from a server's assignment.128*/129public function delete(DeleteSubuserRequest $request, Server $server): JsonResponse130{131/** @var \Pterodactyl\Models\Subuser $subuser */132$subuser = $request->attributes->get('subuser');133134$log = Activity::event('server:subuser.delete')135->subject($subuser->user)136->property('email', $subuser->user->email)137->property('revoked', true);138139$log->transaction(function () use ($server, $subuser) {140$subuser->delete();141142RevokeSftpAccessJob::dispatch($subuser->user->uuid, $server);143});144145return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);146}147148/**149* Returns the default permissions for subusers and parses out any permissions150* that were passed that do not also exist in the internally tracked list of151* permissions.152*/153protected function getDefaultPermissions(Request $request): array154{155$allowed = Permission::permissions()156->map(function ($value, $prefix) {157return array_map(function ($value) use ($prefix) {158return "$prefix.$value";159}, array_keys($value['keys']));160})161->flatten()162->all();163164$cleaned = array_intersect($request->input('permissions') ?? [], $allowed);165166return array_unique(array_merge($cleaned, [Permission::ACTION_WEBSOCKET_CONNECT]));167}168}169170171