55use Carbon \CarbonImmutable ;
66use Illuminate \Http \Response ;
77use Pterodactyl \Models \Server ;
8- use Pterodactyl \Models \Subuser ;
98use Illuminate \Http \JsonResponse ;
109use Pterodactyl \Models \Permission ;
1110use Illuminate \Contracts \Cache \Repository ;
1211use Pterodactyl \Services \Nodes \NodeJWTService ;
1312use Symfony \Component \HttpKernel \Exception \HttpException ;
1413use Pterodactyl \Http \Requests \Api \Client \ClientApiRequest ;
14+ use Pterodactyl \Services \Servers \GetUserPermissionsService ;
1515use Pterodactyl \Http \Controllers \Api \Client \ClientApiController ;
1616
1717class WebsocketController extends ClientApiController
@@ -26,18 +26,28 @@ class WebsocketController extends ClientApiController
2626 */
2727 private $ jwtService ;
2828
29+ /**
30+ * @var \Pterodactyl\Services\Servers\GetUserPermissionsService
31+ */
32+ private $ permissionsService ;
33+
2934 /**
3035 * WebsocketController constructor.
3136 *
3237 * @param \Pterodactyl\Services\Nodes\NodeJWTService $jwtService
38+ * @param \Pterodactyl\Services\Servers\GetUserPermissionsService $permissionsService
3339 * @param \Illuminate\Contracts\Cache\Repository $cache
3440 */
35- public function __construct (NodeJWTService $ jwtService , Repository $ cache )
36- {
41+ public function __construct (
42+ NodeJWTService $ jwtService ,
43+ GetUserPermissionsService $ permissionsService ,
44+ Repository $ cache
45+ ) {
3746 parent ::__construct ();
3847
3948 $ this ->cache = $ cache ;
4049 $ this ->jwtService = $ jwtService ;
50+ $ this ->permissionsService = $ permissionsService ;
4151 }
4252
4353 /**
@@ -53,32 +63,16 @@ public function __construct(NodeJWTService $jwtService, Repository $cache)
5363 public function __invoke (ClientApiRequest $ request , Server $ server )
5464 {
5565 $ user = $ request ->user ();
56- if ($ user ->cannot (Permission::ACTION_WEBSOCKET , $ server )) {
66+ if ($ user ->cannot (Permission::ACTION_WEBSOCKET_CONNECT , $ server )) {
5767 throw new HttpException (Response::HTTP_FORBIDDEN , 'You do not have permission to connect to this server \'s websocket. ' );
5868 }
5969
60- if ($ user ->root_admin || $ user ->id === $ server ->owner_id ) {
61- $ permissions = ['* ' ];
62-
63- if ($ user ->root_admin ) {
64- $ permissions [] = 'admin.errors ' ;
65- $ permissions [] = 'admin.install ' ;
66- }
67- } else {
68- /** @var \Pterodactyl\Models\Subuser|null $subuserPermissions */
69- $ subuserPermissions = $ server ->subusers ->first (function (Subuser $ subuser ) use ($ user ) {
70- return $ subuser ->user_id === $ user ->id ;
71- });
72-
73- $ permissions = $ subuserPermissions ? $ subuserPermissions ->permissions : [];
74- }
75-
7670 $ token = $ this ->jwtService
7771 ->setExpiresAt (CarbonImmutable::now ()->addMinutes (15 ))
7872 ->setClaims ([
7973 'user_id ' => $ request ->user ()->id ,
8074 'server_uuid ' => $ server ->uuid ,
81- 'permissions ' => $ permissions ?? [] ,
75+ 'permissions ' => $ this -> permissionsService -> handle ( $ server , $ user ) ,
8276 ])
8377 ->handle ($ server ->node , $ user ->id . $ server ->uuid );
8478
0 commit comments