Skip to content

Commit 820d2bf

Browse files
committed
Node and user API routes implemented.
More attempts at the logic for API permissions, most likely will need continued tweaking in the future, but base is there.
1 parent f24b238 commit 820d2bf

15 files changed

+447
-26
lines changed

app/Http/Controllers/API/Admin/NodeController.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
use Illuminate\Http\Request;
2929
use Pterodactyl\Models\Node;
3030
use Pterodactyl\Http\Controllers\Controller;
31+
use Pterodactyl\Exceptions\DisplayException;
32+
use Pterodactyl\Repositories\NodeRepository;
3133
use Pterodactyl\Transformers\Admin\NodeTransformer;
34+
use Pterodactyl\Exceptions\DisplayValidationException;
3235

3336
class NodeController extends Controller
3437
{
@@ -72,4 +75,93 @@ public function view(Request $request, $id)
7275
->withResourceName('node')
7376
->toArray();
7477
}
78+
79+
/**
80+
* Display information about a single node on the system.
81+
*
82+
* @param \Illuminate\Http\Request $request
83+
* @param int $id
84+
* @return \Illuminate\Http\JsonResponse
85+
*/
86+
public function viewConfig(Request $request, $id)
87+
{
88+
$this->authorize('node-view-config', $request->apiKey());
89+
90+
$node = Node::findOrFail($id);
91+
92+
return response()->json(json_decode($node->getConfigurationAsJson()));
93+
}
94+
95+
/**
96+
* Create a new node on the system.
97+
*
98+
* @param \Illuminate\Http\Request $request
99+
* @return \Illuminate\Http\JsonResponse|array
100+
*/
101+
public function store(Request $request)
102+
{
103+
$this->authorize('node-create', $request->apiKey());
104+
105+
$repo = new NodeRepository;
106+
try {
107+
$node = $repo->create(array_merge(
108+
$request->only([
109+
'public', 'disk_overallocate', 'memory_overallocate',
110+
]),
111+
$request->intersect([
112+
'name', 'location_id', 'fqdn',
113+
'scheme', 'memory', 'disk',
114+
'daemonBase', 'daemonSFTP', 'daemonListen',
115+
])
116+
));
117+
118+
$fractal = Fractal::create()->item($node)->transformWith(new NodeTransformer($request));
119+
if ($request->input('include')) {
120+
$fractal->parseIncludes(explode(',', $request->input('include')));
121+
}
122+
123+
return $fractal->withResourceName('node')->toArray();
124+
} catch (DisplayValidationException $ex) {
125+
return response()->json([
126+
'error' => json_decode($ex->getMessage()),
127+
], 400);
128+
} catch (DisplayException $ex) {
129+
return response()->json([
130+
'error' => $ex->getMessage(),
131+
], 400);
132+
} catch (\Exception $ex) {
133+
Log::error($ex);
134+
return response()->json([
135+
'error' => 'An unhandled exception occured while attemping to create this node. Please try again.',
136+
], 500);
137+
}
138+
}
139+
140+
/**
141+
* Delete a node from the system.
142+
*
143+
* @param \Illuminate\Http\Request $request
144+
* @param int $id
145+
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
146+
*/
147+
public function delete(Request $request, $id)
148+
{
149+
$this->authorize('node-delete', $request->apiKey());
150+
151+
$repo = new NodeRepository;
152+
try {
153+
$repo->delete($id);
154+
155+
return response('', 204);
156+
} catch (DisplayException $ex) {
157+
return response()->json([
158+
'error' => $ex->getMessage(),
159+
], 400);
160+
} catch (\Exception $ex) {
161+
Log::error($ex);
162+
return response()->json([
163+
'error' => 'An unhandled exception occured while attemping to delete this node. Please try again.',
164+
], 500);
165+
}
166+
}
75167
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Http\Controllers\API\Admin;
26+
27+
use Fractal;
28+
use Illuminate\Http\Request;
29+
use Pterodactyl\Models\User;
30+
use Pterodactyl\Http\Controllers\Controller;
31+
use Pterodactyl\Exceptions\DisplayException;
32+
use Pterodactyl\Repositories\UserRepository;
33+
use Pterodactyl\Transformers\Admin\UserTransformer;
34+
use Pterodactyl\Exceptions\DisplayValidationException;
35+
36+
class UserController extends Controller
37+
{
38+
/**
39+
* Controller to handle returning all users on the system.
40+
*
41+
* @param \Illuminate\Http\Request $request
42+
* @return array
43+
*/
44+
public function index(Request $request)
45+
{
46+
$this->authorize('user-list', $request->apiKey());
47+
48+
$fractal = Fractal::create()->collection(User::all());
49+
if ($request->input('include')) {
50+
$fractal->parseIncludes(explode(',', $request->input('include')));
51+
}
52+
53+
return $fractal->transformWith(new UserTransformer($request))
54+
->withResourceName('user')
55+
->toArray();
56+
}
57+
58+
/**
59+
* Display information about a single user on the system.
60+
*
61+
* @param \Illuminate\Http\Request $request
62+
* @param int $id
63+
* @return array
64+
*/
65+
public function view(Request $request, $id)
66+
{
67+
$this->authorize('user-view', $request->apiKey());
68+
69+
$fractal = Fractal::create()->item(User::findOrFail($id));
70+
if ($request->input('include')) {
71+
$fractal->parseIncludes(explode(',', $request->input('include')));
72+
}
73+
74+
return $fractal->transformWith(new UserTransformer($request))
75+
->withResourceName('user')
76+
->toArray();
77+
}
78+
79+
/**
80+
* Create a new user on the system.
81+
*
82+
* @param \Illuminate\Http\Request $request
83+
* @return \Illuminate\Http\JsonResponse|array
84+
*/
85+
public function store(Request $request)
86+
{
87+
$this->authorize('user-create', $request->apiKey());
88+
89+
$repo = new UserRepository;
90+
try {
91+
$user = $repo->create($request->only([
92+
'custom_id', 'email', 'password', 'name_first',
93+
'name_last', 'username', 'root_admin',
94+
]));
95+
96+
$fractal = Fractal::create()->item($user)->transformWith(new UserTransformer($request));
97+
if ($request->input('include')) {
98+
$fractal->parseIncludes(explode(',', $request->input('include')));
99+
}
100+
101+
return $fractal->withResourceName('user')->toArray();
102+
} catch (DisplayValidationException $ex) {
103+
return response()->json([
104+
'error' => json_decode($ex->getMessage()),
105+
], 400);
106+
} catch (\Exception $ex) {
107+
Log::error($ex);
108+
return response()->json([
109+
'error' => 'An unhandled exception occured while attemping to create this user. Please try again.',
110+
], 500);
111+
}
112+
}
113+
114+
/**
115+
* Update a user.
116+
*
117+
* @param \Illuminate\Http\Request $request
118+
* @param int $user
119+
* @return \Illuminate\Http\RedirectResponse
120+
*/
121+
public function update(Request $request, $user)
122+
{
123+
$this->authorize('user-edit', $request->apiKey());
124+
125+
$repo = new UserRepository;
126+
try {
127+
$user = $repo->update($user, $request->intersect([
128+
'email', 'password', 'name_first',
129+
'name_last', 'username', 'root_admin',
130+
]));
131+
132+
$fractal = Fractal::create()->item($user)->transformWith(new UserTransformer($request));
133+
if ($request->input('include')) {
134+
$fractal->parseIncludes(explode(',', $request->input('include')));
135+
}
136+
137+
return $fractal->withResourceName('user')->toArray();
138+
} catch (DisplayValidationException $ex) {
139+
return response()->json([
140+
'error' => json_decode($ex->getMessage()),
141+
], 400);
142+
} catch (\Exception $ex) {
143+
Log::error($ex);
144+
return response()->json([
145+
'error' => 'An unhandled exception occured while attemping to update this user. Please try again.',
146+
], 500);
147+
}
148+
}
149+
150+
/**
151+
* Delete a user from the system.
152+
*
153+
* @param \Illuminate\Http\Request $request
154+
* @param int $id
155+
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
156+
*/
157+
public function delete(Request $request, $id)
158+
{
159+
$this->authorize('user-delete', $request->apiKey());
160+
161+
$repo = new UserRepository;
162+
try {
163+
$repo->delete($id);
164+
165+
return response('', 204);
166+
} catch (DisplayException $ex) {
167+
return response()->json([
168+
'error' => $ex->getMessage(),
169+
], 400);
170+
} catch (\Exception $ex) {
171+
Log::error($ex);
172+
return response()->json([
173+
'error' => 'An unhandled exception occured while attemping to delete this user. Please try again.',
174+
], 500);
175+
}
176+
}
177+
}

