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

221 lines
7.9 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 Javascript;
2016-01-18 06:24:33 +00:00
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)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('list-subusers', $server);
$node = Models\Node::find($server->node_id);
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
2016-01-18 06:24:33 +00:00
return view('server.users.index', [
'server' => $server,
'node' => $node,
2017-01-22 21:16:26 +00:00
'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp')
2016-01-18 06:24:33 +00:00
->join('users', 'users.id', '=', 'subusers.user_id')
->where('server_id', $server->id)
2016-12-07 22:46:38 +00:00
->get(),
2016-01-18 06:24:33 +00:00
]);
}
public function getView(Request $request, $uuid, $id)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('view-subuser', $server);
$node = Models\Node::find($server->node_id);
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
2016-01-18 06:24:33 +00:00
$subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail')
->join('users', 'users.id', '=', 'subusers.user_id')
->where(DB::raw('md5(subusers.id)'), $id)->where('subusers.server_id', $server->id)
->first();
2016-12-07 22:46:38 +00:00
if (! $subuser) {
2016-01-18 06:24:33 +00:00
abort(404);
}
$permissions = [];
$modelPermissions = Models\Permission::select('permission')
->where('user_id', $subuser->user_id)->where('server_id', $server->id)
->get();
2016-12-07 22:46:38 +00:00
foreach ($modelPermissions as &$perm) {
2016-01-18 06:24:33 +00:00
$permissions[$perm->permission] = true;
}
return view('server.users.view', [
'server' => $server,
'node' => $node,
2016-01-18 06:24:33 +00:00
'subuser' => $subuser,
'permissions' => $permissions,
]);
}
public function postView(Request $request, $uuid, $id)
{
2016-01-19 00:57:10 +00:00
$server = Models\Server::getByUUID($uuid);
$this->authorize('edit-subuser', $server);
$subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
try {
2016-12-07 22:46:38 +00:00
if (! $subuser) {
2016-01-19 00:57:10 +00:00
throw new DisplayException('Unable to locate a subuser by that ID.');
2016-12-07 22:46:38 +00:00
} elseif ($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::getByUUID($uuid);
$this->authorize('create-subuser', $server);
$node = Models\Node::find($server->node_id);
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
2016-01-19 00:57:10 +00:00
return view('server.users.new', [
'server' => $server,
'node' => $node,
2016-01-19 00:57:10 +00:00
]);
}
public function postNew(Request $request, $uuid)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('create-subuser', $server);
try {
$repo = new SubuserRepository;
$id = $repo->create($server->id, $request->except([
2016-12-07 22:46:38 +00:00
'_token',
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,
2016-12-07 22:46:38 +00:00
'id' => md5($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::getByUUID($uuid);
$this->authorize('delete-subuser', $server);
try {
$subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
2016-12-07 22:46:38 +00:00
if (! $subuser) {
2016-01-19 00:57:10 +00:00
throw new DisplayException('No subuser by that ID was found on the system.');
}
$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
}
}