33namespace Pterodactyl \Http \Controllers \Api \Client \Servers ;
44
55use Illuminate \Http \Request ;
6- use Pterodactyl \Models \User ;
76use Pterodactyl \Models \Server ;
8- use Pterodactyl \Models \Subuser ;
97use Illuminate \Http \JsonResponse ;
108use Pterodactyl \Models \Permission ;
9+ use Illuminate \Support \Facades \Log ;
1110use Pterodactyl \Repositories \Eloquent \SubuserRepository ;
1211use Pterodactyl \Services \Subusers \SubuserCreationService ;
12+ use Pterodactyl \Repositories \Wings \DaemonServerRepository ;
1313use Pterodactyl \Transformers \Api \Client \SubuserTransformer ;
1414use Pterodactyl \Http \Controllers \Api \Client \ClientApiController ;
15+ use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
1516use Pterodactyl \Http \Requests \Api \Client \Servers \Subusers \GetSubuserRequest ;
1617use Pterodactyl \Http \Requests \Api \Client \Servers \Subusers \StoreSubuserRequest ;
1718use Pterodactyl \Http \Requests \Api \Client \Servers \Subusers \DeleteSubuserRequest ;
@@ -29,20 +30,28 @@ class SubuserController extends ClientApiController
2930 */
3031 private $ creationService ;
3132
33+ /**
34+ * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
35+ */
36+ private $ serverRepository ;
37+
3238 /**
3339 * SubuserController constructor.
3440 *
3541 * @param \Pterodactyl\Repositories\Eloquent\SubuserRepository $repository
3642 * @param \Pterodactyl\Services\Subusers\SubuserCreationService $creationService
43+ * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $serverRepository
3744 */
3845 public function __construct (
3946 SubuserRepository $ repository ,
40- SubuserCreationService $ creationService
47+ SubuserCreationService $ creationService ,
48+ DaemonServerRepository $ serverRepository
4149 ) {
4250 parent ::__construct ();
4351
4452 $ this ->repository = $ repository ;
4553 $ this ->creationService = $ creationService ;
54+ $ this ->serverRepository = $ serverRepository ;
4655 }
4756
4857 /**
@@ -101,19 +110,38 @@ public function store(StoreSubuserRequest $request, Server $server)
101110 * Update a given subuser in the system for the server.
102111 *
103112 * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest $request
113+ * @param \Pterodactyl\Models\Server $server
104114 * @return array
105115 *
106116 * @throws \Pterodactyl\Exceptions\Model\DataValidationException
107117 * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
108118 */
109- public function update (UpdateSubuserRequest $ request ): array
119+ public function update (UpdateSubuserRequest $ request, Server $ server ): array
110120 {
111121 /** @var \Pterodactyl\Models\Subuser $subuser */
112122 $ subuser = $ request ->attributes ->get ('subuser ' );
113123
114- $ this ->repository ->update ($ subuser ->id , [
115- 'permissions ' => $ this ->getDefaultPermissions ($ request ),
116- ]);
124+ $ permissions = $ this ->getDefaultPermissions ($ request );
125+ $ current = $ subuser ->permissions ;
126+
127+ sort ($ permissions );
128+ sort ($ current );
129+
130+ // Only update the database and hit up the Wings instance to invalidate JTI's if the permissions
131+ // have actually changed for the user.
132+ if ($ permissions !== $ current ) {
133+ $ this ->repository ->update ($ subuser ->id , [
134+ 'permissions ' => $ this ->getDefaultPermissions ($ request ),
135+ ]);
136+
137+ try {
138+ $ this ->serverRepository ->setServer ($ server )->revokeJTIs ([md5 ($ subuser ->user_id . $ server ->uuid )]);
139+ } catch (DaemonConnectionException $ exception ) {
140+ // Don't block this request if we can't connect to the Wings instance. Chances are it is
141+ // offline in this event and the token will be invalid anyways once Wings boots back.
142+ Log::warning ($ exception , ['user_id ' => $ subuser ->user_id , 'server_id ' => $ server ->id ]);
143+ }
144+ }
117145
118146 return $ this ->fractal ->item ($ subuser ->refresh ())
119147 ->transformWith ($ this ->getTransformer (SubuserTransformer::class))
@@ -124,15 +152,23 @@ public function update(UpdateSubuserRequest $request): array
124152 * Removes a subusers from a server's assignment.
125153 *
126154 * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest $request
155+ * @param \Pterodactyl\Models\Server $server
127156 * @return \Illuminate\Http\JsonResponse
128157 */
129- public function delete (DeleteSubuserRequest $ request )
158+ public function delete (DeleteSubuserRequest $ request, Server $ server )
130159 {
131160 /** @var \Pterodactyl\Models\Subuser $subuser */
132161 $ subuser = $ request ->attributes ->get ('subuser ' );
133162
134163 $ this ->repository ->delete ($ subuser ->id );
135164
165+ try {
166+ $ this ->serverRepository ->revokeJTIs ([md5 ($ subuser ->user_id . $ server ->uuid )]);
167+ } catch (DaemonConnectionException $ exception ) {
168+ // Don't block this request if we can't connect to the Wings instance.
169+ Log::warning ($ exception , ['user_id ' => $ subuser ->user_id , 'server_id ' => $ server ->id ]);
170+ }
171+
136172 return new JsonResponse ([], JsonResponse::HTTP_NO_CONTENT );
137173 }
138174
0 commit comments