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 ( a5902b...d5352e )
by Dane
02:52
created

ServerPolicy::createFiles()   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 2
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\Policies;
26
27
use Cache;
28
use Carbon;
29
use Pterodactyl\Models\User;
30
use Pterodactyl\Models\Server;
31
32
class ServerPolicy
33
{
34
    /**
35
     * Create a new policy instance.
36
     *
37
     * @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...
38
     */
39
    public function __construct()
40
    {
41
        //
42
    }
43
44
    /**
45
     * Checks if the user has the given permission on/for the server.
46
     *
47
     * @param \Pterodactyl\Models\User   $user
48
     * @param \Pterodactyl\Models\Server $server
49
     * @param $permission
50
     * @return bool
51
     */
52
    private function checkPermission(User $user, Server $server, $permission)
53
    {
54
        if ($this->isOwner($user, $server)) {
55
            return true;
56
        }
57
58
        $permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(10), function () use ($user, $server) {
0 ignored issues
show
Documentation introduced by
The property uuid 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...
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...
59
            return $user->permissions()->server($server)->get()->transform(function ($item) {
60
                return $item->permission;
61
            })->values();
62
        });
63
64
        return $permissions->search($permission, true) !== false;
65
    }
66
67
    /**
68
     * Determine if current user is the owner of a server.
69
     *
70
     * @param  \Pterodactyl\Models\User    $user
71
     * @param  \Pterodactyl\Models\Server  $server
72
     * @return bool
73
     */
74
    protected function isOwner(User $user, Server $server)
75
    {
76
        return $server->owner_id === $user->id;
0 ignored issues
show
Documentation introduced by
The property owner_id 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...
77
    }
78
79
    /**
80
     * Runs before any of the functions are called. Used to determine if user is root admin, if so, ignore permissions.
81
     *
82
     * @param  \Pterodactyl\Models\User $user
83
     * @param  string $ability
84
     * @return bool
85
     */
86
    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...
87
    {
88
        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...
89
            return true;
90
        }
91
    }
92
93
    /**
94
     * Check if user has permission to control power for a server.
95
     *
96
     * @param  \Pterodactyl\Models\User   $user
97
     * @param  \Pterodactyl\Models\Server $server
98
     * @return bool
99
     */
100
    public function power(User $user, Server $server)
101
    {
102
        return $this->checkPermission($user, $server, 'power');
103
    }
104
105
    /**
106
     * Check if user has permission to start a server.
107
     *
108
     * @param  \Pterodactyl\Models\User   $user
109
     * @param  \Pterodactyl\Models\Server $server
110
     * @return bool
111
     */
112
    public function powerStart(User $user, Server $server)
113
    {
114
        return $this->checkPermission($user, $server, 'power-start');
115
    }
116
117
    /**
118
     * Check if user has permission to stop a server.
119
     *
120
     * @param  \Pterodactyl\Models\User   $user
121
     * @param  \Pterodactyl\Models\Server $server
122
     * @return bool
123
     */
124
    public function powerStop(User $user, Server $server)
125
    {
126
        return $this->checkPermission($user, $server, 'power-stop');
127
    }
128
129
    /**
130
     * Check if user has permission to restart a server.
131
     *
132
     * @param  \Pterodactyl\Models\User   $user
133
     * @param  \Pterodactyl\Models\Server $server
134
     * @return bool
135
     */
136
    public function powerRestart(User $user, Server $server)
137
    {
138
        return $this->checkPermission($user, $server, 'power-restart');
139
    }
140
141
    /**
142
     * Check if user has permission to kill a server.
143
     *
144
     * @param  \Pterodactyl\Models\User   $user
145
     * @param  \Pterodactyl\Models\Server $server
146
     * @return bool
147
     */
148
    public function powerKill(User $user, Server $server)
149
    {
150
        return $this->checkPermission($user, $server, 'power-kill');
151
    }
152
153
    /**
154
     * Check if user has permission to run a command on a server.
155
     *
156
     * @param  \Pterodactyl\Models\User   $user
157
     * @param  \Pterodactyl\Models\Server $server
158
     * @return bool
159
     */
160
    public function sendCommand(User $user, Server $server)
