Improves server model and cleans up model code calls.

This commit is contained in:
Dane Everitt 2017-02-05 19:19:46 -05:00
parent b1389262e2
commit 02458c909d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
8 changed files with 71 additions and 80 deletions

View file

@ -32,7 +32,7 @@ class InfoController extends BaseController
{ {
public function me(Request $request) public function me(Request $request)
{ {
return Models\Server::getUserServers()->map(function ($server) { return $request->user()->serverAccessCollection()->map(function ($server) {
return [ return [
'id' => $server->uuidShort, 'id' => $server->uuidShort,
'uuid' => $server->uuid, 'uuid' => $server->uuid,

View file

@ -48,7 +48,7 @@ class IndexController extends Controller
public function getIndex(Request $request) public function getIndex(Request $request)
{ {
return view('base.index', [ return view('base.index', [
'servers' => Server::getUserServers(10), 'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'),
]); ]);
} }

View file

@ -52,4 +52,15 @@ class Allocation extends Model
'port' => 'integer', 'port' => 'integer',
'server_id' => '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;
}
} }

View file

@ -103,40 +103,6 @@ class Server extends Model
self::$user = Auth::user(); 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. * Returns a single server specified by UUID.
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS. * 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); $query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
if (! Auth::user()->isRootAdmin()) { if (! Auth::user()->isRootAdmin()) {
$query->whereIn('id', Subuser::accessServers()); $query->whereIn('id', Auth::user()->serverAccessArray());
} }
$result = $query->first(); $result = $query->first();
@ -228,6 +194,16 @@ class Server extends Model
return $this->belongsTo(User::class, 'owner_id'); 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. * Gets all allocations associated with this server.
* *

View file

@ -59,29 +59,4 @@ class Subuser extends Model
'user_id' => 'integer', 'user_id' => 'integer',
'server_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');
}
} }

View file

@ -87,16 +87,6 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
*/ */
protected $hidden = ['password', 'remember_token', 'totp_secret']; 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. * 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; 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();
}
} }

View file

@ -62,8 +62,8 @@
<tr class="dynamic-update" data-server="{{ $server->uuidShort }}"> <tr class="dynamic-update" data-server="{{ $server->uuidShort }}">
<td><code>{{ $server->uuidShort }}</code></td> <td><code>{{ $server->uuidShort }}</code></td>
<td><a href="{{ route('server.index', $server->uuidShort) }}">{{ $server->name }}</a></td> <td><a href="{{ route('server.index', $server->uuidShort) }}">{{ $server->name }}</a></td>
<td>{{ $server->node_idName }}</td> <td>{{ $server->node->name }}</td>
<td><code>@if(!is_null($server->ip_alias)){{ $server->ip_alias }}@else{{ $server->ip }}@endif:{{ $server->port }}</code></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 ? '&infin;' : $server->memory }} MB</td> <td class="text-center hidden-sm hidden-xs"><span data-action="memory">--</span> / {{ $server->memory === 0 ? '&infin;' : $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 hidden-sm hidden-xs"><span data-action="cpu" data-cpumax="{{ $server->cpu }}">--</span> %</td>
<td class="text-center" data-action="status"> <td class="text-center" data-action="status">

View file

@ -245,7 +245,7 @@
<div class="tab-content"> <div class="tab-content">
<div class="tab-pane active" id="control-sidebar-servers-tab"> <div class="tab-pane active" id="control-sidebar-servers-tab">
<ul class="control-sidebar-menu"> <ul class="control-sidebar-menu">
@foreach (Pterodactyl\Models\Server::getUserServers() as $s) @foreach (Auth::user()->serverAccessCollection() as $s)
<li> <li>
<a <a
@if(isset($server) && isset($node)) @if(isset($server) && isset($node))
@ -254,7 +254,7 @@
@endif @endif
@endif @endif
href="{{ route('server.index', $s->uuidShort) }}"> 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> <i class="menu-icon fa fa-user bg-blue"></i>
@else @else
<i class="menu-icon fa fa-user-o bg-gray"></i> <i class="menu-icon fa fa-user-o bg-gray"></i>