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 (#206)
by
unknown
02:27
created

ServerPolicy::compressFiles()   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 2
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2016 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\Policies;
26
27
use Pterodactyl\Models\User;
28
use Pterodactyl\Models\Server;
29
30
class ServerPolicy
31
{
32
    /**
33
     * Create a new policy instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        //
40
    }
41
42
    /**
43
     * Determine if current user is the owner of a server.
44
     *
45
     * @param  \Pterodactyl\Models\User    $user
46
     * @param  \Pterodactyl\Models\Server  $server
47
     * @return bool
48
     */
49
    protected function isOwner(User $user, Server $server)
50
    {
51
        return $server->owner === $user->id;
0 ignored issues
show
Documentation introduced by
The property owner 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...
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\User>. 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...
52
    }
53
54
    /**
55
     * Runs before any of the functions are called. Used to determine if user is root admin, if so, ignore permissions.
56
     *
57
     * @param  \Pterodactyl\Models\User $user
58
     * @param  string $ability
59
     * @return bool
60
     */
61
    public function before(User $user, $ability)
0 ignored issues
show
Unused Code introduced by
The parameter $ability is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        if ($user->root_admin === 1) {
0 ignored issues
show
Documentation introduced by
The property root_admin does not exist on object<Pterodactyl\Models\User>. 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...
64
            return true;
65
        }
66
    }
67
68
    /**
69
     * Check if user has permission to control power for a server.
70
     *
71
     * @param  \Pterodactyl\Models\User   $user
72
     * @param  \Pterodactyl\Models\Server $server
73
     * @return bool
74
     */
75
    public function power(User $user, Server $server)
76
    {
77
        return $this->checkPermission($user, $server, 'power');
78
    }
79
80
    /**
81
     * Check if user has permission to start a server.
82
     *
83
     * @param  \Pterodactyl\Models\User   $user
84
     * @param  \Pterodactyl\Models\Server $server
85
     * @return bool
86
     */
87
    public function powerStart(User $user, Server $server)
88
    {
89
        return $this->heckPermission($user, $server, 'power-start');
0 ignored issues
show
Bug introduced by
The method heckPermission() does not exist on Pterodactyl\Policies\ServerPolicy. Did you maybe mean checkPermission()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
    }
91
92
    /**
93
     * Check if user has permission to stop a server.
94
     *
95
     * @param  \Pterodactyl\Models\User   $user
96
     * @param  \Pterodactyl\Models\Server $server
97
     * @return bool
98
     */
99
    public function powerStop(User $user, Server $server)
100
    {
101
        return $this->checkPermission($user, $server, 'power-stop');
102
    }
103
104
    /**
105
     * Check if user has permission to restart a server.
106
     *
107
     * @param  \Pterodactyl\Models\User   $user
108
     * @param  \Pterodactyl\Models\Server $server
109
     * @return bool
110
     */
111
    public function powerRestart(User $user, Server $server)
112
    {
113
        return $this->checkPermission($user, $server, 'power-restart');
114
    }
115
116
    /**
117
     * Check if user has permission to kill a server.
118
     *
119
     * @param  \Pterodactyl\Models\User   $user
120
     * @param  \Pterodactyl\Models\Server $server
121
     * @return bool
122
     */
123
    public function powerKill(User $user, Server $server)
124
    {
125
        return $this->checkPermission($user, $server, 'power-kill');
126
    }
127
128
    /**
129
     * Check if user has permission to run a command on a server.
130
     *
131
     * @param  \Pterodactyl\Models\User   $user
132
     * @param  \Pterodactyl\Models\Server $server
133
     * @return bool
134
     */
135
    public function sendCommand(User $user, Server $server)
136
    {
137
        return $this->checkPermission($user, $server, 'send-command');
138
    }
139
140
    /**
141
     * Check if user has permission to list files on a server.
142
     *
143
     * @param  \Pterodactyl\Models\User   $user
144
     * @param  \Pterodactyl\Models\Server $server
145
     * @return bool
146
     */
147
    public function listFiles(User $user, Server $server)
148
    {
149
        return $this->checkPermission($user, $server, 'list-files');
150
    }
151
152
    /**
153
     * Check if user has permission to edit files on a server.
154
     *
155
     * @param  \Pterodactyl\Models\User   $user
156
     * @param  \Pterodactyl\Models\Server $server
157
     * @return bool
158
     */
159
    public function editFiles(User $user, Server $server)
160
    {
161
        return $this->checkPermission($user, $server, 'edit-files');
162
    }
163
164
    /**
165
     * Check if user has permission to save files on a server.
166
     *
167
     * @param  \Pterodactyl\Models\User   $user
168
     * @param  \Pterodactyl\Models\Server $server
169
     * @return bool
170
     */
171
    public function saveFiles(User $user, Server $server)
172
    {
173
        return $this->checkPermission($user, $server, 'save-files');
174
    }
175
176
    /**
177
     * Check if user has permission to move and rename files and folders on a server.
178
     *
179
     * @param  \Pterodactyl\Models\User   $user
180
     * @param  \Pterodactyl\Models\Server $server
181
     * @return bool
182
     */
183
    public function moveFiles(User $user, Server $server)
184
    {
185
        return $this->checkPermission($user, $server, 'move-files');
186
    }
187
188
    /**
189
     * Check if user has permission to copy folders and files on a server.
190
     *
191
     * @param  \Pterodactyl\Models\User   $user
192
     * @param  \Pterodactyl\Models\Server $server
193
     * @return bool
194
     */
195
    public function copyFiles(User $user, Server $server)
196
    {
197
        return $this->checkPermission($user, $server, 'copy-files');
198
    }
199
200
    /**
201
     * Check if user has permission to compress files and folders on a server.
202
     *
203
     * @param  \Pterodactyl\Models\User   $user
204
     * @param  \Pterodactyl\Models\Server $server
205
     * @return bool
206
     */
207
    public function compressFiles(User $user, Server $server)
208
    {
209
        return $this->checkPermission($user, $server, 'compress-files');
210
    }
211
212
    /**
213
     * Check if user has permission to decompress files on a server.
214
     *
215
     * @param  \Pterodactyl\Models\User   $user
216
     * @param  \Pterodactyl\Models\Server $server
217
     * @return bool
218
     */
219
    public function decompressFiles(User $user, Server $server)
220
    {
221
        return $this->checkPermission($user, $server, 'decompress-files');
222
    }
223
224
    /**
225
     * Check if user has permission to add files to a server.
226
     *
227
     * @param  \Pterodactyl\Models\User   $user
228
     * @param  \Pterodactyl\Models\Server $server
229
     * @return bool
230
     */
231
    public function addFiles(User $user, Server $server)
232
    {
233
        return $this->checkPermission($user, $server, 'add-files');
234
    }
235
236
    /**
237
     * Check if user has permission to upload files to a server.
238
     * This permission relies on the user having the 'add-files' permission as well due to page authorization.
239
     *
240
     * @param  \Pterodactyl\Models\User   $user
241
     * @param  \Pterodactyl\Models\Server $server
242
     * @return bool
243
     */
244
    public function uploadFiles(User $user, Server $server)
245
    {
246
        return $this->checkPermission($user, $server, 'upload-files');
247
    }
248
249
    /**
250
     * Check if user has permission to download files from a server.
251
     *
252
     * @param  \Pterodactyl\Models\User   $user
253
     * @param  \Pterodactyl\Models\Server $server
254
     * @return bool
255
     */
256
    public function downloadFiles(User $user, Server $server)
257
    {
258
        return $this->checkPermission($user, $server, 'download-files');
259
    }
260
261
    /**
262
     * Check if user has permission to delete files from a server.
263
     *
264
     * @param  \Pterodactyl\Models\User   $user
265
     * @param  \Pterodactyl\Models\Server $server
266
     * @return bool
267
     */
268
    public function deleteFiles(User $user, Server $server)
269
    {
270
        return $this->checkPermission($user, $server, 'delete-files');
271
    }
272
273
    /**
274
     * Check if user has permission to view subusers for the server.
275
     *
276
     * @param  \Pterodactyl\Models\User   $user
277
     * @param  \Pterodactyl\Models\Server $server
278
     * @return bool
279
     */
280
    public function listSubusers(User $user, Server $server)
281
    {
282
        return $this->checkPermission($user, $server, 'list-subusers');
283
    }
284
285
    /**
286
     * Check if user has permission to view specific subuser permissions.
287
     *
288
     * @param  \Pterodactyl\Models\User   $user
289
     * @param  \Pterodactyl\Models\Server $server
290
     * @return bool
291
     */
292
    public function viewSubuser(User $user, Server $server)
293
    {
294
        return $this->checkPermission($user, $server, 'view-subuser');
295
    }
296
297
    /**
298
     * Check if user has permission to edit a subuser.
299
     *
300
     * @param  \Pterodactyl\Models\User   $user
301
     * @param  \Pterodactyl\Models\Server $server
302
     * @return bool
303
     */
304
    public function editSubuser(User $user, Server $server)
305
    {
306
        return $this->checkPermission($user, $server, 'edit-subuser');
307
    }
308
309
    /**
310
     * Check if user has permission to delete a subuser.
311
     *
312
     * @param  \Pterodactyl\Models\User   $user
313
     * @param  \Pterodactyl\Models\Server $server
314
     * @return bool
315
     */
316
    public function deleteSubuser(User $user, Server $server)
317
    {
318
        return $this->checkPermission($user, $server, 'delete-subuser');
319
    }
320
321
    /**
322
     * Check if user has permission to edit a subuser.
323
     *
324
     * @param  \Pterodactyl\Models\User   $user
325
     * @param  \Pterodactyl\Models\Server $server
326
     * @return bool
327
     */
328
    public function createSubuser(User $user, Server $server)
329
    {
330
        return $this->checkPermission($user, $server, 'create-subuser');
331
    }
332
333
    /**
334
     * Check if user has permission to set the default connection for a server.
335
     *
336
     * @param  \Pterodactyl\Models\User   $user
337
     * @param  \Pterodactyl\Models\Server $server
338
     * @return bool
339
     */
340
    public function setConnection(User $user, Server $server)
341
    {
342
        return $this->checkPermission($user, $server, 'set-connection');
343
    }
344
345
    /**
346
     * Check if user has permission to view the startup command used for a server.
347
     *
348
     * @param  \Pterodactyl\Models\User   $user
349
     * @param  \Pterodactyl\Models\Server $server
350
     * @return bool
351
     */
352
    public function viewStartup(User $user, Server $server)
353
    {
354
        return $this->checkPermission($user, $server, 'view-startup');
355
    }
356
357
    /**
358
     * Check if user has permission to edit the startup command used for a server.
359
     *
360
     * @param  \Pterodactyl\Models\User   $user
361
     * @param  \Pterodactyl\Models\Server $server
362
     * @return bool
363
     */
364
    public function editStartup(User $user, Server $server)
365
    {
366
        return $this->checkPermission($user, $server, 'edit-startup');
367
    }
368
369
    /**
370
     * Check if user has permission to view the SFTP information for a server.
371
     *
372
     * @param  \Pterodactyl\Models\User   $user
373
     * @param  \Pterodactyl\Models\Server $server
374
     * @return bool
375
     */
376
    public function viewSftp(User $user, Server $server)
377
    {
378
        return $this->checkPermission($user, $server, 'view-sftp');
379
    }
380
381
    /**
382
     * Check if user has permission to reset the SFTP password for a server.
383
     *
384
     * @param  \Pterodactyl\Models\User   $user
385
     * @param  \Pterodactyl\Models\Server $server
386
     * @return bool
387
     */
388
    public function resetSftp(User $user, Server $server)
389
    {
390
        return $this->checkPermission($user, $server, 'reset-sftp');
391
    }
392
393
    /**
394
     * Check if user has permission to view the SFTP password for a server.
395
     *
396
     * @param  \Pterodactyl\Models\User   $user
397
     * @param  \Pterodactyl\Models\Server $server
398
     * @return bool
399
     */
400
    public function viewSftpPassword(User $user, Server $server)
401
    {
402
        return $this->checkPermission($user, $server, 'view-sftp-password');
403
    }
404
405
    /**
406
     * Check if user has permission to view databases for a server.
407
     *
408
     * @param  \Pterodactyl\Models\User   $user
409
     * @param  \Pterodactyl\Models\Server $server
410
     * @return bool
411
     */
412
    public function viewDatabases(User $user, Server $server)
413
    {
414
        return $this->checkPermission($user, $server, 'view-databases');
415
    }
416
417
    /**
418
     * Check if user has permission to reset database passwords.
419
     *
420
     * @param  \Pterodactyl\Models\User   $user
421
     * @param  \Pterodactyl\Models\Server $server
422
     * @return bool
423
     */
424
    public function resetDbPassword(User $user, Server $server)
425
    {
426
        return $this->checkPermission($user, $server, 'reset-db-password');
427
    }
428
429
    /**
430
     * Check if user has permission to view all tasks for a server.
431
     *
432
     * @param  \Pterodactyl\Models\User   $user
433
     * @param  \Pterodactyl\Models\Server $server
434
     * @return bool
435
     */
436
    public function listTasks(User $user, Server $server)
437
    {
438
        return $this->checkPermission($user, $server, 'list-tasks');
439
    }
440
441
    /**
442
     * Check if user has permission to view a specific task for a server.
443
     *
444
     * @param  \Pterodactyl\Models\User   $user
445
     * @param  \Pterodactyl\Models\Server $server
446
     * @return bool
447
     */
448
    public function viewTask(User $user, Server $server)
449
    {
450
        return $this->checkPermission($user, $server, 'view-task');
451
    }
452
453
    /**
454
     * Check if user has permission to view a toggle a task for a server.
455
     *
456
     * @param  \Pterodactyl\Models\User   $user
457
     * @param  \Pterodactyl\Models\Server $server
458
     * @return bool
459
     */
460
    public function toggleTask(User $user, Server $server)
461
    {
462
        return $this->checkPermission($user, $server, 'toggle-task');
463
    }
464
465
    /**
466
     * Check if user has permission to queue a task for a server.
467
     *
468
     * @param  \Pterodactyl\Models\User   $user
469
     * @param  \Pterodactyl\Models\Server $server
470
     * @return bool
471
     */
472
    public function queueTask(User $user, Server $server)
473
    {
474
        return $this->checkPermission($user, $server, 'queue-task');
475
    }
476
477
    /**
478
     * Check if user has permission to delete a specific task for a server.
479
     *
480
     * @param  \Pterodactyl\Models\User   $user
481
     * @param  \Pterodactyl\Models\Server $server
482
     * @return bool
483
     */
484
    public function deleteTask(User $user, Server $server)
485
    {
486
        return $this->checkPermission($user, $server, 'delete-task');
487
    }
488
489
    /**
490
     * Check if user has permission to create a task for a server.
491
     *
492
     * @param  \Pterodactyl\Models\User   $user
493
     * @param  \Pterodactyl\Models\Server $server
494
     * @return bool
495
     */
496
    public function createTask(User $user, Server $server)
497
    {
498
        return $this->checkPermission($user, $server, 'create-task');
499
    }
500
501
    /**
502
     * Checks if the user has the given permission on/for the server.
503
     *
504
     * @param \Pterodactyl\Models\User   $user
505
     * @param \Pterodactyl\Models\Server $server
506
     * @param $permission
507
     * @return bool
508
     */
509
    private function checkPermission(User $user, Server $server, $permission)
510
    {
511
        if ($this->isOwner($user, $server)) {
512
            return true;
513
        }
514
515
        return $user->permissions()->server($server)->permission($permission)->exists();
516
    }
517
}
518