|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\API; |
|
4
|
|
|
|
|
5
|
|
|
use Gameap\Helpers\PermissionHelper; |
|
6
|
|
|
use Gameap\Helpers\ServerPermissionHelper; |
|
7
|
|
|
use Gameap\Http\Controllers\AuthController; |
|
|
|
|
|
|
8
|
|
|
use Gameap\Models\GdaemonTask; |
|
9
|
|
|
use Gameap\Models\User; |
|
10
|
|
|
use Gameap\Repositories\GdaemonTaskRepository; |
|
11
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
|
12
|
|
|
use Illuminate\Http\Response; |
|
13
|
|
|
use Spatie\QueryBuilder\QueryBuilder; |
|
14
|
|
|
|
|
15
|
|
|
class GdaemonTasksController extends AuthController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The GdaemonTaskRepository instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \Gameap\Repositories\GdaemonTaskRepository |
|
21
|
|
|
*/ |
|
22
|
|
|
public $repository; |
|
23
|
|
|
|
|
24
|
|
|
/** @var AuthFactory */ |
|
25
|
|
|
protected $authFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* GdaemonTasksController constructor. |
|
29
|
|
|
* @param GdaemonTaskRepository $repository |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(GdaemonTaskRepository $repository, AuthFactory $auth) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->repository = $repository; |
|
36
|
|
|
$this->authFactory = $auth; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param GdaemonTask $gdaemonTask |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get(GdaemonTask $gdaemonTask) |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var User $currentUser */ |
|
46
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
|
47
|
|
|
|
|
48
|
|
|
if (!$currentUser->can(PermissionHelper::ADMIN_PERMISSIONS)) { |
|
49
|
|
|
if (empty($gdaemonTask->server)) { |
|
50
|
|
|
abort(Response::HTTP_FORBIDDEN); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $gdaemonTask->server); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return [ |
|
57
|
|
|
'id' => $gdaemonTask->id, |
|
58
|
|
|
'run_aft_id' => $gdaemonTask->id, |
|
59
|
|
|
'created_at' => $gdaemonTask->created_at->toDateTimeString(), |
|
60
|
|
|
'updated_at' => $gdaemonTask->updated_at->toDateTimeString(), |
|
61
|
|
|
'server_id' => $gdaemonTask->server_id, |
|
62
|
|
|
'task' => $gdaemonTask->task, |
|
63
|
|
|
'status' => $gdaemonTask->status, |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function list() |
|
68
|
|
|
{ |
|
69
|
|
|
return QueryBuilder::for( |
|
70
|
|
|
GdaemonTask::select('id','created_at', 'updated_at', 'dedicated_server_id', 'server_id', 'task', 'status', 'cmd') |
|
|
|
|
|
|
71
|
|
|
) |
|
72
|
|
|
->allowedSorts(['created_at', 'id']) |
|
73
|
|
|
->allowedFilters(['status', 'dedicated_server_id', 'server_id', 'task', 'status']) |
|
74
|
|
|
->jsonPaginate(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param GdaemonTask $gdaemonTask |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function output(GdaemonTask $gdaemonTask) |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
'id' => $gdaemonTask->id, |
|
85
|
|
|
'dedicated_server_id' => $gdaemonTask->dedicated_server_id, |
|
86
|
|
|
'server_id' => $gdaemonTask->server_id, |
|
87
|
|
|
'task' => $gdaemonTask->task, |
|
88
|
|
|
'created_at' => $gdaemonTask->created_at, |
|
89
|
|
|
'updated_at' => $gdaemonTask->updated_at, |
|
90
|
|
|
'output' => $gdaemonTask->output, |
|
91
|
|
|
'status' => $gdaemonTask->status, |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: