33namespace Pterodactyl \Http \Requests \Api \Client \Servers \Subusers ;
44
55use Illuminate \Http \Request ;
6- use Pterodactyl \Models \Server ;
6+ use Pterodactyl \Models \User ;
77use Pterodactyl \Exceptions \Http \HttpForbiddenException ;
8- use Pterodactyl \Repositories \Eloquent \SubuserRepository ;
98use Pterodactyl \Http \Requests \Api \Client \ClientApiRequest ;
10- use Pterodactyl \Exceptions \Repository \RecordNotFoundException ;
11- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
9+ use Pterodactyl \Services \Servers \GetUserPermissionsService ;
1210
1311abstract class SubuserRequest extends ClientApiRequest
1412{
@@ -30,10 +28,10 @@ public function authorize(): bool
3028 return false ;
3129 }
3230
33- // If there is a subuser present in the URL, validate that it is not the same as the
34- // current request user. You're not allowed to modify yourself .
35- if ($ this -> route ()-> hasParameter ( ' subuser ' ) ) {
36- if ($ this -> endpointSubuser ()-> user_id === $ this ->user ()->id ) {
31+ $ user = $ this -> route ()-> parameter ( ' user ' );
32+ // Don't allow a user to edit themselves on the server .
33+ if ($ user instanceof User ) {
34+ if ($ user -> uuid === $ this ->user ()->uuid ) {
3735 return false ;
3836 }
3937 }
@@ -71,68 +69,14 @@ protected function validatePermissionsCanBeAssigned(array $permissions)
7169 // Otherwise, get the current subuser's permission set, and ensure that the
7270 // permissions they are trying to assign are not _more_ than the ones they
7371 // already have.
74- if (count (array_diff ($ permissions , $ this ->currentUserPermissions ())) > 0 ) {
72+ /** @var \Pterodactyl\Models\Subuser|null $subuser */
73+ /** @var \Pterodactyl\Services\Servers\GetUserPermissionsService $service */
74+ $ service = $ this ->container ->make (GetUserPermissionsService::class);
75+
76+ if (count (array_diff ($ permissions , $ service ->handle ($ server , $ user ))) > 0 ) {
7577 throw new HttpForbiddenException (
7678 'Cannot assign permissions to a subuser that your account does not actively possess. '
7779 );
7880 }
7981 }
80-
81- /**
82- * Returns the currently authenticated user's permissions.
83- *
84- * @return array
85- *
86- * @throws \Illuminate\Contracts\Container\BindingResolutionException
87- */
88- public function currentUserPermissions (): array
89- {
90- /** @var \Pterodactyl\Repositories\Eloquent\SubuserRepository $repository */
91- $ repository = $ this ->container ->make (SubuserRepository::class);
92-
93- /* @var \Pterodactyl\Models\Subuser $model */
94- try {
95- $ model = $ repository ->findFirstWhere ([
96- ['server_id ' , $ this ->route ()->parameter ('server ' )->id ],
97- ['user_id ' , $ this ->user ()->id ],
98- ]);
99- } catch (RecordNotFoundException $ exception ) {
100- return [];
101- }
102-
103- return $ model ->permissions ;
104- }
105-
106- /**
107- * Return the subuser model for the given request which can then be validated. If
108- * required request parameters are missing a 404 error will be returned, otherwise
109- * a model exception will be returned if the model is not found.
110- *
111- * This returns the subuser based on the endpoint being hit, not the actual subuser
112- * for the account making the request.
113- *
114- * @return \Pterodactyl\Models\Subuser
115- *
116- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
117- * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
118- * @throws \Illuminate\Contracts\Container\BindingResolutionException
119- */
120- public function endpointSubuser ()
121- {
122- /** @var \Pterodactyl\Repositories\Eloquent\SubuserRepository $repository */
123- $ repository = $ this ->container ->make (SubuserRepository::class);
124-
125- $ parameters = $ this ->route ()->parameters ();
126- if (
127- ! isset ($ parameters ['server ' ], $ parameters ['server ' ])
128- || ! is_string ($ parameters ['subuser ' ])
129- || ! $ parameters ['server ' ] instanceof Server
130- ) {
131- throw new NotFoundHttpException ;
132- }
133-
134- return $ this ->model ?: $ this ->model = $ repository ->getUserForServer (
135- $ parameters ['server ' ]->id , $ parameters ['subuser ' ]
136- );
137- }
13882}
0 commit comments