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

186 lines
6.2 KiB
PHP
Raw Normal View History

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
*
* 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
*
* 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
*
* 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;
use DB;
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
{
/**
2016-12-07 22:46:38 +00:00
* Controller Constructor.
2016-01-18 06:24:33 +00:00
*
* @return void
*/
public function __construct()
{
//
}
public function getIndex(Request $request, $uuid)
{
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);
$server->js();
2016-01-18 06:24:33 +00:00
return view('server.users.index', [
'server' => $server,
'node' => $server->node,
2017-02-10 00:38:54 +00:00
'subusers' => $server->subusers,
2016-01-18 06:24:33 +00:00
]);
}
public function getView(Request $request, $uuid, $id)
{
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
$server->js();
2016-01-18 06:24:33 +00:00
return view('server.users.view', [
'server' => $server,
'node' => $server->node,
2016-01-18 06:24:33 +00:00
'subuser' => $subuser,
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
]);
}
public function postView(Request $request, $uuid, $id)
{
$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
]);
}
public function getNew(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
2016-01-19 00:57:10 +00:00
$this->authorize('create-subuser', $server);
$server->js();
2016-01-19 00:57:10 +00:00
return view('server.users.new', [
'server' => $server,
'node' => $server->node,
2016-01-19 00:57:10 +00:00
]);
}
public function postNew(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
2016-01-19 00:57:10 +00:00
$this->authorize('create-subuser', $server);
try {
$repo = new SubuserRepository;
$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();
}
public function deleteSubuser(Request $request, $uuid, $id)
{
$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
}
}