Improve server and user model code to accept a specific user

This commit is contained in:
Dane Everitt 2017-04-15 12:48:47 -04:00
parent 2770e6d1b4
commit 0fe9a4566e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 17 additions and 13 deletions

View file

@ -150,24 +150,32 @@ class Server extends Model
/**
* Returns non-administrative headers for accessing a server on the daemon.
*
* @param Pterodactyl\Models\User|null $user
* @return array
*/
public function guzzleHeaders()
public function guzzleHeaders(User $user = null)
{
// If no specific user is passed, see if we can find an active
// auth session to pull data from.
if (is_null($user) && Auth::check()) {
$user = Auth::user();
}
return [
'X-Access-Server' => $this->uuid,
'X-Access-Token' => Auth::user()->daemonToken($this),
'X-Access-Token' => ($user) ? $user->daemonToken($this) : $this->daemonSecret,
];
}
/**
* Return an instance of the Guzzle client for this specific server using defined access token.
*
* @param Pterodactyl\Models\User|null $user
* @return \GuzzleHttp\Client
*/
public function guzzleClient()
public function guzzleClient(User $user = null)
{
return $this->node->guzzleClient($this->guzzleHeaders());
return $this->node->guzzleClient($this->guzzleHeaders($user));
}
/**

View file

@ -176,13 +176,9 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
return $server->daemonSecret;
}
$subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first();
$subuser = $this->subuserOf->where('server_id', $server->id)->first();
if (is_null($subuser)) {
return null;
}
return $subuser->daemonSecret;
return ($subuser) ? $subuser->daemonSecret : null;
}
/**
@ -193,9 +189,9 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
*/
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();
return Server::select('id')->where('owner_id', $this->id)->union(
Subuser::select('server_id')->where('user_id', $this->id)
)->pluck('id')->all();
}
/**