161
    {
162
        return $this->checkPermission($user, $server, 'send-command');
163
    }
164
165
    /**
166
     * Check if user has permission to list files on a server.
167
     *
168
     * @param  \Pterodactyl\Models\User   $user
169
     * @param  \Pterodactyl\Models\Server $server
170
     * @return bool
171
     */
172
    public function listFiles(User $user, Server $server)
173
    {
174
        return $this->checkPermission($user, $server, 'list-files');
175
    }
176
177
    /**
178
     * Check if user has permission to edit files on a server.
179
     *
180
     * @param  \Pterodactyl\Models\User   $user
181
     * @param  \Pterodactyl\Models\Server $server
182
     * @return bool
183
     */
184
    public function editFiles(User $user, Server $server)
185
    {
186
        return $this->checkPermission($user, $server, 'edit-files');
187
    }
188
189
    /**
190
     * Check if user has permission to save files on a server.
191
     *
192
     * @param  \Pterodactyl\Models\User   $user
193
     * @param  \Pterodactyl\Models\Server $server
194
     * @return bool
195
     */
196
    public function saveFiles(User $user, Server $server)
197
    {
198
        return $this->checkPermission($user, $server, 'save-files');
199
    }
200
201
    /**
202
     * Check if user has permission to move and rename files and folders on a server.
203
     *
204
     * @param  \Pterodactyl\Models\User   $user
205
     * @param  \Pterodactyl\Models\Server $server
206
     * @return bool
207
     */
208
    public function moveFiles(User $user, Server $server)
209
    {
210
        return $this->checkPermission($user, $server, 'move-files');
211
    }
212
213
    /**
214
     * Check if user has permission to copy folders and files on a server.
215
     *
216
     * @param  \Pterodactyl\Models\User   $user
217
     * @param  \Pterodactyl\Models\Server $server
218
     * @return bool
219
     */
220
    public function copyFiles(User $user, Server $server)
221
    {
222
        return $this->checkPermission($user, $server, 'copy-files');
223
    }
224
225
    /**
226
     * Check if user has permission to compress files and folders on a server.
227
     *
228
     * @param  \Pterodactyl\Models\User   $user
229
     * @param  \Pterodactyl\Models\Server $server
230
     * @return bool
231
     */
232
    public function compressFiles(User $user, Server $server)
233
    {
234
        return $this->checkPermission($user, $server, 'compress-files');
235
    }
236
237
    /**
238
     * Check if user has permission to decompress files on a server.
239
     *
240
     * @param  \Pterodactyl\Models\User   $user
241
     * @param  \Pterodactyl\Models\Server $server
242
     * @return bool
243
     */
244
    public function decompressFiles(User $user, Server $server)
245
    {
246
        return $this->checkPermission($user, $server, 'decompress-files');
247
    }
248
249
    /**
250
     * Check if user has permission to add files to a server.
251
     *
252
     * @param  \Pterodactyl\Models\User   $user
253
     * @param  \Pterodactyl\Models\Server $server
254
     * @return bool
255
     */
256
    public function createFiles(User $user, Server $server)
257
    {
258
        return $this->checkPermission($user, $server, 'create-files');
259
    }
260
261
    /**
262
     * Check if user has permission to upload files to a server.
263
     * This permission relies on the user having the 'create-files' permission as well due to page authorization.
264
     *
265
     * @param  \Pterodactyl\Models\User   $user
266
     * @param  \Pterodactyl\Models\Server $server
267
     * @return bool
268
     */
269
    public function uploadFiles(User $user, Server $server)
270
    {
271
        return $this->checkPermission($user, $server, 'upload-files');
272
    }
273
274
    /**
275
     * Check if user has permission to download files from a server.
276
     *
277
     * @param  \Pterodactyl\Models\User   $user
278
     * @param  \Pterodactyl\Models\Server $server
279
     * @return bool
280
     */
281
    public function downloadFiles(User $user, Server $server)
282
    {
283
        return $this->checkPermission($user, $server, 'download-files');
284
    }
285
286
    /**
287
     * Check if user has permission to delete files from a server.
288
     *
289
     * @param  \Pterodactyl\Models\User   $user
290
     * @param  \Pterodactyl\Models\Server $server
291
     * @return bool
292
     */
