174 lines
5.8 KiB
PHP
174 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\DnsRecord;
|
|
use App\Models\Site;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\View\View;
|
|
|
|
class DnsRecordController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the DNS records for a site.
|
|
*/
|
|
public function index(Site $site): View
|
|
{
|
|
// Ensure the user owns the site
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403, 'Unauthorized access to this site.');
|
|
}
|
|
|
|
$dnsRecords = $site->dnsRecords()
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
return view('dns-records.index', compact('site', 'dnsRecords'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new DNS record.
|
|
*/
|
|
public function create(Site $site): View
|
|
{
|
|
// Ensure the user owns the site
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403, 'Unauthorized access to this site.');
|
|
}
|
|
|
|
$recordTypes = DnsRecord::getRecordTypes();
|
|
|
|
return view('dns-records.create', compact('site', 'recordTypes'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created DNS record in storage.
|
|
*/
|
|
public function store(Request $request, Site $site): RedirectResponse
|
|
{
|
|
// Ensure the user owns the site
|
|
if ($site->user_id !== Auth::id()) {
|
|
abort(403, 'Unauthorized access to this site.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'domain' => ['required', 'string', 'max:255'],
|
|
'record_type' => ['required', 'string', 'in:A,AAAA,CNAME,MX,TXT,NS,SOA,PTR,SRV'],
|
|
'expected_value' => ['nullable', 'string', 'max:1000'],
|
|
'is_active' => ['boolean'],
|
|
]);
|
|
|
|
// Check for duplicate record
|
|
$exists = $site->dnsRecords()
|
|
->where('domain', $validated['domain'])
|
|
->where('record_type', $validated['record_type'])
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return back()
|
|
->withInput()
|
|
->withErrors(['domain' => "A {$validated['record_type']} record for {$validated['domain']} already exists for this site."]);
|
|
}
|
|
|
|
$dnsRecord = $site->dnsRecords()->create([
|
|
'domain' => $validated['domain'],
|
|
'record_type' => $validated['record_type'],
|
|
'expected_value' => $validated['expected_value'] ?? null,
|
|
'is_active' => $validated['is_active'] ?? true,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('dns-records.show', [$site, $dnsRecord])
|
|
->with('success', 'DNS record created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified DNS record.
|
|
*/
|
|
public function show(Site $site, DnsRecord $dnsRecord): View
|
|
{
|
|
// Ensure the user owns the site and the DNS record belongs to the site
|
|
if ($site->user_id !== Auth::id() || $dnsRecord->site_id !== $site->id) {
|
|
abort(403, 'Unauthorized access to this DNS record.');
|
|
}
|
|
|
|
return view('dns-records.show', compact('site', 'dnsRecord'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified DNS record.
|
|
*/
|
|
public function edit(Site $site, DnsRecord $dnsRecord): View
|
|
{
|
|
// Ensure the user owns the site and the DNS record belongs to the site
|
|
if ($site->user_id !== Auth::id() || $dnsRecord->site_id !== $site->id) {
|
|
abort(403, 'Unauthorized access to this DNS record.');
|
|
}
|
|
|
|
$recordTypes = DnsRecord::getRecordTypes();
|
|
|
|
return view('dns-records.edit', compact('site', 'dnsRecord', 'recordTypes'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified DNS record in storage.
|
|
*/
|
|
public function update(Request $request, Site $site, DnsRecord $dnsRecord): RedirectResponse
|
|
{
|
|
// Ensure the user owns the site and the DNS record belongs to the site
|
|
if ($site->user_id !== Auth::id() || $dnsRecord->site_id !== $site->id) {
|
|
abort(403, 'Unauthorized access to this DNS record.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'domain' => ['required', 'string', 'max:255'],
|
|
'record_type' => ['required', 'string', 'in:A,AAAA,CNAME,MX,TXT,NS,SOA,PTR,SRV'],
|
|
'expected_value' => ['nullable', 'string', 'max:1000'],
|
|
'is_active' => ['boolean'],
|
|
]);
|
|
|
|
// Check for duplicate record (excluding current record)
|
|
$exists = $site->dnsRecords()
|
|
->where('domain', $validated['domain'])
|
|
->where('record_type', $validated['record_type'])
|
|
->where('id', '!=', $dnsRecord->id)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return back()
|
|
->withInput()
|
|
->withErrors(['domain' => "A {$validated['record_type']} record for {$validated['domain']} already exists for this site."]);
|
|
}
|
|
|
|
$dnsRecord->update([
|
|
'domain' => $validated['domain'],
|
|
'record_type' => $validated['record_type'],
|
|
'expected_value' => $validated['expected_value'] ?? null,
|
|
'is_active' => $validated['is_active'] ?? $dnsRecord->is_active,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('dns-records.show', [$site, $dnsRecord])
|
|
->with('success', 'DNS record updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified DNS record from storage.
|
|
*/
|
|
public function destroy(Site $site, DnsRecord $dnsRecord): RedirectResponse
|
|
{
|
|
// Ensure the user owns the site and the DNS record belongs to the site
|
|
if ($site->user_id !== Auth::id() || $dnsRecord->site_id !== $site->id) {
|
|
abort(403, 'Unauthorized access to this DNS record.');
|
|
}
|
|
|
|
$dnsRecord->delete();
|
|
|
|
return redirect()
|
|
->route('dns-records.index', $site)
|
|
->with('success', 'DNS record deleted successfully.');
|
|
}
|
|
}
|