56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Models\Caller_DB;
|
|
class PostHandler extends Controller
|
|
{
|
|
public function caller_add(Request $request)
|
|
{
|
|
try {
|
|
|
|
$data = $request->validate([
|
|
"node_name" => "required|string",
|
|
"whitelist_ip" => "nullable|string",
|
|
"token" => "required|string",
|
|
]);
|
|
|
|
$data["status"] = "active";
|
|
$user = Auth::user();
|
|
|
|
$caller = new Caller_DB();
|
|
$caller->node_name = $data["node_name"];
|
|
$caller->whitelist_ip = $data["whitelist_ip"];
|
|
if ($data["whitelist_ip"]) {
|
|
$ips = explode(',', $data["whitelist_ip"]);
|
|
foreach ($ips as $ip) {
|
|
$ip = trim($ip);
|
|
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
throw new \Exception('Invalid IPv4 format.');
|
|
}
|
|
}
|
|
$caller->whitelist_ip = implode(',', $ips);
|
|
} else {
|
|
$caller->whitelist_ip = null;
|
|
}
|
|
$caller->token = $data["token"];
|
|
$caller->status = $data["status"];
|
|
$caller->created_by = $user->name;
|
|
$caller->updated_by = $user->name;
|
|
$caller->save();
|
|
|
|
return response()->json([
|
|
"message" => "Caller added successfully.",
|
|
"redirect" => "3000",
|
|
], 200);
|
|
|
|
} catch (\Illuminate\Validation\ValidationException $exception) {
|
|
return response()->json(['error' => $exception->errors()], 422);
|
|
} catch (\Exception $exception) {
|
|
return response()->json(['error' => 'An unexpected error occurred.' . $exception->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|