app/Http/Controllers/Admin/UserController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,26 @@ public function postNew(Request $request)
136136
* Update a user.
137137
*
138138
* @param \Illuminate\Http\Request $request
139-
* @param int $user
139+
* @param int $id
140140
* @return \Illuminate\Http\RedirectResponse
141141
*/
142-
public function updateUser(Request $request, $user)
142+
public function updateUser(Request $request, $id)
143143
{
144144
try {
145145
$repo = new UserRepository;
146-
$repo->update($user, $request->only([
146+
$user = $repo->update($user, $request->intersect([
147147
'email', 'password', 'name_first',
148148
'name_last', 'username', 'root_admin',
149149
]));
150150
Alert::success('User account was successfully updated.')->flash();
151151
} catch (DisplayValidationException $ex) {
152-
return redirect()->route('admin.users.view', $user)->withErrors(json_decode($ex->getMessage()));
153-
} catch (\Exception $e) {
154-
Log::error($e);
152+
return redirect()->route('admin.users.view', $id)->withErrors(json_decode($ex->getMessage()));
153+
} catch (\Exception $ex) {
154+
Log::error($ex);
155155
Alert::danger('An error occured while attempting to update this user.')->flash();
156156
}
157157

158-
return redirect()->route('admin.users.view', $user);
158+
return redirect()->route('admin.users.view', $id);
159159
}
160160

161161
/**

app/Repositories/UserRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function create(array $data)
114114
*
115115
* @param int $id
116116
* @param array $data
117-
* @return bool
117+
* @return \Pterodactyl\Models\User
118118
*
119119
* @throws \Pterodactyl\Exceptions\DisplayValidationException
120120
*/
@@ -147,9 +147,9 @@ public function update($id, array $data)
147147
unset($data['password']);
148148
}
149149

150-
$user->fill($data);
150+
$user->fill($data)->save();;
151151

152-
return $user->save();
152+
return $user;
153153
}
154154

155155
/**

app/Transformers/Admin/LocationTransformer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public function transform(Location $location)
7979
*/
8080
public function includeServers(Location $location)
8181
{
82-
return $this->collection($location->servers, new ServerTransformer, 'server');
82+
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
83+
return;
84+
}
85+
86+
return $this->collection($location->servers, new ServerTransformer($this->request), 'server');
8387
}
8488

