2016-02-21 06:15:37 +00:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 06:15:37 +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;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2020-09-13 19:21:44 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2016-02-21 06:15:37 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Illuminate\Contracts\Translation\Translator;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
2017-12-31 02:25:04 +00:00
|
|
|
use Pterodactyl\Traits\Helpers\AvailableLanguages;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Users\UserCreationService;
|
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
2017-06-17 22:36:39 +00:00
|
|
|
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
|
2017-08-05 00:11:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2016-02-21 06:15:37 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-06-11 03:28:44 +00:00
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserCreationService
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
protected $creationService;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-06-17 22:36:39 +00:00
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserDeletionService
|
2017-06-17 22:36:39 +00:00
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
protected $deletionService;
|
2017-06-17 22:36:39 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Translation\Translator
|
|
|
|
*/
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserUpdateService
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
|
|
|
protected $updateService;
|
|
|
|
|
2017-06-11 03:28:44 +00:00
|
|
|
/**
|
|
|
|
* UserController constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Pterodactyl\Services\Users\UserCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Users\UserDeletionService $deletionService
|
|
|
|
* @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
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
2017-08-27 20:10:51 +00:00
|
|
|
UserCreationService $creationService,
|
|
|
|
UserDeletionService $deletionService,
|
2017-08-05 00:11:41 +00:00
|
|
|
Translator $translator,
|
2017-08-27 20:10:51 +00:00
|
|
|
UserUpdateService $updateService,
|
2017-08-05 00:11:41 +00:00
|
|
|
UserRepositoryInterface $repository
|
2017-07-01 20:29:49 +00:00
|
|
|
) {
|
2017-06-14 04:25:37 +00:00
|
|
|
$this->alert = $alert;
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->deletionService = $deletionService;
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository = $repository;
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->translator = $translator;
|
|
|
|
$this->updateService = $updateService;
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 06:15:37 +00:00
|
|
|
/**
|
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
|
2016-02-21 06:15:37 +00:00
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function index(Request $request)
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2020-09-23 03:13:54 +00:00
|
|
|
$users = QueryBuilder::for(
|
|
|
|
User::query()->select('users.*')
|
2020-10-11 01:06:42 +00:00
|
|
|
->selectRaw('COUNT(DISTINCT(subusers.id)) as subuser_of_count')
|
|
|
|
->selectRaw('COUNT(DISTINCT(servers.id)) as servers_count')
|
2020-09-23 03:13:54 +00:00
|
|
|
->leftJoin('subusers', 'subusers.user_id', '=', 'users.id')
|
|
|
|
->leftJoin('servers', 'servers.owner_id', '=', 'users.id')
|
|
|
|
->groupBy('users.id')
|
|
|
|
)
|
2020-09-13 19:26:15 +00:00
|
|
|
->allowedFilters(['username', 'email', 'uuid'])
|
2020-09-13 19:21:44 +00:00
|
|
|
->allowedSorts(['id', 'uuid'])
|
|
|
|
->paginate(50);
|
2017-03-03 23:14:21 +00:00
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
return view('admin.users.index', ['users' => $users]);
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Display new user page.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2017-06-11 03:28:44 +00:00
|
|
|
public function create()
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2017-12-31 02:25:04 +00:00
|
|
|
return view('admin.users.new', [
|
|
|
|
'languages' => $this->getAvailableLanguages(true),
|
|
|
|
]);
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2017-06-11 03:28:44 +00:00
|
|
|
public function view(User $user)
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2017-12-31 02:25:04 +00:00
|
|
|
return view('admin.users.view', [
|
|
|
|
'user' => $user,
|
|
|
|
'languages' => $this->getAvailableLanguages(true),
|
|
|
|
]);
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
2017-06-11 03:28:44 +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
|
2017-06-14 04:25:37 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2017-06-25 00:49:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-03-19 23:36:50 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
public function delete(Request $request, User $user)
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2017-06-25 00:49:09 +00:00
|
|
|
if ($request->user()->id === $user->id) {
|
2017-08-05 00:11:41 +00:00
|
|
|
throw new DisplayException($this->translator->trans('admin/user.exceptions.user_has_servers'));
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->deletionService->handle($user);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2017-10-09 20:13:52 +00:00
|
|
|
return redirect()->route('admin.users');
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-06-14 04:25:37 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Throwable
|
2017-03-19 23:36:50 +00:00
|
|
|
*/
|
2017-06-11 03:28:44 +00:00
|
|
|
public function store(UserFormRequest $request)
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2017-08-05 00:11:41 +00:00
|
|
|
$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();
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
return redirect()->route('admin.users.view', $user->id);
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
2017-06-11 03:28:44 +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
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2017-06-25 00:49:09 +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
|
|
|
*/
|
2017-06-11 03:28:44 +00:00
|
|
|
public function update(UserFormRequest $request, User $user)
|
2016-02-21 06:15:37 +00:00
|
|
|
{
|
2019-12-08 19:02:59 +00:00
|
|
|
$this->updateService
|
|
|
|
->setUserLevel(User::USER_LEVEL_ADMIN)
|
|
|
|
->handle($user, $request->normalize());
|
2017-12-03 20:00:47 +00:00
|
|
|
|
2019-12-08 19:02:59 +00:00
|
|
|
$this->alert->success(trans('admin/user.notices.account_updated'))->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2017-06-11 03:28:44 +00:00
|
|
|
return redirect()->route('admin.users.view', $user->id);
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-04-11 19:56:03 +00:00
|
|
|
* @return \Illuminate\Support\Collection|\Pterodactyl\Models\Model
|
2017-03-19 23:36:50 +00:00
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function json(Request $request)
|
2016-10-21 19:22:47 +00:00
|
|
|
{
|
2020-09-13 19:35:48 +00:00
|
|
|
$users = QueryBuilder::for(User::query())->allowedFilters(['email'])->paginate(25);
|
|
|
|
|
2020-04-11 19:56:03 +00:00
|
|
|
// Handle single user requests.
|
|
|
|
if ($request->query('user_id')) {
|
2020-09-13 19:35:48 +00:00
|
|
|
$user = User::query()->findOrFail($request->input('user_id'));
|
|
|
|
$user->md5 = md5(strtolower($user->email));
|
|
|
|
|
|
|
|
return $user;
|
2020-04-11 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 19:35:48 +00:00
|
|
|
return $users->map(function ($item) {
|
|
|
|
$item->md5 = md5(strtolower($item->email));
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
});
|
2016-10-21 19:22:47 +00:00
|
|
|
}
|
2016-02-21 06:15:37 +00:00
|
|
|
}
|