misc_pterodactyl-panel/app/Http/Controllers/Admin/UserController.php

204 lines
6.4 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Admin;
2016-12-07 22:46:38 +00:00
use Illuminate\Http\Request;
2017-06-17 22:36:39 +00:00
use Pterodactyl\Models\User;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
2017-08-31 02:14:20 +00:00
use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Services\Users\UserUpdateService;
2017-12-31 02:25:04 +00:00
use Pterodactyl\Traits\Helpers\AvailableLanguages;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Services\Users\UserDeletionService;
2017-06-17 22:36:39 +00:00
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller
{
2017-12-31 02:25:04 +00:00
use AvailableLanguages;
2017-06-17 22:36:39 +00:00
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Services\Users\UserCreationService
*/
protected $creationService;
2017-06-17 22:36:39 +00:00
/**
* @var \Pterodactyl\Services\Users\UserDeletionService
2017-06-17 22:36:39 +00:00
*/
protected $deletionService;
2017-06-17 22:36:39 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* @var \Pterodactyl\Services\Users\UserUpdateService
*/
protected $updateService;
/**
* UserController constructor.
*
2017-08-22 03:10:48 +00:00
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\Users\UserCreationService $creationService
* @param \Pterodactyl\Services\Users\UserDeletionService $deletionService
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Pterodactyl\Services\Users\UserUpdateService $updateService
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
*/
public function __construct(
AlertsMessageBag $alert,
UserCreationService $creationService,
UserDeletionService $deletionService,
Translator $translator,
UserUpdateService $updateService,
UserRepositoryInterface $repository
) {
$this->alert = $alert;
$this->creationService = $creationService;
$this->deletionService = $deletionService;
$this->repository = $repository;
$this->translator = $translator;
$this->updateService = $updateService;
}
/**
2017-03-19 23:36:50 +00:00
* Display user index page.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
2017-04-09 23:13:22 +00:00
public function index(Request $request)
{
$users = $this->repository->search($request->input('query'))->getAllUsersWithCounts();
2017-03-03 23:14:21 +00:00
return view('admin.users.index', ['users' => $users]);
}
2017-03-19 23:36:50 +00:00
/**
* Display new user page.
*
* @return \Illuminate\View\View
*/
public function create()
{
2017-12-31 02:25:04 +00:00
return view('admin.users.new', [
'languages' => $this->getAvailableLanguages(true),
]);
}
2017-03-19 23:36:50 +00:00
/**
* Display user view page.
*
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\User $user
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function view(User $user)
{
2017-12-31 02:25:04 +00:00
return view('admin.users.view', [
'user' => $user,
'languages' => $this->getAvailableLanguages(true),
]);
}
2017-03-19 23:36:50 +00:00
/**
* Delete a user from the system.
2017-03-19 23:36:50 +00:00
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\User $user
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\DisplayException
2017-03-19 23:36:50 +00:00
*/
public function delete(Request $request, User $user)
{
if ($request->user()->id === $user->id) {
throw new DisplayException($this->translator->trans('admin/user.exceptions.user_has_servers'));
}
$this->deletionService->handle($user);
2016-12-07 22:46:38 +00:00
return redirect()->route('admin.users');
}
2017-03-19 23:36:50 +00:00
/**
* Create a user.
*
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Http\Requests\Admin\UserFormRequest $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Throwable
2017-03-19 23:36:50 +00:00
*/
public function store(UserFormRequest $request)
{
$user = $this->creationService->handle($request->normalize());
$this->alert->success($this->translator->trans('admin/user.notices.account_created'))->flash();
return redirect()->route('admin.users.view', $user->id);
}
2017-03-19 23:36:50 +00:00
/**
* Update a user on the system.
2017-03-19 23:36:50 +00:00
*
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Http\Requests\Admin\UserFormRequest $request
* @param \Pterodactyl\Models\User $user
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-10-07 22:21:41 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
2017-03-19 23:36:50 +00:00
*/
public function update(UserFormRequest $request, User $user)
{
$this->updateService->setUserLevel(User::USER_LEVEL_ADMIN);
$data = $this->updateService->handle($user, $request->normalize());
if (! empty($data->get('exceptions'))) {
foreach ($data->get('exceptions') as $node => $exception) {
/** @var \GuzzleHttp\Exception\RequestException $exception */
/** @var \GuzzleHttp\Psr7\Response|null $response */
$response = method_exists($exception, 'getResponse') ? $exception->getResponse() : null;
$message = trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]);
$this->alert->danger(trans('exceptions.users.node_revocation_failed', [
'node' => $node,
'error' => $message,
'link' => route('admin.nodes.view', $node),
]))->flash();
}
}
$this->alert->success($this->translator->trans('admin/user.notices.account_updated'))->flash();
2016-12-07 22:46:38 +00:00
return redirect()->route('admin.users.view', $user->id);
}
2017-03-19 23:36:50 +00:00
/**
* Get a JSON response of users on the system.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Collection
2017-03-19 23:36:50 +00:00
*/
2017-04-09 23:13:22 +00:00
public function json(Request $request)
{
return $this->repository->filterUsersByQuery($request->input('q'));
}
}