1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-10-27 20:32:28 +01:00
Pterodactyl-Panel/app/Http/Controllers/Server/TaskController.php

167 lines
4.9 KiB
PHP
Raw Normal View History

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