Skip to content

Commit 5717a70

Browse files
committed
Fix authorization checking for subusers
1 parent e4e5dea commit 5717a70

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

app/Models/User.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
*
3838
* @property string $name
3939
* @property \Pterodactyl\Models\ApiKey[]|\Illuminate\Database\Eloquent\Collection $apiKeys
40-
* @property \Pterodactyl\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
4140
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
42-
* @property \Pterodactyl\Models\Subuser[]|\Illuminate\Database\Eloquent\Collection $subuserOf
4341
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
4442
*/
4543
class User extends Validable implements
@@ -220,16 +218,6 @@ public function getNameAttribute()
220218
return trim($this->name_first . ' ' . $this->name_last);
221219
}
222220

223-
/**
224-
* Returns all permissions that a user has.
225-
*
226-
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
227-
*/
228-
public function permissions()
229-
{
230-
return $this->hasManyThrough(Permission::class, Subuser::class);
231-
}
232-
233221
/**
234222
* Returns all servers that a user owns.
235223
*
@@ -240,16 +228,6 @@ public function servers()
240228
return $this->hasMany(Server::class, 'owner_id');
241229
}
242230

243-
/**
244-
* Return all servers that user is listed as a subuser of directly.
245-
*
246-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
247-
*/
248-
public function subuserOf()
249-
{
250-
return $this->hasMany(Subuser::class);
251-
}
252-
253231
/**
254232
* Return all of the daemon keys that a user belongs to.
255233
*

app/Policies/ServerPolicy.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Policies;
114

12-
use Cache;
13-
use Carbon;
5+
use Carbon\Carbon;
146
use Pterodactyl\Models\User;
157
use Pterodactyl\Models\Server;
8+
use Illuminate\Contracts\Cache\Repository as CacheRepository;
169

1710
class ServerPolicy
1811
{
12+
/**
13+
* @var \Illuminate\Contracts\Cache\Repository
14+
*/
15+
private $cache;
16+
17+
/**
18+
* ServerPolicy constructor.
19+
*
20+
* @param \Illuminate\Contracts\Cache\Repository $cache
21+
*/
22+
public function __construct(CacheRepository $cache)
23+
{
24+
$this->cache = $cache;
25+
}
26+
1927
/**
2028
* Checks if the user has the given permission on/for the server.
2129
*
@@ -26,13 +34,16 @@ class ServerPolicy
2634
*/
2735
protected function checkPermission(User $user, Server $server, $permission)
2836
{
29-
$permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(5), function () use ($user, $server) {
30-
return $user->permissions()->server($server)->get()->transform(function ($item) {
31-
return $item->permission;
32-
})->values();
37+
$key = sprintf('ServerPolicy.%s.%s', $user->uuid, $server->uuid);
38+
39+
$permissions = $this->cache->remember($key, Carbon::now()->addSeconds(5), function () use ($user, $server) {
40+
/** @var \Pterodactyl\Models\Subuser|null $subuser */
41+
$subuser = $server->subusers()->where('user_id', $user->id)->first();
42+
43+
return $subuser ? $subuser->permissions : [];
3344
});
3445

35-
return $permissions->search($permission, true) !== false;
46+
return in_array($permission, $permissions);
3647
}
3748

3849
/**

0 commit comments

Comments
 (0)