8589
/**
@@ -89,6 +93,10 @@ public function includeServers(Location $location)
8993
*/
9094
public function includeNodes(Location $location)
9195
{
92-
return $this->collection($location->nodes, new NodeTransformer, 'node');
96+
if ($this->request && ! $this->request->apiKeyHasPermission('location-list')) {
97+
return;
98+
}
99+
100+
return $this->collection($location->nodes, new NodeTransformer($this->request), 'node');
93101
}
94102
}

app/Transformers/Admin/NodeTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function includeAllocations(Node $node)
8484
return;
8585
}
8686

87-
return $this->collection($node->allocations, new AllocationTransformer, 'allocation');
87+
return $this->collection($node->allocations, new AllocationTransformer($this->request), 'allocation');
8888
}
8989

9090
/**
@@ -98,7 +98,7 @@ public function includeLocation(Node $node)
9898
return;
9999
}
100100

101-
return $this->item($node->location, new LocationTransformer, 'location');
101+
return $this->item($node->location, new LocationTransformer($this->request), 'location');
102102
}
103103

104104
/**
@@ -112,6 +112,6 @@ public function includeServers(Node $node)
112112
return;
113113
}
114114

115-
return $this->collection($node->servers, new ServerTransformer, 'server');
115+
return $this->collection($node->servers, new ServerTransformer($this->request), 'server');
116116
}
117117
}

0 commit comments

Comments
 (0)