mirror of
https://github.com/pterodactyl/panel.git
synced 2024-11-22 17:12:30 +01:00
Improves server model and cleans up model code calls.
This commit is contained in:
parent
b1389262e2
commit
02458c909d
@ -32,7 +32,7 @@ class InfoController extends BaseController
|
||||
{
|
||||
public function me(Request $request)
|
||||
{
|
||||
return Models\Server::getUserServers()->map(function ($server) {
|
||||
return $request->user()->serverAccessCollection()->map(function ($server) {
|
||||
return [
|
||||
'id' => $server->uuidShort,
|
||||
'uuid' => $server->uuid,
|
||||
|
@ -48,7 +48,7 @@ class IndexController extends Controller
|
||||
public function getIndex(Request $request)
|
||||
{
|
||||
return view('base.index', [
|
||||
'servers' => Server::getUserServers(10),
|
||||
'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -52,4 +52,15 @@ class Allocation extends Model
|
||||
'port' => 'integer',
|
||||
'server_id' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* Accessor to automatically provide the IP alias if defined.
|
||||
*
|
||||
* @param null|string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getAliasAttribute($value)
|
||||
{
|
||||
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
|
||||
}
|
||||
}
|
||||
|
@ -103,40 +103,6 @@ class Server extends Model
|
||||
self::$user = Auth::user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of all servers owned by the logged in user.
|
||||
* Returns all users servers if user is a root admin.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getUserServers($paginate = null)
|
||||
{
|
||||
$query = self::select(
|
||||
'servers.*',
|
||||
'nodes.name as nodeName',
|
||||
'locations.short as a_locationShort',
|
||||
'allocations.ip',
|
||||
'allocations.ip_alias',
|
||||
'allocations.port',
|
||||
'services.name as a_serviceName',
|
||||
'service_options.name as a_serviceOptionName'
|
||||
)->join('nodes', 'servers.node_id', '=', 'nodes.id')
|
||||
->join('locations', 'nodes.location_id', '=', 'locations.id')
|
||||
->join('services', 'servers.service_id', '=', 'services.id')
|
||||
->join('service_options', 'servers.option_id', '=', 'service_options.id')
|
||||
->join('allocations', 'servers.allocation_id', '=', 'allocations.id');
|
||||
|
||||
if (self::$user->root_admin !== 1) {
|
||||
$query->whereIn('servers.id', Subuser::accessServers());
|
||||
}
|
||||
|
||||
if (is_numeric($paginate)) {
|
||||
return $query->paginate($paginate);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single server specified by UUID.
|
||||
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
|
||||
@ -150,7 +116,7 @@ class Server extends Model
|
||||
$query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
|
||||
|
||||
if (! Auth::user()->isRootAdmin()) {
|
||||
$query->whereIn('id', Subuser::accessServers());
|
||||
$query->whereIn('id', Auth::user()->serverAccessArray());
|
||||
}
|
||||
|
||||
$result = $query->first();
|
||||
@ -228,6 +194,16 @@ class Server extends Model
|
||||
return $this->belongsTo(User::class, 'owner_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default allocation for a server.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
||||
*/
|
||||
public function allocation()
|
||||
{
|
||||
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all allocations associated with this server.
|
||||
*
|
||||
|
@ -55,33 +55,8 @@ class Subuser extends Model
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'server_id' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected static $user;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$user = Auth::user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of each server ID that the user has access to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function accessServers()
|
||||
{
|
||||
$union = self::select('server_id')->where('user_id', self::$user->id);
|
||||
|
||||
return Server::select('id')->where('owner', self::$user->id)->union($union)->pluck('id');
|
||||
}
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'server_id' => 'integer',
|
||||
];
|
||||
}
|
||||
|
@ -87,16 +87,6 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
|
||||
*/
|
||||
protected $hidden = ['password', 'remember_token', 'totp_secret'];
|
||||
|
||||
/**
|
||||
* Determines if a user has permissions.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function permissions()
|
||||
{
|
||||
return $this->hasMany(Permission::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables TOTP on an account if the token is valid.
|
||||
*
|
||||
@ -176,4 +166,43 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
|
||||
|
||||
return $subuser->daemonSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all permissions that a user has.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function permissions()
|
||||
{
|
||||
return $this->hasMany(Permission::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all servers a user is able to access.
|
||||
* Note: does not account for user admin status.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function serverAccessArray()
|
||||
{
|
||||
$union = Subuser::select('server_id')->where('user_id', $this->id);
|
||||
|
||||
return Server::select('id')->where('owner_id', $this->id)->union($union)->pluck('id')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all servers a user is able to access.
|
||||
* Note: does not account for user admin status.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function serverAccessCollection($paginate = null)
|
||||
{
|
||||
$query = Server::with('service', 'node');
|
||||
if (! $this->isRootAdmin()) {
|
||||
$query->whereIn('id', $this->serverAccessArray());
|
||||
}
|
||||
|
||||
return (is_numeric($paginate)) ? $query->paginate($paginate) : $query->get();
|
||||
}
|
||||
}
|
||||
|
@ -62,8 +62,8 @@
|
||||
<tr class="dynamic-update" data-server="{{ $server->uuidShort }}">
|
||||
<td><code>{{ $server->uuidShort }}</code></td>
|
||||
<td><a href="{{ route('server.index', $server->uuidShort) }}">{{ $server->name }}</a></td>
|
||||
<td>{{ $server->node_idName }}</td>
|
||||
<td><code>@if(!is_null($server->ip_alias)){{ $server->ip_alias }}@else{{ $server->ip }}@endif:{{ $server->port }}</code></td>
|
||||
<td>{{ $server->node->name }}</td>
|
||||
<td><code>{{ $server->allocation->alias }}:{{ $server->allocation->port }}</code></td>
|
||||
<td class="text-center hidden-sm hidden-xs"><span data-action="memory">--</span> / {{ $server->memory === 0 ? '∞' : $server->memory }} MB</td>
|
||||
<td class="text-center hidden-sm hidden-xs"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
|
||||
<td class="text-center" data-action="status">
|
||||
|
@ -245,7 +245,7 @@
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane active" id="control-sidebar-servers-tab">
|
||||
<ul class="control-sidebar-menu">
|
||||
@foreach (Pterodactyl\Models\Server::getUserServers() as $s)
|
||||
@foreach (Auth::user()->serverAccessCollection() as $s)
|
||||
<li>
|
||||
<a
|
||||
@if(isset($server) && isset($node))
|
||||
@ -254,7 +254,7 @@
|
||||
@endif
|
||||
@endif
|
||||
href="{{ route('server.index', $s->uuidShort) }}">
|
||||
@if($s->owner === Auth::user()->id)
|
||||
@if($s->owner_id === Auth::user()->id)
|
||||
<i class="menu-icon fa fa-user bg-blue"></i>
|
||||
@else
|
||||
<i class="menu-icon fa fa-user-o bg-gray"></i>
|
||||
|
Loading…
Reference in New Issue
Block a user