GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 91ce97...de923b )
by Dane
02:47
created

Server::downloads()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Carbon;
30
use Schema;
31
use Javascript;
32
use Illuminate\Database\Eloquent\Model;
33
use Illuminate\Notifications\Notifiable;
34
use Nicolaslopezj\Searchable\SearchableTrait;
35
36
class Server extends Model
37
{
38
    use Notifiable, SearchableTrait;
39
40
    /**
41
     * The table associated with the model.
42
     *
43
     * @var string
44
     */
45
    protected $table = 'servers';
46
47
    /**
48
     * The attributes excluded from the model's JSON form.
49
     *
50
     * @var array
51
     */
52
    protected $hidden = ['daemonSecret', 'sftp_password'];
53
54
    /**
55
     * The attributes that should be mutated to dates.
56
     *
57
     * @var array
58
     */
59
    protected $dates = ['deleted_at'];
60
61
    /**
62
     * Fields that are not mass assignable.
63
     *
64
     * @var array
65
     */
66
    protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
67
68
    /**
69
     * Cast values to correct type.
70
     *
71
     * @var array
72
     */
73
    protected $casts = [
74
        'node_id' => 'integer',
75
        'suspended' => 'integer',
76
        'owner_id' => 'integer',
77
        'memory' => 'integer',
78
        'swap' => 'integer',
79
        'disk' => 'integer',
80
        'io' => 'integer',
81
        'cpu' => 'integer',
82
        'oom_disabled' => 'integer',
83
        'allocation_id' => 'integer',
84
        'service_id' => 'integer',
85
        'option_id' => 'integer',
86
        'pack_id' => 'integer',
87
        'installed' => 'integer',
88
    ];
89
90
    /**
91
     * Parameters for search querying.
92
     *
93
     * @var array
94
     */
95
    protected $searchable = [
96
        'columns' => [
97
            'servers.name' => 10,
98
            'servers.username' => 10,
99
            'servers.uuidShort' => 9,
100
            'servers.uuid' => 8,
101
            'packs.name' => 7,
102
            'users.email' => 6,
103
            'users.username' => 6,
104
            'nodes.name' => 2,
105
        ],
106
        'joins' => [
107
            'packs' => ['packs.id', 'servers.pack_id'],
108
            'users' => ['users.id', 'servers.owner_id'],
109
            'nodes' => ['nodes.id', 'servers.node_id'],
110
        ],
111
    ];
112
113
    /**
114
     * Returns a single server specified by UUID.
115
     * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
116
     * YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
117
     *
118
     * @param  string  $uuid
119
     * @param  array   $with
120
     * @param  array   $withCount
121
     * @return \Pterodactyl\Models\Server
122
     * @todo   Remove $with and $withCount due to cache issues, they aren't used anyways.
123
     */
124
    public static function byUuid($uuid, array $with = [], array $withCount = [])
125
    {
126
        if (! Auth::check()) {
127
            throw new \Exception('You must call Server:byUuid as an authenticated user.');
128
        }
129
130
        // Results are cached because we call this functions a few times on page load.
131
        $result = Cache::tags(['Model:Server', 'Model:Server:byUuid:' . $uuid])->remember('Model:Server:byUuid:' . $uuid . Auth::user()->uuid, Carbon::now()->addMinutes(15), function () use ($uuid) {
132
            $query = self::with('service', 'node')->where(function ($q) use ($uuid) {
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
                $q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
134
            });
135
136
            if (! Auth::user()->isRootAdmin()) {
137
                $query->whereIn('id', Auth::user()->serverAccessArray());
138
            }
139
140
            return $query->first();
141
        });
142
143
        if (! is_null($result)) {
144
            $result->daemonSecret = Auth::user()->daemonToken($result);
145
        }
146
147
        return $result;
148
    }
149
150
    /**
151
     * Returns non-administrative headers for accessing a server on the daemon.
152
     *
153
     * @param  Pterodactyl\Models\User|null  $user
154
     * @return array
155
     */
156
    public function guzzleHeaders(User $user = null)
157
    {
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
164
        return [
165
            'X-Access-Server' => $this->uuid,
166
            'X-Access-Token' => ($user) ? $user->daemonToken($this) : $this->daemonSecret,
167
        ];
168
    }
169
170
    /**
171
     * Return an instance of the Guzzle client for this specific server using defined access token.
172
     *
173
     * @param  Pterodactyl\Models\User|null  $user
174
     * @return \GuzzleHttp\Client
175
     */
176
    public function guzzleClient(User $user = null)
177
    {
178
        return $this->node->guzzleClient($this->guzzleHeaders($user));
179
    }
180
181
    /**
182
     * Returns javascript object to be embedded on server view pages with relevant information.
183
     *
184
     * @param  array|null  $additional
185
     * @param  array|null  $overwrite
186
     * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
187
     */
188
    public function js($additional = null, $overwrite = null)
189
    {
190
        $response = [
191
            'server' => collect($this->makeVisible('daemonSecret'))->only([
192
                'uuid',
193
                'uuidShort',
194
                'daemonSecret',
195
                'username',
196
            ]),
197
            'node' => collect($this->node)->only([
198
                'fqdn',
199
                'scheme',
200
                'daemonListen',
201
            ]),
202
        ];
203
204
        if (is_array($additional)) {
205
            $response = array_merge($response, $additional);
206
        }
207
208
        if (is_array($overwrite)) {
209
            $response = $overwrite;
210
        }
211
212
        return Javascript::put($response);
213
    }
214
215
    /**
216
     * Return the columns available for this table.
217
     *
218
     * @return array
219
     */
220
    public function getTableColumns()
221
    {
222
        return Schema::getColumnListing($this->getTable());
223
    }
224
225
    /**
226
     * Gets the user who owns the server.
227
     *
228
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
229
     */
230
    public function user()
231
    {
232
        return $this->belongsTo(User::class, 'owner_id');
233
    }
234
235
    /**
236
     * Gets the subusers associated with a server.
237
     *
238
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
239
     */
240
    public function subusers()
241
    {
242
        return $this->hasMany(Subuser::class);
243
    }
244
245
    /**
246
     * Gets the default allocation for a server.
247
     *
248
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
249
     */
250
    public function allocation()
251
    {
252
        return $this->hasOne(Allocation::class, 'id', 'allocation_id');
253
    }
254
255
    /**
256
     * Gets all allocations associated with this server.
257
     *
258
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
259
     */
260
    public function allocations()
261
    {
262
        return $this->hasMany(Allocation::class, 'server_id');
263
    }
264
265
    /**
266
     * Gets information for the pack associated with this server.
267
     *
268
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
269
     */
270
    public function pack()
271
    {
272
        return $this->belongsTo(Pack::class);
273
    }
274
275
    /**
276
     * Gets information for the service associated with this server.
277
     *
278
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
279
     */
280
    public function service()
281
    {
282
        return $this->belongsTo(Service::class);
283
    }
284
285
    /**
286
     * Gets information for the service option associated with this server.
287
     *
288
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
289
     */
290
    public function option()
291
    {
292
        return $this->belongsTo(ServiceOption::class);
293
    }
294
295
    /**
296
     * Gets information for the service variables associated with this server.
297
     *
298
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
299
     */
300
    public function variables()
301
    {
302
        return $this->hasMany(ServerVariable::class);
303
    }
304
305
    /**
306
     * Gets information for the node associated with this server.
307
     *
308
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
309
     */
310
    public function node()
311
    {
312
        return $this->belongsTo(Node::class);
313
    }
314
315
    /**
316
     * Gets information for the tasks associated with this server.
317
     *
318
     * @TODO adjust server column in tasks to be server_id
319
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
320
     */
321
    public function tasks()
322
    {
323
        return $this->hasMany(Task::class);
324
    }
325
326
    /**
327
     * Gets all databases associated with a server.
328
     *
329
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
330
     */
331
    public function databases()
332
    {
333
        return $this->hasMany(Database::class);
334
    }
335
336
    /**
337
     * Gets all downloads associated with a server.
338
     *
339
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
340
     */
341
    public function downloads()
342
    {
343
        return $this->hasMany(Download::class, 'server', 'id');
344
    }
345
346
    /**
347
     * Gets the location of the server.
348
     *
349
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
350
     */
351
    public function location()
352
    {
353
        return $this->node->location();
354
    }
355
}
356