Browse Source

added the ability to edit users pterodactyl id

AVMG20 4 years ago
parent
commit
7c644b3960

+ 44 - 30
app/Classes/Pterodactyl.php

@@ -3,7 +3,6 @@
 namespace App\Classes;
 namespace App\Classes;
 
 
 use App\Models\Egg;
 use App\Models\Egg;
-use App\Models\Location;
 use App\Models\Nest;
 use App\Models\Nest;
 use App\Models\Node;
 use App\Models\Node;
 use App\Models\Server;
 use App\Models\Server;
@@ -14,9 +13,33 @@ use Illuminate\Support\Facades\Http;
 
 
 class Pterodactyl
 class Pterodactyl
 {
 {
+    /**
+     * @return PendingRequest
+     */
+    public static function client()
+    {
+        return Http::withHeaders([
+            'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
+            'Content-type'  => 'application/json',
+            'Accept'        => 'Application/vnd.pterodactyl.v1+json',
+        ])->baseUrl(env('PTERODACTYL_URL') . '/api');
+    }
+
+    /**
+     * Get user by pterodactyl id
+     * @param int $pterodactylId
+     * @return mixed
+     */
+    public static function getUser(int $pterodactylId){
+        $response = self::client()->get("/application/users/{$pterodactylId}");
+        if ($response->failed()) return null;
+        return $response->json()['attributes'];
+    }
+
     /**
     /**
      * @param Node $node
      * @param Node $node
      * @return array|mixed|null
      * @return array|mixed|null
+     * @throws Exception
      */
      */
     public static function getFreeAllocations(Node $node)
     public static function getFreeAllocations(Node $node)
     {
     {
@@ -98,17 +121,6 @@ class Pterodactyl
         return $response->json();
         return $response->json();
     }
     }
 
 
-    /**
-     * @return PendingRequest
-     */
-    public static function client()
-    {
-        return Http::withHeaders([
-            'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
-            'Content-type' => 'application/json',
-            'Accept' => 'Application/vnd.pterodactyl.v1+json',
-        ])->baseUrl(env('PTERODACTYL_URL') . '/api');
-    }
 
 
     /**
     /**
      * @param String $route
      * @param String $route
@@ -125,41 +137,43 @@ class Pterodactyl
      * @param Node $node
      * @param Node $node
      * @return Response
      * @return Response
      */
      */
-    public static function createServer(Server $server , Egg $egg , Node $node)
+    public static function createServer(Server $server, Egg $egg, Node $node)
     {
     {
         return self::client()->post("/application/servers", [
         return self::client()->post("/application/servers", [
-            "name" => $server->name,
-            "external_id" => $server->id,
-            "user" => $server->user->pterodactyl_id,
-            "egg" => $egg->id,
-            "docker_image" => $egg->docker_image,
-            "startup" => $egg->startup,
-            "environment" => $egg->getEnvironmentVariables(),
-            "limits" => [
+            "name"           => $server->name,
+            "external_id"    => $server->id,
+            "user"           => $server->user->pterodactyl_id,
+            "egg"            => $egg->id,
+            "docker_image"   => $egg->docker_image,
+            "startup"        => $egg->startup,
+            "environment"    => $egg->getEnvironmentVariables(),
+            "limits"         => [
                 "memory" => $server->product->memory,
                 "memory" => $server->product->memory,
-                "swap" => $server->product->swap,
-                "disk" => $server->product->disk,
-                "io" => $server->product->io,
-                "cpu" => $server->product->cpu
+                "swap"   => $server->product->swap,
+                "disk"   => $server->product->disk,
+                "io"     => $server->product->io,
+                "cpu"    => $server->product->cpu
             ],
             ],
             "feature_limits" => [
             "feature_limits" => [
-                "databases" => $server->product->databases,
-                "backups" => $server->product->backups,
+                "databases"   => $server->product->databases,
+                "backups"     => $server->product->backups,
                 "allocations" => 1
                 "allocations" => 1
             ],
             ],
-            "allocation" => [
+            "allocation"     => [
                 "default" => Pterodactyl::getFreeAllocationId($node)
                 "default" => Pterodactyl::getFreeAllocationId($node)
             ]
             ]
         ]);
         ]);
     }
     }
 
 
-    public static function suspendServer(Server $server){
+    public static function suspendServer(Server $server)
+    {
         $response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
         $response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
         if ($response->failed()) throw self::getException();
         if ($response->failed()) throw self::getException();
         return $response;
         return $response;
     }
     }
 
 
-    public static function unSuspendServer(Server $server){
+    public static function unSuspendServer(Server $server)
+    {
         $response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
         $response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
         if ($response->failed()) throw self::getException();
         if ($response->failed()) throw self::getException();
         return $response;
         return $response;

+ 17 - 6
app/Http/Controllers/Admin/UserController.php

@@ -2,8 +2,10 @@
 
 
 namespace App\Http\Controllers\Admin;
 namespace App\Http\Controllers\Admin;
 
 
+use App\Classes\Pterodactyl;
 use App\Http\Controllers\Controller;
 use App\Http\Controllers\Controller;
 use App\Models\User;
 use App\Models\User;
+use Exception;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\View\Factory;
 use Illuminate\Contracts\View\Factory;
 use Illuminate\Contracts\View\View;
 use Illuminate\Contracts\View\View;
@@ -12,6 +14,7 @@ use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Validation\Rule;
 use Illuminate\Validation\Rule;
+use Illuminate\Validation\ValidationException;
 
 
 class UserController extends Controller
 class UserController extends Controller
 {
 {
@@ -79,17 +82,25 @@ class UserController extends Controller
      * @param Request $request
      * @param Request $request
      * @param User $user
      * @param User $user
      * @return RedirectResponse
      * @return RedirectResponse
+     * @throws Exception
      */
      */
     public function update(Request $request, User $user)
     public function update(Request $request, User $user)
     {
     {
         $request->validate([
         $request->validate([
-            "name"         => "required|string|min:4|max:30",
-            "email"        => "required|string|email",
-            "credits"      => "required|numeric|min:0|max:1000000",
-            "server_limit" => "required|numeric|min:0|max:1000000",
-            "role"         => Rule::in(['admin', 'mod', 'client', 'member']),
+            "name"           => "required|string|min:4|max:30",
+            "pterodactyl_id" => "required|numeric|unique:users,pterodactyl_id,{$user->pterodactyl_id}",
+            "email"          => "required|string|email",
+            "credits"        => "required|numeric|min:0|max:1000000",
+            "server_limit"   => "required|numeric|min:0|max:1000000",
+            "role"           => Rule::in(['admin', 'mod', 'client', 'member']),
         ]);
         ]);
 
 
+        if (is_null(Pterodactyl::getUser($request->input('pterodactyl_id')))){
+            throw ValidationException::withMessages([
+                'pterodactyl_id' => ["User does not exists on pterodactyl's panel"]
+            ]);
+        }
+
         $user->update($request->all());
         $user->update($request->all());
 
 
         return redirect()->route('admin.users.index')->with('success', 'User updated!');
         return redirect()->route('admin.users.index')->with('success', 'User updated!');
@@ -146,7 +157,7 @@ class UserController extends Controller
 
 
     /**
     /**
      *
      *
-     * @throws \Exception
+     * @throws Exception
      */
      */
     public function dataTable()
     public function dataTable()
     {
     {

+ 14 - 0
resources/views/admin/users/edit.blade.php

@@ -51,6 +51,20 @@
                                     </div>
                                     </div>
                                     @enderror
                                     @enderror
                                 </div>
                                 </div>
+                                <div class="form-group">
+                                    <label for="pterodactyl_id">Pterodactyl ID</label>
+                                    <input value="{{$user->pterodactyl_id}}" id="pterodactyl_id" name="pterodactyl_id" type="number"
+                                           class="form-control @error('pterodactyl_id') is-invalid @enderror" required="required">
+                                    @error('pterodactyl_id')
+                                    <div class="invalid-feedback">
+                                        {{$message}}
+                                    </div>
+                                    @enderror
+                                    <div class="text-muted">
+                                        This ID refers to the user account created on pterodactyl's panel. <br>
+                                        <small>Only edit this if you know what you're doing :)</small>
+                                    </div>
+                                </div>
                                 <div class="form-group">
                                 <div class="form-group">
                                     <label for="credits">Credits</label>
                                     <label for="credits">Credits</label>
                                     <input value="{{$user->credits}}" id="credits" name="credits" step="any" min="0" max="1000000"
                                     <input value="{{$user->credits}}" id="credits" name="credits" step="any" min="0" max="1000000"