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
Pull Request — develop (#286)
by Dane
07:21 queued 04:33
created

Server::node()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Javascript;
29
use Illuminate\Database\Eloquent\Model;
30
use Illuminate\Notifications\Notifiable;
31
use Illuminate\Database\Eloquent\SoftDeletes;
32
33
class Server extends Model
34
{
35
    use Notifiable, SoftDeletes;
36
37
    /**
38
     * The table associated with the model.
39
     *
40
     * @var string
41
     */
42
    protected $table = 'servers';
43
44
    /**
45
     * The attributes excluded from the model's JSON form.
46
     *
47
     * @var array
48
     */
49
    protected $hidden = ['daemonSecret', 'sftp_password'];
50
51
    /**
52
     * The attributes that should be mutated to dates.
53
     *
54
     * @var array
55
     */
56
    protected $dates = ['deleted_at'];
57
58
    /**
59
     * Fields that are not mass assignable.
60
     *
61
     * @var array
62
     */
63
    protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
64
65
     /**
66
      * Cast values to correct type.
67
      *
68
      * @var array
69
      */
70
     protected $casts = [
71
         'node_id' => 'integer',
72
         'suspended' => 'integer',
73
         'owner_id' => 'integer',
74
         'memory' => 'integer',
75
         'swap' => 'integer',
76
         'disk' => 'integer',
77
         'io' => 'integer',
78
         'cpu' => 'integer',
79
         'oom_disabled' => 'integer',
80
         'allocation_id' => 'integer',
81
         'service_id' => 'integer',
82
         'option_id' => 'integer',
83
         'pack_id' => 'integer',
84
         'installed' => 'integer',
85
     ];
86
87
    /**
88
     * @var array
89
     */
90
    protected static $serverUUIDInstance = [];
91
92
    /**
93
     * @var mixed
94
     */
95
    protected static $user;
96
97
    /**
98
     * Constructor.
99
     */
100
    public function __construct()
101
    {
102
        parent::__construct();
103
        self::$user = Auth::user();
104
    }
105
106
    /**
107
     * Returns array of all servers owned by the logged in user.
108
     * Returns all users servers if user is a root admin.
109
     *
110
     * @return \Illuminate\Database\Eloquent\Collection
111
     */
112
    public static function getUserServers($paginate = null)
113
    {
114
        $query = self::select(
115
            'servers.*',
116
            'nodes.name as nodeName',
117
            'locations.short as a_locationShort',
118
            'allocations.ip',
119
            'allocations.ip_alias',
120
            'allocations.port',
121
            'services.name as a_serviceName',
122
            'service_options.name as a_serviceOptionName'
123
        )->join('nodes', 'servers.node_id', '=', 'nodes.id')
124
        ->join('locations', 'nodes.location', '=', 'locations.id')
125
        ->join('services', 'servers.service_id', '=', 'services.id')
126
        ->join('service_options', 'servers.option_id', '=', 'service_options.id')
127
        ->join('allocations', 'servers.allocation_id', '=', 'allocations.id');
128
129
        if (self::$user->root_admin !== 1) {
130
            $query->whereIn('servers.id', Subuser::accessServers());
131
        }
132
133
        if (is_numeric($paginate)) {
134
            return $query->paginate($paginate);
135
        }
136
137
        return $query->get();
138
    }
139
140
    /**
141
     * Returns a single server specified by UUID.
142
     * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
143
     * YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
144
     *
145
     * @param  string $uuid The Short-UUID of the server to return an object about.
146
     * @return \Illuminate\Database\Eloquent\Collection
147
     */
148
    public static function byUuid($uuid)
149
    {
150
        $query = self::with('service', 'node')->where('uuidShort', $uuid)->orWhere('uuid', $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...
151
152
        if (! Auth::user()->isRootAdmin()) {
153
            $query->whereIn('id', Subuser::accessServers());
154
        }
155
156
        $result = $query->first();
157
158
        if (! is_null($result)) {
159
            $result->daemonSecret = Auth::user()->daemonToken($result);
160
        }
161
162
        return $result;
163
    }
164
165
    /**
166
     * Returns non-administrative headers for accessing a server on the daemon.
167
     *
168
     * @param  string $uuid
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
169
     * @return array
170
     */
171
    public function guzzleHeaders()
172
    {
173
        return [
174
            'X-Access-Server' => $this->uuid,
0 ignored issues
show
Documentation introduced by
The property uuid does not exist on object<Pterodactyl\Models\Server>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
            'X-Access-Token' => Auth::user()->daemonToken($this),
176
        ];
177
    }
178
179
    /**
180
     * Return an instance of the Guzzle client for this specific server using defined access token.
181
     *
182
     * @return \GuzzleHttp\Client
183
     */
184
    public function guzzleClient()
185
    {
186
        return $this->node->guzzleClient($this->guzzleHeaders());
0 ignored issues
show
Documentation introduced by
The property node does not exist on object<Pterodactyl\Models\Server>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
187
    }
188
189
    /**
190
     * Returns javascript object to be embedded on server view pages with relevant information.
191
     *
192
     * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
193
     */
194
    public function js($additional = null, $overwrite = null)
195
    {
196
        $response = [
197
            'server' => collect($this->makeVisible('daemonSecret'))->only([
198
                'uuid',
199
                'uuidShort',
200
                'daemonSecret',
201
                'username',
202
            ]),
203
            'node' => collect($this->node)->only([
0 ignored issues
show
Documentation introduced by
The property node does not exist on object<Pterodactyl\Models\Server>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
204
                'fqdn',
205
                'scheme',
206
                'daemonListen',
207
            ]),
208
        ];
209
210
        if (is_array($additional)) {
211
            $response = array_merge($response, $additional);
212
        }
213
214
        if (is_array($overwrite)) {
215
            $response = $overwrite;
216
        }
217
218
        return Javascript::put($response);
219
    }
220
221
    /**
222
     * Gets all allocations associated with this server.
223
     *
224
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
225
     */
226
    public function allocations()
227
    {
228
        return $this->hasMany(Allocation::class, 'assigned_to');
229
    }
230
231
    /**
232
     * Gets information for the pack associated with this server.
233
     *
234
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
235
     */
236
    public function pack()
237
    {
238
        return $this->hasOne(ServicePack::class, 'id', 'pack_id');
239
    }
240
241
    /**
242
     * Gets information for the service associated with this server.
243
     *
244
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
245
     */
246
    public function service()
247
    {
248
        return $this->hasOne(Service::class, 'id', 'service_id');
249
    }
250
251
    /**
252
     * Gets information for the service option associated with this server.
253
     *
254
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
255
     */
256
    public function option()
257
    {
258
        return $this->hasOne(ServiceOptions::class, 'id', 'option_id');
259
    }
260
261
    /**
262
     * Gets information for the service variables associated with this server.
263
     *
264
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
265
     */
266
    public function variables()
267
    {
268
        return $this->hasMany(ServerVariables::class);
269
    }
270
271
    /**
272
     * Gets information for the node associated with this server.
273
     *
274
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
275
     */
276
    public function node()
277
    {
278
        return $this->hasOne(Node::class, 'id', 'node_id');
279
    }
280
281
    /**
282
     * Gets information for the tasks associated with this server.
283
     *
284
     * @TODO adjust server column in tasks to be server_id
285
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
286
     */
287
    public function tasks()
288
    {
289
        return $this->hasMany(Task::class, 'server', 'id');
290
    }
291
}
292