2016-01-18 06:24:33 +00:00
|
|
|
<?php
|
2016-01-20 00:10:39 +00:00
|
|
|
/**
|
2016-01-20 21:05:16 +00:00
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2016-01-20 00:10:39 +00:00
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Server;
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
use Log;
|
2016-01-19 00:57:10 +00:00
|
|
|
use Auth;
|
2016-01-18 06:24:33 +00:00
|
|
|
use Alert;
|
|
|
|
use Pterodactyl\Models;
|
|
|
|
use Illuminate\Http\Request;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-01-18 06:24:33 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Repositories\SubuserRepository;
|
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
2016-01-18 06:24:33 +00:00
|
|
|
|
|
|
|
class SubuserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2017-03-19 23:36:50 +00:00
|
|
|
* Displays the subuser overview index.
|
2016-01-18 06:24:33 +00:00
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\View\View
|
2016-01-18 06:24:33 +00:00
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function index(Request $request, $uuid)
|
2016-01-18 06:24:33 +00:00
|
|
|
{
|
2017-02-10 00:38:54 +00:00
|
|
|
$server = Models\Server::byUuid($uuid)->load('subusers.user');
|
2016-01-18 06:24:33 +00:00
|
|
|
$this->authorize('list-subusers', $server);
|
2017-01-21 18:49:14 +00:00
|
|
|
|
2017-02-03 00:41:38 +00:00
|
|
|
$server->js();
|
2016-01-18 06:24:33 +00:00
|
|
|
|
|
|
|
return view('server.users.index', [
|
|
|
|
'server' => $server,
|
2017-02-03 00:41:38 +00:00
|
|
|
'node' => $server->node,
|
2017-02-10 00:38:54 +00:00
|
|
|
'subusers' => $server->subusers,
|
2016-01-18 06:24:33 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Displays the a single subuser overview.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
|
|
|
* @param int $id
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function view(Request $request, $uuid, $id)
|
2016-01-18 06:24:33 +00:00
|
|
|
{
|
2017-02-10 00:38:54 +00:00
|
|
|
$server = Models\Server::byUuid($uuid)->load('node');
|
2016-01-18 06:24:33 +00:00
|
|
|
$this->authorize('view-subuser', $server);
|
|
|
|
|
2017-02-10 00:38:54 +00:00
|
|
|
$subuser = Models\Subuser::with('permissions', 'user')
|
|
|
|
->where('server_id', $server->id)->findOrFail($id);
|
2016-01-18 06:24:33 +00:00
|
|
|
|
2017-02-03 00:41:38 +00:00
|
|
|
$server->js();
|
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
return view('server.users.view', [
|
|
|
|
'server' => $server,
|
2017-02-03 00:41:38 +00:00
|
|
|
'node' => $server->node,
|
2016-01-18 06:24:33 +00:00
|
|
|
'subuser' => $subuser,
|
2017-04-28 14:19:04 +00:00
|
|
|
'permlist' => Models\Permission::listPermissions(),
|
2017-02-10 00:38:54 +00:00
|
|
|
'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) {
|
|
|
|
return [$item->permission => true];
|
|
|
|
}),
|
2016-01-18 06:24:33 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Handles editing a subuser.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
|
|
|
* @param int $id
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function update(Request $request, $uuid, $id)
|
2016-01-18 06:24:33 +00:00
|
|
|
{
|
2017-02-03 00:41:38 +00:00
|
|
|
$server = Models\Server::byUuid($uuid);
|
2016-01-19 00:57:10 +00:00
|
|
|
$this->authorize('edit-subuser', $server);
|
|
|
|
|
2017-02-10 00:38:54 +00:00
|
|
|
$subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
try {
|
2017-02-10 00:38:54 +00:00
|
|
|
if ($subuser->user_id === Auth::user()->id) {
|
2016-01-19 00:57:10 +00:00
|
|
|
throw new DisplayException('You are not authorized to edit you own account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$repo = new SubuserRepository;
|
|
|
|
$repo->update($subuser->id, [
|
|
|
|
'permissions' => $request->input('permissions'),
|
|
|
|
'server' => $server->id,
|
2016-12-07 22:46:38 +00:00
|
|
|
'user' => $subuser->user_id,
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
Alert::success('Subuser permissions have successfully been updated.')->flash();
|
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('server.subusers.view', [
|
|
|
|
'uuid' => $uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'id' => $id,
|
2016-01-19 00:57:10 +00:00
|
|
|
])->withErrors(json_decode($ex->getMessage()));
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An unknown error occured while attempting to update this subuser.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return redirect()->route('server.subusers.view', [
|
|
|
|
'uuid' => $uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'id' => $id,
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Display new subuser creation page.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function create(Request $request, $uuid)
|
2016-01-19 00:57:10 +00:00
|
|
|
{
|
2017-02-03 00:41:38 +00:00
|
|
|
$server = Models\Server::byUuid($uuid);
|
2016-01-19 00:57:10 +00:00
|
|
|
$this->authorize('create-subuser', $server);
|
2017-02-03 00:41:38 +00:00
|
|
|
$server->js();
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
return view('server.users.new', [
|
|
|
|
'server' => $server,
|
2017-04-28 14:19:04 +00:00
|
|
|
'permissions' => Models\Permission::listPermissions(),
|
2017-02-03 00:41:38 +00:00
|
|
|
'node' => $server->node,
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Handles creating a new subuser.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function store(Request $request, $uuid)
|
2016-01-19 00:57:10 +00:00
|
|
|
{
|
2017-02-03 00:41:38 +00:00
|
|
|
$server = Models\Server::byUuid($uuid);
|
2016-01-19 00:57:10 +00:00
|
|
|
$this->authorize('create-subuser', $server);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$repo = new SubuserRepository;
|
2017-02-09 22:43:54 +00:00
|
|
|
$subuser = $repo->create($server->id, $request->only([
|
|
|
|
'permissions', 'email',
|
2016-01-19 00:57:10 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Successfully created new subuser.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return redirect()->route('server.subusers.view', [
|
|
|
|
'uuid' => $uuid,
|
2017-02-10 00:38:54 +00:00
|
|
|
'id' => $subuser->id,
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An unknown error occured while attempting to add a new subuser.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return redirect()->route('server.subusers.new', $uuid)->withInput();
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Handles deleting a subuser.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $uuid
|
|
|
|
* @param int $id
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
|
|
|
*/
|
2017-04-09 23:13:22 +00:00
|
|
|
public function delete(Request $request, $uuid, $id)
|
2016-01-19 00:57:10 +00:00
|
|
|
{
|
2017-02-03 00:41:38 +00:00
|
|
|
$server = Models\Server::byUuid($uuid);
|
2016-01-19 00:57:10 +00:00
|
|
|
$this->authorize('delete-subuser', $server);
|
|
|
|
|
|
|
|
try {
|
2017-02-10 00:38:54 +00:00
|
|
|
$subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
$repo = new SubuserRepository;
|
|
|
|
$repo->delete($subuser->id);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return response('', 204);
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
response()->json([
|
2016-12-07 22:46:38 +00:00
|
|
|
'error' => $ex->getMessage(),
|
2016-01-19 00:57:10 +00:00
|
|
|
], 422);
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
response()->json([
|
2016-12-07 22:46:38 +00:00
|
|
|
'error' => 'An unknown error occured while attempting to delete this subuser.',
|
2016-01-19 00:57:10 +00:00
|
|
|
], 503);
|
|
|
|
}
|
2016-01-18 06:24:33 +00:00
|
|
|
}
|
|
|
|
}
|