93 lines
3.4 KiB
PHP
93 lines
3.4 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Exception;
|
|
|
|
|
|
class SubscriptionServiceController extends Controller
|
|
{
|
|
public function create()
|
|
{
|
|
return view('services.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
// 1. Validate the incoming data
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'schema_type' => 'required|string|in:type_a,type_b', // Restrict to known schema mappers
|
|
'db_host' => 'required|string|max:255',
|
|
'db_port' => 'required|integer|min:1|max:65535',
|
|
'db_name' => 'required|string|max:255',
|
|
'db_user' => 'required|string|max:255',
|
|
'db_password' => 'required|string', // Plain text from form
|
|
]);
|
|
|
|
// 2. Insert into the database with encryption
|
|
DB::table('subscription_services')->insert([
|
|
'name' => $validated['name'],
|
|
'schema_type' => $validated['schema_type'],
|
|
'db_host' => $validated['db_host'],
|
|
'db_port' => $validated['db_port'],
|
|
'db_name' => $validated['db_name'],
|
|
'db_user' => $validated['db_user'],
|
|
'db_password' => Crypt::encryptString($validated['db_password']), // Secure encryption
|
|
'last_synced_at' => null, // Will be populated on first ETL run
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
// 3. Redirect back with a success message
|
|
return redirect()
|
|
->route('services.create')
|
|
->with('success', 'Subscription service added successfully.');
|
|
}
|
|
|
|
public function testConnection(Request $request)
|
|
{
|
|
// Validate the incoming AJAX payload
|
|
$request->validate([
|
|
'db_host' => 'required|string',
|
|
'db_port' => 'required|integer',
|
|
'db_name' => 'required|string',
|
|
'db_user' => 'required|string',
|
|
'db_password' => 'required|string',
|
|
]);
|
|
|
|
try {
|
|
// 1. Set a temporary dynamic configuration
|
|
Config::set('database.connections.test_dynamic', [
|
|
'driver' => 'mysql',
|
|
'host' => $request->db_host,
|
|
'port' => $request->db_port,
|
|
'database' => $request->db_name,
|
|
'username' => $request->db_user,
|
|
'password' => $request->db_password,
|
|
'charset' => 'utf8mb4',
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
]);
|
|
|
|
DB::purge('test_dynamic');
|
|
|
|
// 2. Attempt to resolve the PDO instance to verify the connection
|
|
DB::connection('test_dynamic')->getPdo();
|
|
|
|
// 3. Return success if no exception was thrown
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Connection established successfully.'
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Connection failed: ' . $e->getMessage()
|
|
], 400); // 400 Bad Request triggers the AJAX error block
|
|
}
|
|
}
|
|
} |