2015-12-06 13:58:49 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2015-12-06 13:58:49 -05:00
|
|
|
namespace Pterodactyl\Policies;
|
|
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\User;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
|
|
|
|
class ServerPolicy
|
|
|
|
|
{
|
2017-02-09 18:44:07 -05:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* 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.
|
2017-02-09 18:44:07 -05:00
|
|
|
*/
|
2026-10-11 19:54:55 -04:00
|
|
|
/** @param list<mixed> $arguments */
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __call(string $name, array $arguments): void
|
2017-02-09 18:44:07 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
// do nothing
|
2017-02-09 18:44:07 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-06 13:58:49 -05:00
|
|
|
/**
|
2017-04-09 13:34:47 -04:00
|
|
|
* Runs before any of the functions are called. Used to determine if user is root admin, if so, ignore permissions.
|
2015-12-06 13:58:49 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function before(User $user, string $ability, Server $server): bool
|
2015-12-06 13:58:49 -05:00
|
|
|
{
|
2017-09-10 23:57:18 -05:00
|
|
|
if ($user->root_admin || $server->owner_id === $user->id) {
|
2015-12-06 13:58:49 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-09 13:34:47 -04:00
|
|
|
return $this->checkPermission($user, $server, $ability);
|
2017-01-18 20:45:10 -05:00
|
|
|
}
|
2017-12-17 13:07:38 -06:00
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* Checks if the user has the given permission on/for the server.
|
2017-12-17 13:07:38 -06:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
protected function checkPermission(User $user, Server $server, string $permission): bool
|
2017-12-17 13:07:38 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$subuser = $server->loadMissing('subusers')->subusers->where('user_id', $user->id)->first();
|
|
|
|
|
if (! $subuser || empty($permission)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return in_array($permission, $subuser->permissions);
|
2017-12-17 13:07:38 -06:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|