Skip to content

Commit ac6edc4

Browse files
committed
Completed subuser system
1 parent 251700b commit ac6edc4

File tree

16 files changed

+731
-15
lines changed

16 files changed

+731
-15
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DB_PASSWORD=secret
1010

1111
CACHE_DRIVER=file
1212
SESSION_DRIVER=file
13-
QUEUE_DRIVER=sync
13+
QUEUE_DRIVER=database
1414

1515
REDIS_HOST=localhost
1616
REDIS_PASSWORD=null

app/Http/Controllers/Server/SubuserController.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
namespace Pterodactyl\Http\Controllers\Server;
44

55
use DB;
6+
use Auth;
67
use Alert;
8+
use Log;
9+
710
use Pterodactyl\Models;
11+
use Pterodactyl\Repositories\SubuserRepository;
12+
13+
use Pterodactyl\Exceptions\DisplayException;
14+
use Pterodactyl\Exceptions\DisplayValidationException;
815

916
use Illuminate\Http\Request;
1017
use Pterodactyl\Http\Controllers\Controller;
@@ -71,7 +78,106 @@ public function getView(Request $request, $uuid, $id)
7178

7279
public function postView(Request $request, $uuid, $id)
7380
{
74-
//
81+
82+
$server = Models\Server::getByUUID($uuid);
83+
$this->authorize('edit-subuser', $server);
84+
85+
$subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
86+
87+
try {
88+
89+
if (!$subuser) {
90+
throw new DisplayException('Unable to locate a subuser by that ID.');
91+
} else if ($subuser->user_id === Auth::user()->id) {
92+
throw new DisplayException('You are not authorized to edit you own account.');
93+
}
94+
95+
$repo = new SubuserRepository;
96+
$repo->update($subuser->id, [
97+
'permissions' => $request->input('permissions'),
98+
'server' => $server->id,
99+
'user' => $subuser->user_id
100+
]);
101+
102+
Alert::success('Subuser permissions have successfully been updated.')->flash();
103+
} catch (DisplayValidationException $ex) {
104+
return redirect()->route('server.subusers.view', [
105+
'uuid' => $uuid,
106+
'id' => $id
107+
])->withErrors(json_decode($ex->getMessage()));
108+
} catch (DisplayException $ex) {
109+
Alert::danger($ex->getMessage())->flash();
110+
} catch (\Exception $ex) {
111+
Log::error($ex);
112+
Alert::danger('An unknown error occured while attempting to update this subuser.')->flash();
113+
}
114+
return redirect()->route('server.subusers.view', [
115+
'uuid' => $uuid,
116+
'id' => $id
117+
]);
118+
}
119+
120+
public function getNew(Request $request, $uuid)
121+
{
122+
$server = Models\Server::getByUUID($uuid);
123+
$this->authorize('create-subuser', $server);
124+
125+
return view('server.users.new', [
126+
'server' => $server,
127+
'node' => Models\Node::find($server->node)
128+
]);
129+
}
130+
131+
public function postNew(Request $request, $uuid)
132+
{
133+
$server = Models\Server::getByUUID($uuid);
134+
$this->authorize('create-subuser', $server);
135+
136+
try {
137+
$repo = new SubuserRepository;
138+
$id = $repo->create($server->id, $request->except([
139+
'_token'
140+
]));
141+
Alert::success('Successfully created new subuser.')->flash();
142+
return redirect()->route('server.subusers.view', [
143+
'uuid' => $uuid,
144+
'id' => md5($id)
145+
]);
146+
} catch (DisplayValidationException $ex) {
147+
return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
148+
} catch (DisplayException $ex) {
149+
Alert::danger($ex->getMessage())->flash();
150+
} catch (\Exception $ex) {
151+
Log::error($ex);
152+
Alert::danger('An unknown error occured while attempting to add a new subuser.')->flash();
153+
}
154+
return redirect()->route('server.subusers.new', $uuid)->withInput();
155+
}
156+
157+
public function deleteSubuser(Request $request, $uuid, $id)
158+
{
159+
$server = Models\Server::getByUUID($uuid);
160+
$this->authorize('delete-subuser', $server);
161+
162+
try {
163+
$subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
164+
if (!$subuser) {
165+
throw new DisplayException('No subuser by that ID was found on the system.');
166+
}
167+
168+
$repo = new SubuserRepository;
169+
$repo->delete($subuser->id);
170+
return response('', 204);
171+
} catch (DisplayException $ex) {
172+
response()->json([
173+
'error' => $ex->getMessage()
174+
], 422);
175+
} catch (\Exception $ex) {
176+
Log::error($ex);
177+
response()->json([
178+
'error' => 'An unknown error occured while attempting to delete this subuser.'
179+
], 503);
180+
}
75181
}
76182

77183
}

app/Http/Routes/AuthRoutes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function map(Router $router) {
3535

3636
// Show Password Reset Form
3737
$router->get('password', [
38+
'as' => 'auth.password',
3839
'uses' => 'Auth\PasswordController@getEmail'
3940
]);
4041

@@ -45,6 +46,7 @@ public function map(Router $router) {
4546

4647
// Show Verification Checkpoint
4748
$router->get('password/reset/{token}', [
49+
'as' => 'auth.reset',
4850
'uses' => 'Auth\PasswordController@getReset'
4951
]);
5052

app/Http/Routes/ServerRoutes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public function map(Router $router) {
5858
'uses' => 'Server\SubuserController@getIndex'
5959
]);
6060

61+
$router->get('users/new', [
62+
'as' => 'server.subusers.new',
63+
'uses' => 'Server\SubuserController@getNew'
64+
]);
65+
66+
$router->post('users/new', [
67+
'uses' => 'Server\SubuserController@postNew'
68+
]);
69+
6170
$router->get('users/view/{id}', [
6271
'as' => 'server.subusers.view',
6372
'uses' => 'Server\SubuserController@getView'
@@ -67,6 +76,10 @@ public function map(Router $router) {
6776
'uses' => 'Server\SubuserController@postView'
6877
]);
6978

79+
$router->delete('users/delete/{id}', [
80+
'uses' => 'Server\SubuserController@deleteSubuser'
81+
]);
82+
7083
// Assorted AJAX Routes
7184
$router->group(['prefix' => 'ajax'], function ($server) use ($router) {
7285
// Returns Server Status

app/Models/Permission.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class Permission extends Model
1414
*/
1515
protected $table = 'permissions';
1616

17+
/**
18+
* Fields that are not mass assignable.
19+
*
20+
* @var array
21+
*/
22+
protected $guarded = ['id', 'created_at', 'updated_at'];
23+
1724
public function scopePermission($query, $permission)
1825
{
1926
return $query->where('permission', $permission);

app/Models/Subuser.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class Subuser extends Model
1515
*/
1616
protected $table = 'subusers';
1717

18+
/**
19+
* The attributes excluded from the model's JSON form.
20+
*
21+
* @var array
22+
*/
23+
protected $hidden = ['daemonSecret'];
24+
25+
/**
26+
* Fields that are not mass assignable.
27+
*
28+
* @var array
29+
*/
30+
protected $guarded = ['id', 'created_at', 'updated_at'];
31+
1832
/**
1933
* @var mixed
2034
*/

0 commit comments

Comments
 (0)