misc_pterodactyl-panel/app/Http/Controllers/Server/SubuserController.php

200 lines
7.1 KiB
PHP
Raw Normal View History

2016-01-18 06:24:33 +00:00
<?php
2016-12-07 22:46:38 +00:00
2016-01-18 06:24:33 +00:00
namespace Pterodactyl\Http\Controllers\Server;
use Illuminate\View\View;
2016-01-18 06:24:33 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2017-09-04 23:12:13 +00:00
use Pterodactyl\Models\Permission;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller;
2017-09-04 23:12:13 +00:00
use Pterodactyl\Services\Subusers\SubuserUpdateService;
use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Services\Subusers\SubuserCreationService;
use Pterodactyl\Services\Subusers\SubuserDeletionService;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest;
use Pterodactyl\Http\Requests\Server\Subuser\SubuserUpdateFormRequest;
2016-01-18 06:24:33 +00:00
class SubuserController extends Controller
{
2017-09-04 23:12:13 +00:00
use JavascriptInjection;
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Subusers\SubuserCreationService
*/
protected $subuserCreationService;
/**
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService
*/
protected $subuserDeletionService;
/**
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService
*/
protected $subuserUpdateService;
/**
* SubuserController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\Subusers\SubuserCreationService $subuserCreationService
* @param \Pterodactyl\Services\Subusers\SubuserDeletionService $subuserDeletionService
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $repository
* @param \Pterodactyl\Services\Subusers\SubuserUpdateService $subuserUpdateService
*/
public function __construct(
AlertsMessageBag $alert,
SubuserCreationService $subuserCreationService,
SubuserDeletionService $subuserDeletionService,
SubuserRepositoryInterface $repository,
SubuserUpdateService $subuserUpdateService
) {
$this->alert = $alert;
$this->repository = $repository;
$this->subuserCreationService = $subuserCreationService;
$this->subuserDeletionService = $subuserDeletionService;
$this->subuserUpdateService = $subuserUpdateService;
}
2016-01-18 06:24:33 +00:00
/**
2017-03-19 23:36:50 +00:00
* Displays the subuser overview index.
2016-01-18 06:24:33 +00:00
*
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
2017-09-04 23:12:13 +00:00
*
* @throws \Illuminate\Auth\Access\AuthorizationException
2016-01-18 06:24:33 +00:00
*/
public function index(Request $request): View
2016-01-18 06:24:33 +00:00
{
$server = $request->attributes->get('server');
2016-01-18 06:24:33 +00:00
$this->authorize('list-subusers', $server);
$this->setRequest($request)->injectJavascript();
2016-01-18 06:24:33 +00:00
return view('server.users.index', [
2017-09-04 23:12:13 +00:00
'subusers' => $this->repository->findWhere([['server_id', '=', $server->id]]),
2016-01-18 06:24:33 +00:00
]);
}
2017-03-19 23:36:50 +00:00
/**
* Displays a single subuser overview.
2017-03-19 23:36:50 +00:00
*
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
2017-09-04 23:12:13 +00:00
*
* @throws \Illuminate\Auth\Access\AuthorizationException
2017-03-19 23:36:50 +00:00
*/
public function view(Request $request): View
2016-01-18 06:24:33 +00:00
{
$server = $request->attributes->get('server');
2016-01-18 06:24:33 +00:00
$this->authorize('view-subuser', $server);
$subuser = $this->repository->getWithPermissions($request->attributes->get('subuser'));
$this->setRequest($request)->injectJavascript();
2016-01-18 06:24:33 +00:00
return view('server.users.view', [
'subuser' => $subuser,
2017-09-04 23:12:13 +00:00
'permlist' => Permission::getPermissions(),
'permissions' => $subuser->getRelation('permissions')->mapWithKeys(function ($item) {
2017-02-10 00:38:54 +00:00
return [$item->permission => true];
}),
2016-01-18 06:24:33 +00:00
]);
}
2017-03-19 23:36:50 +00:00
/**
* Handles editing a subuser.
*
* @param \Pterodactyl\Http\Requests\Server\Subuser\SubuserUpdateFormRequest $request
* @param string $uuid
* @param string $hash
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-09-04 23:12:13 +00:00
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-19 23:36:50 +00:00
*/
public function update(SubuserUpdateFormRequest $request, string $uuid, string $hash): RedirectResponse
2016-01-18 06:24:33 +00:00
{
$this->subuserUpdateService->handle($request->attributes->get('subuser'), $request->input('permissions'));
2017-09-04 23:12:13 +00:00
$this->alert->success(trans('server.users.user_updated'))->flash();
2016-12-07 22:46:38 +00:00
return redirect()->route('server.subusers.view', ['uuid' => $uuid, 'subuser' => $hash]);
2016-01-19 00:57:10 +00:00
}
2017-03-19 23:36:50 +00:00
/**
* Display new subuser creation page.
*
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
2017-09-04 23:12:13 +00:00
* @throws \Illuminate\Auth\Access\AuthorizationException
2017-03-19 23:36:50 +00:00
*/
public function create(Request $request): View
2016-01-19 00:57:10 +00:00
{
$server = $request->attributes->get('server');
2016-01-19 00:57:10 +00:00
$this->authorize('create-subuser', $server);
$this->setRequest($request)->injectJavascript();
2017-09-04 23:12:13 +00:00
return view('server.users.new', ['permissions' => Permission::getPermissions()]);
2016-01-19 00:57:10 +00:00
}
2017-03-19 23:36:50 +00:00
/**
* Handles creating a new subuser.
*
* @param \Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-09-04 23:12:13 +00:00
*
* @throws \Exception
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
2017-03-19 23:36:50 +00:00
*/
public function store(SubuserStoreFormRequest $request, $uuid): RedirectResponse
2016-01-19 00:57:10 +00:00
{
$server = $request->attributes->get('server');
2016-01-19 00:57:10 +00:00
$subuser = $this->subuserCreationService->handle($server, $request->input('email'), $request->input('permissions'));
2017-09-04 23:12:13 +00:00
$this->alert->success(trans('server.users.user_assigned'))->flash();
return redirect()->route('server.subusers.view', [
'uuid' => $uuid,
'id' => $subuser->id,
]);
2016-01-19 00:57:10 +00:00
}
2017-03-19 23:36:50 +00:00
/**
* Handles deleting a subuser.
*
* @param \Illuminate\Http\Request $request
2017-09-04 23:12:13 +00:00
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-19 23:36:50 +00:00
*/
public function delete(Request $request): Response
2016-01-19 00:57:10 +00:00
{
$server = $request->attributes->get('server');
2016-01-19 00:57:10 +00:00
$this->authorize('delete-subuser', $server);
$this->subuserDeletionService->handle($request->attributes->get('subuser'));
2017-09-04 23:12:13 +00:00
return response('', 204);
2016-01-18 06:24:33 +00:00
}
}