Improves server model and cleans up model code calls.
This commit is contained in:
parent
b1389262e2
commit
02458c909d
8 changed files with 71 additions and 80 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue