Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Acl/Api/AdminAcl.php
10297 views
1
<?php
2
3
namespace Pterodactyl\Services\Acl\Api;
4
5
use Pterodactyl\Models\ApiKey;
6
7
class AdminAcl
8
{
9
/**
10
* Resource permission columns in the api_keys table begin
11
* with this identifier.
12
*/
13
public const COLUMN_IDENTIFIER = 'r_';
14
15
/**
16
* The different types of permissions available for API keys. This
17
* implements a read/write/none permissions scheme for all endpoints.
18
*/
19
public const NONE = 0;
20
public const READ = 1;
21
public const WRITE = 2;
22
23
/**
24
* Resources that are available on the API and can contain a permissions
25
* set for each key. These are stored in the database as r_{resource}.
26
*/
27
public const RESOURCE_SERVERS = 'servers';
28
public const RESOURCE_NODES = 'nodes';
29
public const RESOURCE_ALLOCATIONS = 'allocations';
30
public const RESOURCE_USERS = 'users';
31
public const RESOURCE_LOCATIONS = 'locations';
32
public const RESOURCE_NESTS = 'nests';
33
public const RESOURCE_EGGS = 'eggs';
34
public const RESOURCE_DATABASE_HOSTS = 'database_hosts';
35
public const RESOURCE_SERVER_DATABASES = 'server_databases';
36
37
/**
38
* Determine if an API key has permission to perform a specific read/write operation.
39
*/
40
public static function can(int $permission, int $action = self::READ): bool
41
{
42
if ($permission & $action) {
43
return true;
44
}
45
46
return false;
47
}
48
49
/**
50
* Determine if an API Key model has permission to access a given resource
51
* at a specific action level.
52
*/
53
public static function check(ApiKey $key, string $resource, int $action = self::READ): bool
54
{
55
return self::can(data_get($key, self::COLUMN_IDENTIFIER . $resource, self::NONE), $action);
56
}
57
58
/**
59
* Return a list of all resource constants defined in this ACL.
60
*
61
* @throws \ReflectionException
62
*/
63
public static function getResourceList(): array
64
{
65
$reflect = new \ReflectionClass(__CLASS__);
66
67
return collect($reflect->getConstants())->filter(function ($value, $key) {
68
return substr($key, 0, 9) === 'RESOURCE_';
69
})->values()->toArray();
70
}
71
}
72
73