2019-11-03 20:20:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 23:26:28 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2020-04-19 18:58:26 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-11-04 05:01:15 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Services\Subusers\SubuserCreationService;
|
2020-11-04 05:01:15 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\SubuserTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2020-11-04 05:01:15 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\StoreSubuserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest;
|
2019-11-03 20:20:11 +00:00
|
|
|
|
|
|
|
class SubuserController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* SubuserController constructor.
|
|
|
|
*/
|
2020-03-27 21:23:13 +00:00
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private SubuserRepository $repository,
|
|
|
|
private SubuserCreationService $creationService,
|
|
|
|
private DaemonServerRepository $serverRepository
|
2020-03-27 21:23:13 +00:00
|
|
|
) {
|
2019-11-03 20:20:11 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the users associated with this server instance.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function index(GetSubuserRequest $request, Server $server): array
|
2019-11-03 20:20:11 +00:00
|
|
|
{
|
2019-12-28 20:03:19 +00:00
|
|
|
return $this->fractal->collection($server->subusers)
|
2019-11-03 20:20:11 +00:00
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2020-03-27 21:23:13 +00:00
|
|
|
|
2020-08-20 03:21:12 +00:00
|
|
|
/**
|
|
|
|
* Returns a single subuser associated with this server instance.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function view(GetSubuserRequest $request): array
|
2020-08-20 03:21:12 +00:00
|
|
|
{
|
|
|
|
$subuser = $request->attributes->get('subuser');
|
|
|
|
|
|
|
|
return $this->fractal->item($subuser)
|
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
/**
|
|
|
|
* Create a new subuser for the given server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function store(StoreSubuserRequest $request, Server $server): array
|
2020-03-27 21:23:13 +00:00
|
|
|
{
|
|
|
|
$response = $this->creationService->handle(
|
2021-01-23 20:33:34 +00:00
|
|
|
$server,
|
|
|
|
$request->input('email'),
|
|
|
|
$this->getDefaultPermissions($request)
|
2020-03-27 21:23:13 +00:00
|
|
|
);
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:subuser.create')
|
|
|
|
->subject($response->user)
|
|
|
|
->property(['email' => $request->input('email'), 'permissions' => $this->getDefaultPermissions($request)])
|
|
|
|
->log();
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
return $this->fractal->item($response)
|
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a given subuser in the system for the server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2020-11-04 05:01:15 +00:00
|
|
|
public function update(UpdateSubuserRequest $request, Server $server): array
|
2020-03-27 21:23:13 +00:00
|
|
|
{
|
2020-08-20 03:21:12 +00:00
|
|
|
/** @var \Pterodactyl\Models\Subuser $subuser */
|
|
|
|
$subuser = $request->attributes->get('subuser');
|
|
|
|
|
2020-11-04 05:01:15 +00:00
|
|
|
$permissions = $this->getDefaultPermissions($request);
|
|
|
|
$current = $subuser->permissions;
|
|
|
|
|
|
|
|
sort($permissions);
|
|
|
|
sort($current);
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
$log = Activity::event('server:subuser.update')
|
|
|
|
->subject($subuser->user)
|
|
|
|
->property([
|
|
|
|
'email' => $subuser->user->email,
|
|
|
|
'old' => $current,
|
|
|
|
'new' => $permissions,
|
|
|
|
'revoked' => true,
|
|
|
|
]);
|
|
|
|
|
2020-11-04 05:01:15 +00:00
|
|
|
// Only update the database and hit up the Wings instance to invalidate JTI's if the permissions
|
|
|
|
// have actually changed for the user.
|
|
|
|
if ($permissions !== $current) {
|
2022-05-29 23:26:28 +00:00
|
|
|
$log->transaction(function ($instance) use ($request, $subuser, $server) {
|
|
|
|
$this->repository->update($subuser->id, [
|
|
|
|
'permissions' => $this->getDefaultPermissions($request),
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
// Don't block this request if we can't connect to the Wings instance. Chances are it is
|
2022-10-14 16:59:20 +00:00
|
|
|
// offline and the token will be invalid once Wings boots back.
|
2022-05-29 23:26:28 +00:00
|
|
|
Log::warning($exception, ['user_id' => $subuser->user_id, 'server_id' => $server->id]);
|
|
|
|
|
|
|
|
$instance->property('revoked', false);
|
|
|
|
}
|
|
|
|
});
|
2020-11-04 05:01:15 +00:00
|
|
|
}
|
2020-03-27 21:23:13 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
$log->reset();
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
return $this->fractal->item($subuser->refresh())
|
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a subusers from a server's assignment.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function delete(DeleteSubuserRequest $request, Server $server): JsonResponse
|
2020-03-27 21:23:13 +00:00
|
|
|
{
|
2020-08-20 03:21:12 +00:00
|
|
|
/** @var \Pterodactyl\Models\Subuser $subuser */
|
|
|
|
$subuser = $request->attributes->get('subuser');
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
$log = Activity::event('server:subuser.delete')
|
|
|
|
->subject($subuser->user)
|
|
|
|
->property('email', $subuser->user->email)
|
|
|
|
->property('revoked', true);
|
2020-03-27 21:23:13 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
$log->transaction(function ($instance) use ($server, $subuser) {
|
|
|
|
$subuser->delete();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
// Don't block this request if we can't connect to the Wings instance.
|
|
|
|
Log::warning($exception, ['user_id' => $subuser->user_id, 'server_id' => $server->id]);
|
|
|
|
|
|
|
|
$instance->property('revoked', false);
|
|
|
|
}
|
|
|
|
});
|
2020-11-04 05:01:15 +00:00
|
|
|
|
2020-08-20 03:21:12 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2020-03-27 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-09 22:14:16 +00:00
|
|
|
* Returns the default permissions for subusers and parses out any permissions
|
|
|
|
* that were passed that do not also exist in the internally tracked list of
|
|
|
|
* permissions.
|
2020-03-27 21:23:13 +00:00
|
|
|
*/
|
|
|
|
protected function getDefaultPermissions(Request $request): array
|
|
|
|
{
|
2022-10-09 22:14:16 +00:00
|
|
|
$allowed = Permission::permissions()
|
|
|
|
->map(function ($value, $prefix) {
|
|
|
|
return array_map(function ($value) use ($prefix) {
|
|
|
|
return "$prefix.$value";
|
|
|
|
}, array_keys($value['keys']));
|
|
|
|
})
|
|
|
|
->flatten()
|
|
|
|
->all();
|
|
|
|
|
|
|
|
$cleaned = array_intersect($request->input('permissions') ?? [], $allowed);
|
|
|
|
|
|
|
|
return array_unique(array_merge($cleaned, [Permission::ACTION_WEBSOCKET_CONNECT]));
|
2020-03-27 21:23:13 +00:00
|
|
|
}
|
2019-11-03 20:20:11 +00:00
|
|
|
}
|