293
    public function deleteFiles(User $user, Server $server)
294
    {
295
        return $this->checkPermission($user, $server, 'delete-files');
296
    }
297
298
    /**
299
     * Check if user has permission to view subusers for the server.
300
     *
301
     * @param  \Pterodactyl\Models\User   $user
302
     * @param  \Pterodactyl\Models\Server $server
303
     * @return bool
304
     */
305
    public function listSubusers(User $user, Server $server)
306
    {
307
        return $this->checkPermission($user, $server, 'list-subusers');
308
    }
309
310
    /**
311
     * Check if user has permission to view specific subuser permissions.
312
     *
313
     * @param  \Pterodactyl\Models\User   $user
314
     * @param  \Pterodactyl\Models\Server $server
315
     * @return bool
316
     */
317
    public function viewSubuser(User $user, Server $server)
318
    {
319
        return $this->checkPermission($user, $server, 'view-subuser');
320
    }
321
322
    /**
323
     * Check if user has permission to edit a subuser.
324
     *
325
     * @param  \Pterodactyl\Models\User   $user
326
     * @param  \Pterodactyl\Models\Server $server
327
     * @return bool
328
     */
329
    public function editSubuser(User $user, Server $server)
330
    {
331
        return $this->checkPermission($user, $server, 'edit-subuser');
332
    }
333
334
    /**
335
     * Check if user has permission to delete a subuser.
336
     *
337
     * @param  \Pterodactyl\Models\User   $user
338
     * @param  \Pterodactyl\Models\Server $server
339
     * @return bool
340
     */
341
    public function deleteSubuser(User $user, Server $server)
342
    {
343
        return $this->checkPermission($user, $server, 'delete-subuser');
344
    }
345
346
    /**
347
     * Check if user has permission to edit a subuser.
348
     *
349
     * @param  \Pterodactyl\Models\User   $user
350
     * @param  \Pterodactyl\Models\Server $server
351
     * @return bool
352
     */
353
    public function createSubuser(User $user, Server $server)
354
    {
355
        return $this->checkPermission($user, $server, 'create-subuser');
356
    }
357
358
    /**
359
     * Check if user has permission to set the default connection for a server.
360
     *
361
     * @param  \Pterodactyl\Models\User   $user
362
     * @param  \Pterodactyl\Models\Server $server
363
     * @return bool
364
     */
365
    public function setConnection(User $user, Server $server)
366
    {
367
        return $this->checkPermission($user, $server, 'set-connection');
368
    }
369
370
    /**
371
     * Check if user has permission to view the startup command used for a server.
372
     *
373
     * @param  \Pterodactyl\Models\User   $user
374
     * @param  \Pterodactyl\Models\Server $server
375
     * @return bool
376
     */
377
    public function viewStartup(User $user, Server $server)
378
    {
379
        return $this->checkPermission($user, $server, 'view-startup');
380
    }
381
382
    /**
383
     * Check if user has permission to edit the startup command used for a server.
384
     *
385
     * @param  \Pterodactyl\Models\User   $user
386
     * @param  \Pterodactyl\Models\Server $server
387
     * @return bool
388
     */
389
    public function editStartup(User $user, Server $server)
390
    {
391
        return $this->checkPermission($user, $server, 'edit-startup');
392
    }
393
394
    /**
395
     * Check if user has permission to view the SFTP information for a server.
396
     *
397
     * @param  \Pterodactyl\Models\User   $user
398
     * @param  \Pterodactyl\Models\Server $server
399
     * @return bool
400
     */
401
    public function viewSftp(User $user, Server $server)
402
    {
403
        return $this->checkPermission($user, $server, 'view-sftp');
404
    }
405
406
    /**
407
     * Check if user has permission to reset the SFTP password for a server.
408
     *
409
     * @param  \Pterodactyl\Models\User   $user
410
     * @param  \Pterodactyl\Models\Server $server
411
     * @return bool
412
     */
413
    public function resetSftp(User $user, Server $server)
414
    {
415
        return $this->checkPermission($user, $server, 'reset-sftp');
416
    }
417
418
    /**
419
     * Check if user has permission to view the SFTP password for a server.
420
     *
421
     * @param  \Pterodactyl\Models\User   $user
422
     * @param  \Pterodactyl\Models\Server $server
423
     * @return bool
424
     */
425
    public function viewSftpPassword(User $user, Server $server)
426
    {
427
        return $this->checkPermission($user, $server, 'view-sftp-password');
428
    }
429
430
    /**
431
     * Check if user has permission to view databases for a server.
432
     *
433
     * @param  \Pterodactyl\Models\User   $user
434
     * @param  \Pterodactyl\Models\Server $server
435
     * @return bool
436
     */
437
    public function viewDatabases(User $user, Server $server)
438
    {
439
        return $this->checkPermission($user, $server, 'view-databases');
440
    }
441
442
    /**
443
     * Check if user has permission to reset database passwords.
444
     *
445
     * @param  \Pterodactyl\Models\User   $user
446
     * @param  \Pterodactyl\Models\Server $server
447
     * @return bool
448
     */
449
    public function resetDbPassword(User $user, Server $server)
450
    {
451
        return $this->checkPermission($user, $server, 'reset-db-password');
452
    }
453
454
    /**
455
     * Check if user has permission to view all tasks for a server.
456
     *
457
     * @param  \Pterodactyl\Models\User   $user
458
     * @param  \Pterodactyl\Models\Server $server
459
     * @return bool
460
     */
461
    public function listTasks(User $user, Server $server)
462
    {
463
        return $this->checkPermission($user, $server, 'list-tasks');
464
    }
465
466
    /**
467
     * Check if user has permission to view a specific task for a server.
468
     *
469
     * @param  \Pterodactyl\Models\User   $user
470
     * @param  \Pterodactyl\Models\Server $server
471
     * @return bool
472
     */
473
    public function viewTask(User $user, Server $server)
474
    {
475
        return $this->checkPermission($user, $server, 'view-task');
476
    }
477
478
    /**
479
     * Check if user has permission to view a toggle a task for a server.
480
     *
481
     * @param  \Pterodactyl\Models\User   $user
482
     * @param  \Pterodactyl\Models\Server $server
483
     * @return bool
484
     */
485
    public function toggleTask(User $user, Server $server)
486
    {
487
        return $this->checkPermission($user, $server, 'toggle-task');
488
    }
489
490
    /**
491
     * Check if user has permission to queue a task for a server.
492
     *
493
     * @param  \Pterodactyl\Models\User   $user
494
     * @param  \Pterodactyl\Models\Server $server
495
     * @return bool
496
     */
497
    public function queueTask(User $user, Server $server)
498
    {
499
        return $this->checkPermission($user, $server, 'queue-task');
500
    }
501
502
    /**
503
     * Check if user has permission to delete a specific task for a server.
504
     *
505
     * @param  \Pterodactyl\Models\User   $user
506
     * @param  \Pterodactyl\Models\Server $server
507
     * @return bool
508
     */
509
    public function deleteTask(User $user, Server $server)
510
    {
511
        return $this->checkPermission($user, $server, 'delete-task');
512
    }
513
514
    /**
515
     * Check if user has permission to create a task for a server.
516
     *
517
     * @param  \Pterodactyl\Models\User   $user
518
     * @param  \Pterodactyl\Models\Server $server
519
     * @return bool
520
     */
521
    public function createTask(User $user, Server $server)
522
    {
523
        return $this->checkPermission($user, $server, 'create-task');
524
    }
525
526
    /**
527
     * Check if user has permission to view server allocations.
528
     *
529
     * @param  \Pterodactyl\Models\User   $user
530
     * @param  \Pterodactyl\Models\Server $server
531
     * @return bool
532
     */
533
    public function viewAllocation(User $user, Server $server)
534
    {
535
        return $this->checkPermission($user, $server, 'view-allocation');
536
    }
537
538
    /**
539
     * Check if user has permission to set the default allocation.
540
     *
541
     * @param  \Pterodactyl\Models\User   $user
542
     * @param  \Pterodactyl\Models\Server $server
543
     * @return bool
544
     */
545
    public function setAllocation(User $user, Server $server)
546
    {
547
        return $this->checkPermission($user, $server, 'set-allocation');
548
    }
549
}
550