Final adjustments to Daemon <-> Panel communication change

This commit is contained in:
Dane Everitt 2017-09-24 21:12:30 -05:00
parent 8e2b77dc1e
commit 7d1c233c49
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
32 changed files with 528 additions and 538 deletions

View file

@ -25,12 +25,14 @@
namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Permission extends Model implements CleansAttributes
class Permission extends Model implements CleansAttributes, ValidableContract
{
use Eloquence;
use Eloquence, Validable;
/**
* Should timestamps be used on this model.
@ -62,6 +64,22 @@ class Permission extends Model implements CleansAttributes
'subuser_id' => 'integer',
];
/**
* @var array
*/
protected static $applicationRules = [
'subuser_id' => 'required',
'permission' => 'required',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'subuser_id' => 'numeric|min:1',
'permission' => 'string',
];
/**
* A list of all permissions available for a user.
*

View file

@ -58,6 +58,13 @@ class Server extends Model implements CleansAttributes, ValidableContract
*/
protected $dates = ['deleted_at'];
/**
* Always eager load these relationships on the model.
*
* @var array
*/
protected $with = ['key'];
/**
* Fields that are not mass assignable.
*
@ -286,7 +293,7 @@ class Server extends Model implements CleansAttributes, ValidableContract
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function ownerKey()
public function key()
{
return $this->hasOne(DaemonKey::class, 'user_id', 'owner_id');
}

View file

@ -104,4 +104,14 @@ class Subuser extends Model implements CleansAttributes, ValidableContract
{
return $this->hasMany(Permission::class);
}
/**
* Return the key that belongs to this subuser for the server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function key()
{
return $this->hasOne(DaemonKey::class, 'server_id', 'server_id')->where('daemon_keys.user_id', '=', $this->user_id);
}
}

View file

@ -24,14 +24,11 @@
namespace Pterodactyl\Models;
use Hash;
use Google2FA;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Pterodactyl\Exceptions\DisplayException;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
@ -69,7 +66,18 @@ class User extends Model implements
*
* @var array
*/
protected $fillable = ['username', 'email', 'name_first', 'name_last', 'password', 'language', 'use_totp', 'totp_secret', 'gravatar', 'root_admin'];
protected $fillable = [
'username',
'email',
'name_first',
'name_last',
'password',
'language',
'use_totp',
'totp_secret',
'gravatar',
'root_admin',
];
/**
* Cast values to correct type.
@ -144,46 +152,6 @@ class User extends Model implements
'totp_secret' => 'nullable|string',
];
/**
* Enables or disables TOTP on an account if the token is valid.
*
* @param int $token
* @return bool
* @deprecated
*/
public function toggleTotp($token)
{
if (! Google2FA::verifyKey($this->totp_secret, $token, 1)) {
return false;
}
$this->use_totp = ! $this->use_totp;
return $this->save();
}
/**
* Set a user password to a new value assuming it meets the following requirements:
* - 8 or more characters in length
* - at least one uppercase character
* - at least one lowercase character
* - at least one number.
*
* @param string $password
* @param string $regex
* @throws \Pterodactyl\Exceptions\DisplayException
* @deprecated
*/
public function setPassword($password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})')
{
if (! preg_match($regex, $password)) {
throw new DisplayException('The password passed did not meet the minimum password requirements.');
}
$this->password = Hash::make($password);
$this->save();
}
/**
* Send the password reset notification.
*
@ -194,102 +162,6 @@ class User extends Model implements
$this->notify(new ResetPasswordNotification($token));
}
/**
* Return true or false depending on wether the user is root admin or not.
*
* @return bool
* @deprecated
*/
public function isRootAdmin()
{
return $this->root_admin;
}
/**
* Returns the user's daemon secret for a given server.
*
* @param \Pterodactyl\Models\Server $server
* @return null|string
*/
public function daemonToken(Server $server)
{
if ($this->id === $server->owner_id || $this->isRootAdmin()) {
return $server->daemonSecret;
}
$subuser = $this->subuserOf->where('server_id', $server->id)->first();
return ($subuser) ? $subuser->daemonSecret : null;
}
/**
* 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()
{
return Server::select('id')->where('owner_id', $this->id)->union(
Subuser::select('server_id')->where('user_id', $this->id)
)->pluck('id')->all();
}
/**
* Change the access level for a given call to `access()` on the user.
*
* @param string $level can be all, admin, subuser, owner
* @return $this
*/
public function setAccessLevel($level = 'all')
{
if (! in_array($level, ['all', 'admin', 'subuser', 'owner'])) {
$level = 'all';
}
$this->accessLevel = $level;
return $this;
}
/**
* Returns an array of all servers a user is able to access.
* Note: does not account for user admin status.
*
* @param array $load
* @return \Pterodactyl\Models\Server
*/
public function access(...$load)
{
if (count($load) > 0 && is_null($load[0])) {
$query = Server::query();
} else {
$query = Server::with(! empty($load) ? $load : ['service', 'node', 'allocation']);
}
// If access level is set to owner, only display servers
// that the user owns.
if ($this->accessLevel === 'owner') {
$query->where('owner_id', $this->id);
}
// If set to all, display all servers they can access, including
// those they access as an admin.
//
// If set to subuser, only return the servers they can access because
// they are owner, or marked as a subuser of the server.
if (($this->accessLevel === 'all' && ! $this->isRootAdmin()) || $this->accessLevel === 'subuser') {
$query->whereIn('id', $this->serverAccessArray());
}
// If set to admin, only display the servers a user can access
// as an administrator (leaves out owned and subuser of).
if ($this->accessLevel === 'admin' && $this->isRootAdmin()) {
$query->whereNotIn('id', $this->serverAccessArray());
}
return $query;
}
/**
* Store the username as a lowecase string.
*
@ -339,4 +211,14 @@ class User extends Model implements
{
return $this->hasMany(Subuser::class);
}
/**
* Return all of the daemon keys that a user belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function keys()
{
return $this->hasMany(DaemonKey::class);
}
}