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\APIKey as Key; |
31
|
|
|
use Pterodactyl\Models\APIPermission as Permission; |
32
|
|
|
|
33
|
|
|
class APIKeyPolicy |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Checks if the API key has permission to perform an action. |
37
|
|
|
* |
38
|
|
|
* @param \Pterodactyl\Models\User $user |
39
|
|
|
* @param \Pterodactyl\Models\APIKey $key |
40
|
|
|
* @param string $permission |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
protected function checkPermission(User $user, Key $key, $permission) |
44
|
|
|
{ |
45
|
|
|
// Non-administrative users cannot use administrative routes. |
46
|
|
|
if (! starts_with($key, 'user.') && ! $user->isRootAdmin()) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// We don't tag this cache key with the user uuid because the key is already unique, |
51
|
|
|
// and multiple users are not defiend for a single key. |
52
|
|
|
$permissions = Cache::remember('APIKeyPolicy.' . $key->public, Carbon::now()->addSeconds(5), function () use ($key) { |
53
|
|
|
return $key->permissions()->get()->transform(function ($item) { |
54
|
|
|
return $item->permission; |
55
|
|
|
})->values(); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
return $permissions->search($permission, true) !== false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Determine if a user has permission to perform this action against the system. |
63
|
|
|
* |
64
|
|
|
* @param \Pterodactyl\Models\User $user |
65
|
|
|
* @param string $permission |
66
|
|
|
* @param \Pterodactyl\Models\APIKey $key |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function before(User $user, $permission, Key $key) |
70
|
|
|
{ |
71
|
|
|
return $this->checkPermission($user, $key, $permission); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|