1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Models; |
26
|
|
|
|
27
|
|
|
use Auth; |
28
|
|
|
use Cache; |
29
|
|
|
use Javascript; |
30
|
|
|
use Illuminate\Database\Eloquent\Model; |
31
|
|
|
use Illuminate\Notifications\Notifiable; |
32
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
33
|
|
|
|
34
|
|
|
class Server extends Model |
35
|
|
|
{ |
36
|
|
|
use Notifiable, SoftDeletes; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The table associated with the model. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $table = 'servers'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The attributes excluded from the model's JSON form. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $hidden = ['daemonSecret', 'sftp_password']; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The attributes that should be mutated to dates. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $dates = ['deleted_at']; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fields that are not mass assignable. |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at']; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Cast values to correct type. |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $casts = [ |
72
|
|
|
'node_id' => 'integer', |
73
|
|
|
'suspended' => 'integer', |
74
|
|
|
'owner_id' => 'integer', |
75
|
|
|
'memory' => 'integer', |
76
|
|
|
'swap' => 'integer', |
77
|
|
|
'disk' => 'integer', |
78
|
|
|
'io' => 'integer', |
79
|
|
|
'cpu' => 'integer', |
80
|
|
|
'oom_disabled' => 'integer', |
81
|
|
|
'allocation_id' => 'integer', |
82
|
|
|
'service_id' => 'integer', |
83
|
|
|
'option_id' => 'integer', |
84
|
|
|
'pack_id' => 'integer', |
85
|
|
|
'installed' => 'integer', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns a single server specified by UUID. |
90
|
|
|
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS. |
91
|
|
|
* YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS. |
92
|
|
|
* |
93
|
|
|
* @param string $uuid The Short-UUID of the server to return an object about. |
94
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
95
|
|
|
*/ |
96
|
|
|
public static function byUuid($uuid) |
97
|
|
|
{ |
98
|
|
|
// Results are cached because we call this functions a few times on page load. |
99
|
|
|
$result = Cache::remember('Server.byUuid.' . $uuid, 60, function () use ($uuid) { |
100
|
|
|
$query = self::with('service', 'node')->where(function ($q) use ($uuid) { |
|
|
|
|
101
|
|
|
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid); |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
if (! Auth::user()->isRootAdmin()) { |
105
|
|
|
$query->whereIn('id', Auth::user()->serverAccessArray()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $query->first(); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
if (! is_null($result)) { |
112
|
|
|
$result->daemonSecret = Auth::user()->daemonToken($result); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns non-administrative headers for accessing a server on the daemon. |
120
|
|
|
* |
121
|
|
|
* @param string $uuid |
|
|
|
|
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function guzzleHeaders() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
'X-Access-Server' => $this->uuid, |
|
|
|
|
128
|
|
|
'X-Access-Token' => Auth::user()->daemonToken($this), |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return an instance of the Guzzle client for this specific server using defined access token. |
134
|
|
|
* |
135
|
|
|
* @return \GuzzleHttp\Client |
136
|
|
|
*/ |
137
|
|
|
public function guzzleClient() |
138
|
|
|
{ |
139
|
|
|
return $this->node->guzzleClient($this->guzzleHeaders()); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns javascript object to be embedded on server view pages with relevant information. |
144
|
|
|
* |
145
|
|
|
* @return \Laracasts\Utilities\JavaScript\JavaScriptFacade |
146
|
|
|
*/ |
147
|
|
|
public function js($additional = null, $overwrite = null) |
148
|
|
|
{ |
149
|
|
|
$response = [ |
150
|
|
|
'server' => collect($this->makeVisible('daemonSecret'))->only([ |
151
|
|
|
'uuid', |
152
|
|
|
'uuidShort', |
153
|
|
|
'daemonSecret', |
154
|
|
|
'username', |
155
|
|
|
]), |
156
|
|
|
'node' => collect($this->node)->only([ |
|
|
|
|
157
|
|
|
'fqdn', |
158
|
|
|
'scheme', |
159
|
|
|
'daemonListen', |
160
|
|
|
]), |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
if (is_array($additional)) { |
164
|
|
|
$response = array_merge($response, $additional); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (is_array($overwrite)) { |
168
|
|
|
$response = $overwrite; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return Javascript::put($response); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets the user who owns the server. |
176
|
|
|
* |
177
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
178
|
|
|
*/ |
179
|
|
|
public function user() |
180
|
|
|
{ |
181
|
|
|
return $this->belongsTo(User::class, 'owner_id'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Gets the subusers associated with a server. |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
188
|
|
|
*/ |
189
|
|
|
public function subusers() |
190
|
|
|
{ |
191
|
|
|
return $this->hasMany(Subuser::class); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets the default allocation for a server. |
196
|
|
|
* |
197
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
198
|
|
|
*/ |
199
|
|
|
public function allocation() |
200
|
|
|
{ |
201
|
|
|
return $this->hasOne(Allocation::class, 'id', 'allocation_id'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Gets all allocations associated with this server. |
206
|
|
|
* |
207
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
208
|
|
|
*/ |
209
|
|
|
public function allocations() |
210
|
|
|
{ |
211
|
|
|
return $this->hasMany(Allocation::class, 'server_id'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets information for the pack associated with this server. |
216
|
|
|
* |
217
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
218
|
|
|
*/ |
219
|
|
|
public function pack() |
220
|
|
|
{ |
221
|
|
|
return $this->hasOne(ServicePack::class, 'id', 'pack_id'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets information for the service associated with this server. |
226
|
|
|
* |
227
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
228
|
|
|
*/ |
229
|
|
|
public function service() |
230
|
|
|
{ |
231
|
|
|
return $this->belongsTo(Service::class); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Gets information for the service option associated with this server. |
236
|
|
|
* |
237
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
238
|
|
|
*/ |
239
|
|
|
public function option() |
240
|
|
|
{ |
241
|
|
|
return $this->belongsTo(ServiceOption::class); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Gets information for the service variables associated with this server. |
246
|
|
|
* |
247
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
248
|
|
|
*/ |
249
|
|
|
public function variables() |
250
|
|
|
{ |
251
|
|
|
return $this->hasMany(ServerVariable::class); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Gets information for the node associated with this server. |
256
|
|
|
* |
257
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
258
|
|
|
*/ |
259
|
|
|
public function node() |
260
|
|
|
{ |
261
|
|
|
return $this->belongsTo(Node::class); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Gets information for the tasks associated with this server. |
266
|
|
|
* |
267
|
|
|
* @TODO adjust server column in tasks to be server_id |
268
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
269
|
|
|
*/ |
270
|
|
|
public function tasks() |
271
|
|
|
{ |
272
|
|
|
return $this->hasMany(Task::class, 'server', 'id'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Gets all databases associated with a server. |
277
|
|
|
* |
278
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
279
|
|
|
*/ |
280
|
|
|
public function databases() |
281
|
|
|
{ |
282
|
|
|
return $this->hasMany(Database::class); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: