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

167 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Server;
use Log;
2016-12-07 22:46:38 +00:00
use Alert;
use Illuminate\Http\Request;
2017-04-15 17:52:43 +00:00
use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
2017-04-24 20:56:38 +00:00
use Pterodactyl\Repositories\TaskRepository;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Exceptions\DisplayValidationException;
class TaskController extends Controller
{
2017-03-19 23:36:50 +00:00
/**
* Display task index 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 index(Request $request, $uuid)
{
2017-04-15 17:52:43 +00:00
$server = Server::byUuid($uuid)->load('tasks');
$this->authorize('list-tasks', $server);
$server->js();
2017-09-14 02:46:43 +00:00
return view('server.schedules.index', [
'server' => $server,
'node' => $server->node,
'tasks' => $server->tasks,
'actions' => [
2017-09-14 02:46:43 +00:00
'command' => trans('server.schedules.actions.command'),
'power' => trans('server.schedules.actions.power'),
2016-12-07 22:46:38 +00:00
],
]);
}
2017-03-19 23:36:50 +00:00
/**
* Display new task 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)
{
2017-04-15 17:52:43 +00:00
$server = Server::byUuid($uuid);
$this->authorize('create-task', $server);
$server->js();
2017-01-21 20:56:32 +00:00
2017-09-14 02:46:43 +00:00
return view('server.schedules.new', [
'server' => $server,
'node' => $server->node,
]);
}
2017-03-19 23:36:50 +00:00
/**
* Handle creation of new task.
*
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)
{
2017-04-15 17:52:43 +00:00
$server = Server::byUuid($uuid);
$this->authorize('create-task', $server);
2017-04-15 17:52:43 +00:00
$repo = new TaskRepository;
try {
2017-04-15 17:52:43 +00:00
$repo->create($server->id, $request->user()->id, $request->except([
2016-12-07 22:46:38 +00:00
'_token',
]));
2017-01-21 20:56:32 +00:00
2017-09-14 02:46:43 +00:00
return redirect()->route('server.schedules', $uuid);
} catch (DisplayValidationException $ex) {
2017-09-14 02:46:43 +00:00
return redirect()->route('server.schedules.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 create this task.')->flash();
}
2017-09-14 02:46:43 +00:00
return redirect()->route('server.schedules.new', $uuid);
}
2016-09-05 21:13:22 +00:00
2017-03-19 23:36:50 +00:00
/**
* Handle deletion of a task.
*
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
*/
2017-04-09 23:13:22 +00:00
public function delete(Request $request, $uuid, $id)
2016-09-05 21:13:22 +00:00
{
2017-04-15 17:52:43 +00:00
$server = Server::byUuid($uuid)->load('tasks');
2016-09-05 21:13:22 +00:00
$this->authorize('delete-task', $server);
$task = $server->tasks->where('id', $id)->first();
if (! $task) {
2016-09-05 21:13:22 +00:00
return response()->json([
2016-12-07 22:46:38 +00:00
'error' => 'No task by that ID was found associated with this server.',
2016-09-05 21:13:22 +00:00
], 404);
}
2017-04-15 17:52:43 +00:00
$repo = new TaskRepository;
2016-09-05 21:13:22 +00:00
try {
$repo->delete($id);
2016-12-07 22:46:38 +00:00
2016-09-05 21:13:22 +00:00
return response()->json([], 204);
} catch (\Exception $ex) {
Log::error($ex);
2016-12-07 22:46:38 +00:00
2016-09-05 21:13:22 +00:00
return response()->json([
2016-12-07 22:46:38 +00:00
'error' => 'A server error occured while attempting to delete this task.',
2016-09-05 21:13:22 +00:00
], 503);
}
}
2017-03-19 23:36:50 +00:00
/**
* Toggle the status of a task.
*
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
*/
2017-04-09 23:13:22 +00:00
public function toggle(Request $request, $uuid, $id)
2016-09-05 21:13:22 +00:00
{
2017-04-15 17:52:43 +00:00
$server = Server::byUuid($uuid)->load('tasks');
2016-09-05 21:13:22 +00:00
$this->authorize('toggle-task', $server);
$task = $server->tasks->where('id', $id)->first();
if (! $task) {
2016-09-05 21:13:22 +00:00
return response()->json([
2016-12-07 22:46:38 +00:00
'error' => 'No task by that ID was found associated with this server.',
2016-09-05 21:13:22 +00:00
], 404);
}
2017-04-15 17:52:43 +00:00
$repo = new TaskRepository;
2016-09-05 21:13:22 +00:00
try {
$resp = $repo->toggle($id);
2016-12-07 22:46:38 +00:00
2016-09-05 21:13:22 +00:00
return response()->json([
2016-12-07 22:46:38 +00:00
'status' => $resp,
2016-09-05 21:13:22 +00:00
]);
} catch (\Exception $ex) {
Log::error($ex);
2016-12-07 22:46:38 +00:00
2016-09-05 21:13:22 +00:00
return response()->json([
2016-12-07 22:46:38 +00:00
'error' => 'A server error occured while attempting to toggle this task.',
2016-09-05 21:13:22 +00:00
], 503);
}
}
}