187 lines
5.3 KiB
PHP
187 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Site;
|
|
use App\Models\TcpPort;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class TcpPortController extends Controller
|
|
{
|
|
/**
|
|
* Display overview of all TCP ports across all sites
|
|
*/
|
|
public function overview()
|
|
{
|
|
$sites = Site::where('user_id', Auth::id())
|
|
->withCount('tcpPorts')
|
|
->with(['tcpPorts' => function ($query) {
|
|
$query->where('is_active', true)->limit(5);
|
|
}])
|
|
->get();
|
|
|
|
$stats = [
|
|
'total_sites' => $sites->count(),
|
|
'total_ports_monitored' => $sites->sum('tcp_ports_count'),
|
|
'sites_with_ports' => $sites->where('tcp_ports_count', '>', 0)->count(),
|
|
];
|
|
|
|
return view('tcp-ports.overview', compact('sites', 'stats'));
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request, Site $site)
|
|
{
|
|
// Ensure the site belongs to the authenticated user
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$tcpPorts = $site->tcpPorts()
|
|
->orderBy('port')
|
|
->paginate(20);
|
|
|
|
return view('tcp-ports.index', compact('site', 'tcpPorts'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(Site $site)
|
|
{
|
|
// Ensure the site belongs to the authenticated user
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$commonPorts = TcpPort::getCommonPorts();
|
|
|
|
return view('tcp-ports.create', compact('site', 'commonPorts'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request, Site $site)
|
|
{
|
|
// Ensure the site belongs to the authenticated user
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'port' => ['required', 'integer', 'min:1', 'max:65535'],
|
|
'name' => ['nullable', 'string', 'max:255'],
|
|
'is_active' => ['boolean'],
|
|
]);
|
|
|
|
// Check if port already exists for this site
|
|
$exists = $site->tcpPorts()
|
|
->where('port', $validated['port'])
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return back()
|
|
->withInput()
|
|
->withErrors(['port' => 'This port is already being monitored for this site.']);
|
|
}
|
|
|
|
$site->tcpPorts()->create([
|
|
'port' => $validated['port'],
|
|
'name' => $validated['name'] ?? null,
|
|
'is_active' => $validated['is_active'] ?? true,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('tcp-ports.index', $site)
|
|
->with('success', 'TCP port added successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Site $site, TcpPort $tcpPort)
|
|
{
|
|
// Ensure the port belongs to the site and user
|
|
if ($tcpPort->site_id !== $site->id || $site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
return view('tcp-ports.show', compact('site', 'tcpPort'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Site $site, TcpPort $tcpPort)
|
|
{
|
|
// Ensure the port belongs to the site and user
|
|
if ($tcpPort->site_id !== $site->id || $site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$commonPorts = TcpPort::getCommonPorts();
|
|
|
|
return view('tcp-ports.edit', compact('site', 'tcpPort', 'commonPorts'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Site $site, TcpPort $tcpPort)
|
|
{
|
|
// Ensure the port belongs to the site and user
|
|
if ($tcpPort->site_id !== $site->id || $site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'port' => ['required', 'integer', 'min:1', 'max:65535'],
|
|
'name' => ['nullable', 'string', 'max:255'],
|
|
'is_active' => ['boolean'],
|
|
]);
|
|
|
|
// Check if port already exists for this site (excluding current)
|
|
$exists = $site->tcpPorts()
|
|
->where('port', $validated['port'])
|
|
->where('id', '!=', $tcpPort->id)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return back()
|
|
->withInput()
|
|
->withErrors(['port' => 'This port is already being monitored for this site.']);
|
|
}
|
|
|
|
$tcpPort->update([
|
|
'port' => $validated['port'],
|
|
'name' => $validated['name'] ?? null,
|
|
'is_active' => $validated['is_active'] ?? $tcpPort->is_active,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('tcp-ports.index', $site)
|
|
->with('success', 'TCP port updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Site $site, TcpPort $tcpPort)
|
|
{
|
|
// Ensure the port belongs to the site and user
|
|
if ($tcpPort->site_id !== $site->id || $site->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
|
|
$tcpPort->delete();
|
|
|
|
return redirect()
|
|
->route('tcp-ports.index', $site)
|
|
->with('success', 'TCP port deleted successfully.');
|
|
}
|
|
}
|