Skip to content

Commit 0fe9a45

Browse files
committed
Improve server and user model code to accept a specific user
1 parent 2770e6d commit 0fe9a45

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

app/Models/Server.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,32 @@ public static function byUuid($uuid, array $with = [], array $withCount = [])
150150
/**
151151
* Returns non-administrative headers for accessing a server on the daemon.
152152
*
153+
* @param Pterodactyl\Models\User|null $user
153154
* @return array
154155
*/
155-
public function guzzleHeaders()
156+
public function guzzleHeaders(User $user = null)
156157
{
158+
// If no specific user is passed, see if we can find an active
159+
// auth session to pull data from.
160+
if (is_null($user) && Auth::check()) {
161+
$user = Auth::user();
162+
}
163+
157164
return [
158165
'X-Access-Server' => $this->uuid,
159-
'X-Access-Token' => Auth::user()->daemonToken($this),
166+
'X-Access-Token' => ($user) ? $user->daemonToken($this) : $this->daemonSecret,
160167
];
161168
}
162169

163170
/**
164171
* Return an instance of the Guzzle client for this specific server using defined access token.
165172
*
173+
* @param Pterodactyl\Models\User|null $user
166174
* @return \GuzzleHttp\Client
167175
*/
168-
public function guzzleClient()
176+
public function guzzleClient(User $user = null)
169177
{
170-
return $this->node->guzzleClient($this->guzzleHeaders());
178+
return $this->node->guzzleClient($this->guzzleHeaders($user));
171179
}
172180

173181
/**

app/Models/User.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,9 @@ public function daemonToken(Server $server)
176176
return $server->daemonSecret;
177177
}
178178

179-
$subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first();
179+
$subuser = $this->subuserOf->where('server_id', $server->id)->first();
180180

181-
if (is_null($subuser)) {
182-
return null;
183-
}
184-
185-
return $subuser->daemonSecret;
181+
return ($subuser) ? $subuser->daemonSecret : null;
186182
}
187183

188184
/**
@@ -193,9 +189,9 @@ public function daemonToken(Server $server)
193189
*/
194190
public function serverAccessArray()
195191
{
196-
$union = Subuser::select('server_id')->where('user_id', $this->id);
197-
198-
return Server::select('id')->where('owner_id', $this->id)->union($union)->pluck('id')->all();
192+
return Server::select('id')->where('owner_id', $this->id)->union(
193+
Subuser::select('server_id')->where('user_id', $this->id)
194+
)->pluck('id')->all();
199195
}
200196

201197
/**

0 commit comments

Comments
 (0)