Skip to content

Commit

Permalink
Merge pull request #11 from liberu-genealogy/main
Browse files Browse the repository at this point in the history
DNS
  • Loading branch information
curtisdelicata authored Jun 16, 2024
2 parents d5f2cda + 1562252 commit 81d50d4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/Filament/Admin/Resources/DnsSettingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('domain_id')
->required()
->numeric(),
Forms\Components\TextInput::make('record_type')
->required()
->maxLength(255),
Forms\Components\Select::make('record_type')
->options([
'A' => 'A',
'MX' => 'MX',
])
->required(),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Expand Down
43 changes: 43 additions & 0 deletions app/Listeners/UpdateBindDnsRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Listeners;

use App\Events\DnsSettingSaved;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class UpdateBindDnsRecords implements ShouldQueue
{
/**
* Handle the event.
*
* @param \App\Events\DnsSettingSaved $event
* @return void
*/
public function handle(DnsSettingSaved $event)
{
$dnsSetting = $event->dnsSetting;

try {
$domain = $dnsSetting->domain->name;
$recordType = $dnsSetting->record_type;
$recordName = $dnsSetting->name;
$recordValue = $dnsSetting->value;
$ttl = $dnsSetting->ttl;

$zoneFile = "./bind/records/{$domain}.db";

if ($recordType === 'A') {
$recordLine = "{$recordName} {$ttl} IN A {$recordValue}";
} elseif ($recordType === 'MX') {
$recordLine = "{$domain}. {$ttl} IN MX 10 {$recordValue}.";
}

file_put_contents($zoneFile, $recordLine . "\n", FILE_APPEND);

exec('docker-compose restart bind9');
} catch (\Exception $e) {
Log::error('Failed to update Bind9 DNS records: ' . $e->getMessage());
}
}
}
22 changes: 22 additions & 0 deletions app/Models/DnsSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

namespace App\Models;

use App\Events\DnsSettingSaved;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DnsSetting extends Model
{
use HasFactory;

/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::saved(function ($dnsSetting) {
event(new DnsSettingSaved($dnsSetting));
});
}

/**
* The table associated with the model.
*
Expand All @@ -28,6 +41,15 @@ class DnsSetting extends Model
'value',
'ttl',
];

/**
* The validation rules for the model attributes.
*
* @var array
*/
public $rules = [
'record_type' => 'required|in:A,MX',
];

/**
* The attributes that should be cast.
Expand Down
29 changes: 29 additions & 0 deletions app/Providers/DnsSettingSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Events;

use App\Models\DnsSetting;
use Illuminate\Foundation\Events\Dispatchable;

class DnsSettingSaved
{
use Dispatchable;

/**
* The DnsSetting instance.
*
* @var \App\Models\DnsSetting
*/
public $dnsSetting;

/**
* Create a new event instance.
*
* @param \App\Models\DnsSetting $dnsSetting
* @return void
*/
public function __construct(DnsSetting $dnsSetting)
{
$this->dnsSetting = $dnsSetting;
}
}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class EventServiceProvider extends ServiceProvider
{
use App\Events\DnsSettingSaved;
use App\Listeners\UpdateBindDnsRecords;

/**
* The event to listener mappings for the application.
*
Expand All @@ -18,6 +21,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
DnsSettingSaved::class => [
UpdateBindDnsRecords::class,
],
];

/**
Expand Down

0 comments on commit 81d50d4

Please sign in to comment.