Bozhidar Slaveykov 1 rok temu
rodzic
commit
9373b21241

+ 22 - 0
bin/mysql-create-db-and-user.sh

@@ -0,0 +1,22 @@
+#!/bin/bash
+
+echo "Creating MySQL user and database"
+
+PASS=$3
+if [ -z "$3" ]; then
+PASS=`openssl rand -base64 8`
+fi
+
+mysql -u root <<MYSQL_SCRIPT
+CREATE DATABASE $1;
+CREATE USER '$2'@'localhost' IDENTIFIED BY '$PASS';
+GRANT ALL PRIVILEGES ON $1.* TO '$2'@'localhost';
+FLUSH PRIVILEGES;
+MYSQL_SCRIPT
+
+echo "MySQL user and database created."
+echo "Database: $1"
+echo "Username: $2"
+echo "Password: $PASS"
+echo "Success!"
+

+ 31 - 2
web/app/Filament/Resources/HostingDatabaseResource.php

@@ -23,7 +23,31 @@ class HostingDatabaseResource extends Resource
     {
         return $form
             ->schema([
-                //
+
+                Forms\Components\TextInput::make('database_name')
+                    ->required()
+                    ->disabled(function ($record) {
+                        if (isset($record->exists)) {
+                            return $record->exists;
+                        } else {
+                            return false;
+                        }
+                    }),
+
+                Forms\Components\TextInput::make('database_username')
+                    ->required()
+                    ->disabled(function ($record) {
+                        if (isset($record->exists)) {
+                            return $record->exists;
+                        } else {
+                            return false;
+                        }
+                    }),
+
+                Forms\Components\TextInput::make('database_password')
+                    ->required(),
+
+
             ]);
     }
 
@@ -31,7 +55,12 @@ class HostingDatabaseResource extends Resource
     {
         return $table
             ->columns([
-                //
+                Tables\Columns\TextColumn::make('database_name')
+                    ->searchable()
+                    ->sortable(),
+                Tables\Columns\TextColumn::make('database_username')
+                    ->searchable()
+                    ->sortable(),
             ])
             ->filters([
                 //

+ 67 - 2
web/app/Models/HostingDatabase.php

@@ -2,10 +2,75 @@
 
 namespace App\Models;
 
-use Illuminate\Database\Eloquent\Factories\HasFactory;
+use App\ShellApi;
+use Sushi\Sushi;
 use Illuminate\Database\Eloquent\Model;
 
 class HostingDatabase extends Model
 {
-    use HasFactory;
+    use Sushi;
+
+    protected $fillable = [
+        'database_name',
+        'database_username',
+        'database_password'
+    ];
+
+    protected $schema = [
+        'database_name' => 'string',
+        'database_username' => 'string',
+        'database_password' => 'string'
+    ];
+
+    public static function boot()
+    {
+        parent::boot();
+
+        static::creating(function ($model) {
+
+            $createDbAndUser = ShellApi::callBin('mysql-create-db-and-user', [
+                $model->database_name,
+                $model->database_username,
+                $model->database_password
+            ]);
+
+            if (empty($createDbAndUser)) {
+                return false;
+            }
+
+        });
+
+        static::deleting(function ($model) {
+
+        });
+    }
+
+    protected function sushiShouldCache()
+    {
+        return true;
+    }
+
+    public function getRows()
+    {
+//        $websitesList = ShellApi::callBin('nginx-websites-list');
+//
+//        $rows = [];
+//        if (!empty($websitesList)) {
+//            $websitesList = json_decode($websitesList, true);
+//            if (!empty($websitesList)) {
+//                foreach ($websitesList as $website) {
+//                    if (isset($website['file'])) {
+//                        $rows[] = [
+//                            'file' => $website['file'],
+//                            'domain' => $website['server_name'],
+//                            'root' => $website['root']
+//                        ];
+//                    }
+//                }
+//            }
+//        }
+
+        return [];
+    }
+
 }