2424
2525namespace Pterodactyl \Http \Controllers \Server ;
2626
27- use Log ;
28- use Auth ;
29- use Alert ;
30- use Pterodactyl \Models ;
27+ use Illuminate \Contracts \Session \Session ;
28+ use Prologue \Alerts \AlertsMessageBag ;
29+ use Pterodactyl \Contracts \Repository \SubuserRepositoryInterface ;
3130use Illuminate \Http \Request ;
32- use Pterodactyl \Exceptions \DisplayException ;
3331use Pterodactyl \Http \Controllers \Controller ;
34- use Pterodactyl \Repositories \SubuserRepository ;
35- use Pterodactyl \Exceptions \DisplayValidationException ;
32+ use Pterodactyl \Models \Permission ;
33+ use Pterodactyl \Services \Subusers \SubuserCreationService ;
34+ use Pterodactyl \Services \Subusers \SubuserDeletionService ;
35+ use Pterodactyl \Services \Subusers \SubuserUpdateService ;
36+ use Pterodactyl \Traits \Controllers \JavascriptInjection ;
3637
3738class SubuserController extends Controller
3839{
40+ use JavascriptInjection;
41+
42+ /**
43+ * @var \Prologue\Alerts\AlertsMessageBag
44+ */
45+ protected $ alert ;
46+
47+ /**
48+ * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
49+ */
50+ protected $ repository ;
51+
52+ /**
53+ * @var \Illuminate\Contracts\Session\Session
54+ */
55+ protected $ session ;
56+
57+ /**
58+ * @var \Pterodactyl\Services\Subusers\SubuserCreationService
59+ */
60+ protected $ subuserCreationService ;
61+
62+ /**
63+ * @var \Pterodactyl\Services\Subusers\SubuserDeletionService
64+ */
65+ protected $ subuserDeletionService ;
66+
67+ /**
68+ * @var \Pterodactyl\Services\Subusers\SubuserUpdateService
69+ */
70+ protected $ subuserUpdateService ;
71+
72+ /**
73+ * SubuserController constructor.
74+ *
75+ * @param \Prologue\Alerts\AlertsMessageBag $alert
76+ * @param \Illuminate\Contracts\Session\Session $session
77+ * @param \Pterodactyl\Services\Subusers\SubuserCreationService $subuserCreationService
78+ * @param \Pterodactyl\Services\Subusers\SubuserDeletionService $subuserDeletionService
79+ * @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $repository
80+ * @param \Pterodactyl\Services\Subusers\SubuserUpdateService $subuserUpdateService
81+ */
82+ public function __construct (
83+ AlertsMessageBag $ alert ,
84+ Session $ session ,
85+ SubuserCreationService $ subuserCreationService ,
86+ SubuserDeletionService $ subuserDeletionService ,
87+ SubuserRepositoryInterface $ repository ,
88+ SubuserUpdateService $ subuserUpdateService
89+ ) {
90+ $ this ->alert = $ alert ;
91+ $ this ->repository = $ repository ;
92+ $ this ->session = $ session ;
93+ $ this ->subuserCreationService = $ subuserCreationService ;
94+ $ this ->subuserDeletionService = $ subuserDeletionService ;
95+ $ this ->subuserUpdateService = $ subuserUpdateService ;
96+ }
97+
3998 /**
4099 * Displays the subuser overview index.
41100 *
42- * @param \Illuminate\Http\Request $request
43- * @param string $uuid
44101 * @return \Illuminate\View\View
102+ *
103+ * @throws \Illuminate\Auth\Access\AuthorizationException
104+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
45105 */
46- public function index (Request $ request , $ uuid )
106+ public function index ()
47107 {
48- $ server = Models \Server:: byUuid ( $ uuid )-> load ( ' subusers.user ' );
108+ $ server = $ this -> session -> get ( ' server_data.model ' );
49109 $ this ->authorize ('list-subusers ' , $ server );
50110
51- $ server -> js ();
111+ $ this -> injectJavascript ();
52112
53113 return view ('server.users.index ' , [
54- 'server ' => $ server ,
55- 'node ' => $ server ->node ,
56- 'subusers ' => $ server ->subusers ,
114+ 'subusers ' => $ this ->repository ->findWhere ([['server_id ' , '= ' , $ server ->id ]]),
57115 ]);
58116 }
59117
60118 /**
61119 * Displays the a single subuser overview.
62120 *
63- * @param \Illuminate\Http\Request $request
64- * @param string $uuid
65- * @param int $id
121+ * @param string $uuid
122+ * @param int $id
66123 * @return \Illuminate\View\View
124+ *
125+ * @throws \Illuminate\Auth\Access\AuthorizationException
126+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
67127 */
68- public function view (Request $ request , $ uuid , $ id )
128+ public function view ($ uuid , $ id )
69129 {
70- $ server = Models \Server:: byUuid ( $ uuid )-> load ( ' node ' );
130+ $ server = $ this -> session -> get ( ' server_data.model ' );
71131 $ this ->authorize ('view-subuser ' , $ server );
72132
73- $ subuser = Models \Subuser::with ('permissions ' , 'user ' )
74- ->where ('server_id ' , $ server ->id )->findOrFail ($ id );
75-
76- $ server ->js ();
133+ $ subuser = $ this ->repository ->getWithPermissions ($ id );
134+ $ this ->injectJavascript ();
77135
78136 return view ('server.users.view ' , [
79- 'server ' => $ server ,
80- 'node ' => $ server ->node ,
81137 'subuser ' => $ subuser ,
82- 'permlist ' => Models \ Permission::listPermissions (),
138+ 'permlist ' => Permission::getPermissions (),
83139 'permissions ' => $ subuser ->permissions ->mapWithKeys (function ($ item , $ key ) {
84140 return [$ item ->permission => true ];
85141 }),
@@ -93,63 +149,38 @@ public function view(Request $request, $uuid, $id)
93149 * @param string $uuid
94150 * @param int $id
95151 * @return \Illuminate\Http\RedirectResponse
152+ *
153+ * @throws \Illuminate\Auth\Access\AuthorizationException
154+ * @throws \Pterodactyl\Exceptions\DisplayException
155+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
156+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
96157 */
97158 public function update (Request $ request , $ uuid , $ id )
98159 {
99- $ server = Models \Server:: byUuid ( $ uuid );
160+ $ server = $ this -> session -> get ( ' server_data.model ' );
100161 $ this ->authorize ('edit-subuser ' , $ server );
101162
102- $ subuser = Models \Subuser::where ('server_id ' , $ server ->id )->findOrFail ($ id );
103-
104- try {
105- if ($ subuser ->user_id === Auth::user ()->id ) {
106- throw new DisplayException ('You are not authorized to edit you own account. ' );
107- }
108-
109- $ repo = new SubuserRepository ;
110- $ repo ->update ($ subuser ->id , [
111- 'permissions ' => $ request ->input ('permissions ' ),
112- 'server ' => $ server ->id ,
113- 'user ' => $ subuser ->user_id ,
114- ]);
115-
116- Alert::success ('Subuser permissions have successfully been updated. ' )->flash ();
117- } catch (DisplayValidationException $ ex ) {
118- return redirect ()->route ('server.subusers.view ' , [
119- 'uuid ' => $ uuid ,
120- 'id ' => $ id ,
121- ])->withErrors (json_decode ($ ex ->getMessage ()));
122- } catch (DisplayException $ ex ) {
123- Alert::danger ($ ex ->getMessage ())->flash ();
124- } catch (\Exception $ ex ) {
125- Log::error ($ ex );
126- Alert::danger ('An unknown error occured while attempting to update this subuser. ' )->flash ();
127- }
163+ $ this ->subuserUpdateService ->handle ($ id , $ request ->input ('permissions ' , []));
164+ $ this ->alert ->success (trans ('server.users.user_updated ' ))->flash ();
128165
129- return redirect ()->route ('server.subusers.view ' , [
130- 'uuid ' => $ uuid ,
131- 'id ' => $ id ,
132- ]);
166+ return redirect ()->route ('server.subusers.view ' , ['uuid ' => $ uuid , 'id ' => $ id ]);
133167 }
134168
135169 /**
136170 * Display new subuser creation page.
137171 *
138- * @param \Illuminate\Http\Request $request
139- * @param string $uuid
140172 * @return \Illuminate\View\View
173+ *
174+ * @throws \Illuminate\Auth\Access\AuthorizationException
141175 */
142- public function create (Request $ request , $ uuid )
176+ public function create ()
143177 {
144- $ server = Models \Server:: byUuid ( $ uuid );
178+ $ server = $ this -> session -> get ( ' server_data.model ' );
145179 $ this ->authorize ('create-subuser ' , $ server );
146- $ server ->js ();
147180
148- return view ('server.users.new ' , [
149- 'server ' => $ server ,
150- 'permissions ' => Models \Permission::listPermissions (),
151- 'node ' => $ server ->node ,
152- ]);
181+ $ this ->injectJavascript ();
182+
183+ return view ('server.users.new ' , ['permissions ' => Permission::getPermissions ()]);
153184 }
154185
155186 /**
@@ -158,64 +189,47 @@ public function create(Request $request, $uuid)
158189 * @param \Illuminate\Http\Request $request
159190 * @param string $uuid
160191 * @return \Illuminate\Http\RedirectResponse
192+ *
193+ * @throws \Exception
194+ * @throws \Illuminate\Auth\Access\AuthorizationException
195+ * @throws \Pterodactyl\Exceptions\DisplayException
196+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
197+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
198+ * @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
199+ * @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
161200 */
162201 public function store (Request $ request , $ uuid )
163202 {
164- $ server = Models \Server:: byUuid ( $ uuid );
203+ $ server = $ this -> session -> get ( ' server_data.model ' );
165204 $ this ->authorize ('create-subuser ' , $ server );
166205
167- try {
168- $ repo = new SubuserRepository ;
169- $ subuser = $ repo ->create ($ server ->id , $ request ->only ([
170- 'permissions ' , 'email ' ,
171- ]));
172- Alert::success ('Successfully created new subuser. ' )->flash ();
173-
174- return redirect ()->route ('server.subusers.view ' , [
175- 'uuid ' => $ uuid ,
176- 'id ' => $ subuser ->id ,
177- ]);
178- } catch (DisplayValidationException $ ex ) {
179- return redirect ()->route ('server.subusers.new ' , $ uuid )->withErrors (json_decode ($ ex ->getMessage ()))->withInput ();
180- } catch (DisplayException $ ex ) {
181- Alert::danger ($ ex ->getMessage ())->flash ();
182- } catch (\Exception $ ex ) {
183- Log::error ($ ex );
184- Alert::danger ('An unknown error occured while attempting to add a new subuser. ' )->flash ();
185- }
186-
187- return redirect ()->route ('server.subusers.new ' , $ uuid )->withInput ();
206+ $ subuser = $ this ->subuserCreationService ->handle ($ server , $ request ->input ('email ' ), $ request ->input ('permissions ' , []));
207+ $ this ->alert ->success (trans ('server.users.user_assigned ' ))->flash ();
208+
209+ return redirect ()->route ('server.subusers.view ' , [
210+ 'uuid ' => $ uuid ,
211+ 'id ' => $ subuser ->id ,
212+ ]);
188213 }
189214
190215 /**
191216 * Handles deleting a subuser.
192217 *
193- * @param \Illuminate\Http\Request $request
194- * @param string $uuid
195- * @param int $id
196- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
218+ * @param string $uuid
219+ * @param int $id
220+ * @return \Illuminate\Http\Response
221+ *
222+ * @throws \Illuminate\Auth\Access\AuthorizationException
223+ * @throws \Pterodactyl\Exceptions\DisplayException
224+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
197225 */
198- public function delete (Request $ request , $ uuid , $ id )
226+ public function delete ($ uuid , $ id )
199227 {
200- $ server = Models \Server:: byUuid ( $ uuid );
228+ $ server = $ this -> session -> get ( ' server_data.model ' );
201229 $ this ->authorize ('delete-subuser ' , $ server );
202230
203- try {
204- $ subuser = Models \Subuser::where ('server_id ' , $ server ->id )->findOrFail ($ id );
205-
206- $ repo = new SubuserRepository ;
207- $ repo ->delete ($ subuser ->id );
208-
209- return response ('' , 204 );
210- } catch (DisplayException $ ex ) {
211- response ()->json ([
212- 'error ' => $ ex ->getMessage (),
213- ], 422 );
214- } catch (\Exception $ ex ) {
215- Log::error ($ ex );
216- response ()->json ([
217- 'error ' => 'An unknown error occured while attempting to delete this subuser. ' ,
218- ], 503 );
219- }
231+ $ this ->subuserDeletionService ->handle ($ id );
232+
233+ return response ('' , 204 );
220234 }
221235}
0 commit comments