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

154 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\View\View;
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 Pterodactyl\Models\Model;
use Illuminate\Support\Collection;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\View\Factory as ViewFactory;
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;
2022-10-24 00:51:20 +00:00
use Pterodactyl\Http\Requests\Admin\NewUserFormRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller
{
2017-12-31 02:25:04 +00:00
use AvailableLanguages;
/**
* UserController constructor.
*/
public function __construct(
protected AlertsMessageBag $alert,
protected UserCreationService $creationService,
protected UserDeletionService $deletionService,
protected Translator $translator,
protected UserUpdateService $updateService,
protected UserRepositoryInterface $repository,
protected ViewFactory $view
) {
}
/**
2017-03-19 23:36:50 +00:00
* Display user index page.
*/
public function index(Request $request): View
{
$users = QueryBuilder::for(
User::query()->select('users.*')
->selectRaw('COUNT(DISTINCT(subusers.id)) as subuser_of_count')
->selectRaw('COUNT(DISTINCT(servers.id)) as servers_count')
->leftJoin('subusers', 'subusers.user_id', '=', 'users.id')
->leftJoin('servers', 'servers.owner_id', '=', 'users.id')
->groupBy('users.id')
)
->allowedFilters(['username', 'email', 'uuid'])
->allowedSorts(['id', 'uuid'])
->paginate(50);
2017-03-03 23:14:21 +00:00
return $this->view->make('admin.users.index', ['users' => $users]);
}
2017-03-19 23:36:50 +00:00
/**
* Display new user page.
*/
public function create(): View
{
return $this->view->make('admin.users.new', [
2017-12-31 02:25:04 +00:00
'languages' => $this->getAvailableLanguages(true),
]);
}
2017-03-19 23:36:50 +00:00
/**
* Display user view page.
*/
public function view(User $user): View
{
return $this->view->make('admin.users.view', [
2017-12-31 02:25:04 +00:00
'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
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\DisplayException
2017-03-19 23:36:50 +00:00
*/
public function delete(Request $request, User $user): RedirectResponse
{
if ($request->user()->id === $user->id) {
throw new DisplayException($this->translator->get('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.
*
* @throws \Exception
* @throws \Throwable
2017-03-19 23:36:50 +00:00
*/
2022-10-24 00:51:20 +00:00
public function store(NewUserFormRequest $request): RedirectResponse
{
$user = $this->creationService->handle($request->normalize());
2019-12-17 05:25:48 +00:00
$this->alert->success($this->translator->get('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
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-10-07 22:21:41 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-19 23:36:50 +00:00
*/
public function update(UserFormRequest $request, User $user): RedirectResponse
{
$this->updateService
->setUserLevel(User::USER_LEVEL_ADMIN)
->handle($user, $request->normalize());
$this->alert->success(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.
*/
public function json(Request $request): Model|Collection
{
$users = QueryBuilder::for(User::query())->allowedFilters(['email'])->paginate(25);
// Handle single user requests.
if ($request->query('user_id')) {
$user = User::query()->findOrFail($request->input('user_id'));
$user->md5 = md5(strtolower($user->email));
return $user;
}
return $users->map(function ($item) {
$item->md5 = md5(strtolower($item->email));
return $item;
});
}
}