Skip to content

Commit 02458c9

Browse files
committed
Improves server model and cleans up model code calls.
1 parent b138926 commit 02458c9

File tree

8 files changed

+71
-80
lines changed

8 files changed

+71
-80
lines changed

app/Http/Controllers/API/User/InfoController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class InfoController extends BaseController
3232
{
3333
public function me(Request $request)
3434
{
35-
return Models\Server::getUserServers()->map(function ($server) {
35+
return $request->user()->serverAccessCollection()->map(function ($server) {
3636
return [
3737
'id' => $server->uuidShort,
3838
'uuid' => $server->uuid,

app/Http/Controllers/Base/IndexController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct()
4848
public function getIndex(Request $request)
4949
{
5050
return view('base.index', [
51-
'servers' => Server::getUserServers(10),
51+
'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'),
5252
]);
5353
}
5454

app/Models/Allocation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ class Allocation extends Model
5252
'port' => 'integer',
5353
'server_id' => 'integer',
5454
];
55+
56+
/**
57+
* Accessor to automatically provide the IP alias if defined.
58+
*
59+
* @param null|string $value
60+
* @return string
61+
*/
62+
public function getAliasAttribute($value)
63+
{
64+
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
65+
}
5566
}

app/Models/Server.php

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,6 @@ public function __construct()
103103
self::$user = Auth::user();
104104
}
105105

106-
/**
107-
* Returns array of all servers owned by the logged in user.
108-
* Returns all users servers if user is a root admin.
109-
*
110-
* @return \Illuminate\Database\Eloquent\Collection
111-
*/
112-
public static function getUserServers($paginate = null)
113-
{
114-
$query = self::select(
115-
'servers.*',
116-
'nodes.name as nodeName',
117-
'locations.short as a_locationShort',
118-
'allocations.ip',
119-
'allocations.ip_alias',
120-
'allocations.port',
121-
'services.name as a_serviceName',
122-
'service_options.name as a_serviceOptionName'
123-
)->join('nodes', 'servers.node_id', '=', 'nodes.id')
124-
->join('locations', 'nodes.location_id', '=', 'locations.id')
125-
->join('services', 'servers.service_id', '=', 'services.id')
126-
->join('service_options', 'servers.option_id', '=', 'service_options.id')
127-
->join('allocations', 'servers.allocation_id', '=', 'allocations.id');
128-
129-
if (self::$user->root_admin !== 1) {
130-
$query->whereIn('servers.id', Subuser::accessServers());
131-
}
132-
133-
if (is_numeric($paginate)) {
134-
return $query->paginate($paginate);
135-
}
136-
137-
return $query->get();
138-
}
139-
140106
/**
141107
* Returns a single server specified by UUID.
142108
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
@@ -150,7 +116,7 @@ public static function byUuid($uuid)
150116
$query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
151117

152118
if (! Auth::user()->isRootAdmin()) {
153-
$query->whereIn('id', Subuser::accessServers());
119+
$query->whereIn('id', Auth::user()->serverAccessArray());
154120
}
155121

156122
$result = $query->first();
@@ -228,6 +194,16 @@ public function user()
228194
return $this->belongsTo(User::class, 'owner_id');
229195
}
230196

197+
/**
198+
* Gets the default allocation for a server.
199+
*
200+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
201+
*/
202+
public function allocation()
203+
{
204+
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
205+
}
206+
231207
/**
232208
* Gets all allocations associated with this server.
233209
*

app/Models/Subuser.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,8 @@ class Subuser extends Model
5555
*
5656
* @var array
5757
*/
58-
protected $casts = [
59-
'user_id' => 'integer',
60-
'server_id' => 'integer',
61-
];
62-
63-
/**
64-
* @var mixed
65-
*/
66-
protected static $user;
67-
68-
/**
69-
* Constructor.
70-
*/
71-
public function __construct()
72-
{
73-
self::$user = Auth::user();
74-
}
75-
76-
/**
77-
* Returns an array of each server ID that the user has access to.
78-
*
79-
* @return array
80-
*/
81-
public static function accessServers()
82-
{
83-
$union = self::select('server_id')->where('user_id', self::$user->id);
84-
85-
return Server::select('id')->where('owner', self::$user->id)->union($union)->pluck('id');
86-
}
58+
protected $casts = [
59+
'user_id' => 'integer',
60+
'server_id' => 'integer',
61+
];
8762
}

app/Models/User.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
8787
*/
8888
protected $hidden = ['password', 'remember_token', 'totp_secret'];
8989

90-
/**
91-
* Determines if a user has permissions.
92-
*
93-
* @return bool
94-
*/
95-
public function permissions()
96-
{
97-
return $this->hasMany(Permission::class);
98-
}
99-
10090
/**
10191
* Enables or disables TOTP on an account if the token is valid.
10292
*
@@ -176,4 +166,43 @@ public function daemonToken(Server $server)
176166

177167
return $subuser->daemonSecret;
178168
}
169+
170+
/**
171+
* Returns all permissions that a user has.
172+
*
173+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
174+
*/
175+
public function permissions()
176+
{
177+
return $this->hasMany(Permission::class);
178+
}
179+
180+
/**
181+
* Returns an array of all servers a user is able to access.
182+
* Note: does not account for user admin status.
183+
*
184+
* @return array
185+
*/
186+
public function serverAccessArray()
187+
{
188+
$union = Subuser::select('server_id')->where('user_id', $this->id);
189+
190+
return Server::select('id')->where('owner_id', $this->id)->union($union)->pluck('id')->all();
191+
}
192+
193+
/**
194+
* Returns an array of all servers a user is able to access.
195+
* Note: does not account for user admin status.
196+
*
197+
* @return Collection
198+
*/
199+
public function serverAccessCollection($paginate = null)
200+
{
201+
$query = Server::with('service', 'node');
202+
if (! $this->isRootAdmin()) {
203+
$query->whereIn('id', $this->serverAccessArray());
204+
}
205+
206+
return (is_numeric($paginate)) ? $query->paginate($paginate) : $query->get();
207+
}
179208
}

resources/themes/pterodactyl/base/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
<tr class="dynamic-update" data-server="{{ $server->uuidShort }}">
6363
<td><code>{{ $server->uuidShort }}</code></td>
6464
<td><a href="{{ route('server.index', $server->uuidShort) }}">{{ $server->name }}</a></td>
65-
<td>{{ $server->node_idName }}</td>
66-
<td><code>@if(!is_null($server->ip_alias)){{ $server->ip_alias }}@else{{ $server->ip }}@endif:{{ $server->port }}</code></td>
65+
<td>{{ $server->node->name }}</td>
66+
<td><code>{{ $server->allocation->alias }}:{{ $server->allocation->port }}</code></td>
6767
<td class="text-center hidden-sm hidden-xs"><span data-action="memory">--</span> / {{ $server->memory === 0 ? '&infin;' : $server->memory }} MB</td>
6868
<td class="text-center hidden-sm hidden-xs"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
6969
<td class="text-center" data-action="status">

resources/themes/pterodactyl/layouts/master.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class="active"
245245
<div class="tab-content">
246246
<div class="tab-pane active" id="control-sidebar-servers-tab">
247247
<ul class="control-sidebar-menu">
248-
@foreach (Pterodactyl\Models\Server::getUserServers() as $s)
248+
@foreach (Auth::user()->serverAccessCollection() as $s)
249249
<li>
250250
<a
251251
@if(isset($server) && isset($node))
@@ -254,7 +254,7 @@ class="active"
254254
@endif
255255
@endif
256256
href="{{ route('server.index', $s->uuidShort) }}">
257-
@if($s->owner === Auth::user()->id)
257+
@if($s->owner_id === Auth::user()->id)
258258
<i class="menu-icon fa fa-user bg-blue"></i>
259259
@else
260260
<i class="menu-icon fa fa-user-o bg-gray"></i>

0 commit comments

Comments
 (0)