forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerPolicy.php
More file actions
74 lines (63 loc) · 1.9 KB
/
ServerPolicy.php
File metadata and controls
74 lines (63 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Pterodactyl\Policies;
use Carbon\Carbon;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
class ServerPolicy
{
/**
* @var \Illuminate\Contracts\Cache\Repository
*/
private $cache;
/**
* ServerPolicy constructor.
*/
public function __construct(CacheRepository $cache)
{
$this->cache = $cache;
}
/**
* Checks if the user has the given permission on/for the server.
*
* @param string $permission
*
* @return bool
*/
protected function checkPermission(User $user, Server $server, $permission)
{
$key = sprintf('ServerPolicy.%s.%s', $user->uuid, $server->uuid);
$permissions = $this->cache->remember($key, Carbon::now()->addSeconds(5), function () use ($user, $server) {
/** @var \Pterodactyl\Models\Subuser|null $subuser */
$subuser = $server->subusers()->where('user_id', $user->id)->first();
return $subuser ? $subuser->permissions : [];
});
return in_array($permission, $permissions);
}
/**
* Runs before any of the functions are called. Used to determine if user is root admin, if so, ignore permissions.
*
* @param string $ability
*
* @return bool
*/
public function before(User $user, $ability, Server $server)
{
if ($user->root_admin || $server->owner_id === $user->id) {
return true;
}
return $this->checkPermission($user, $server, $ability);
}
/**
* This is a horrendous hack to avoid Laravel's "smart" behavior that does
* not call the before() function if there isn't a function matching the
* policy permission.
*
* @param string $name
* @param mixed $arguments
*/
public function __call($name, $arguments)
{
// do nothing
}
}