101 lines
3.1 KiB
PHP
101 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Domain extends Model
|
|
{
|
|
public $afterCommit = true;
|
|
|
|
protected $fillable = [
|
|
'domain','description','tags',
|
|
'is_active','is_system','is_server',
|
|
'max_aliases','max_mailboxes',
|
|
'default_quota_mb','max_quota_per_mailbox_mb','total_quota_mb',
|
|
'rate_limit_per_hour','rate_limit_override',
|
|
];
|
|
|
|
protected $casts = [
|
|
'tags' => 'array',
|
|
'is_active' => 'bool',
|
|
'is_system' => 'bool',
|
|
'is_server' => 'boolean',
|
|
'max_aliases' => 'int',
|
|
'max_mailboxes' => 'int',
|
|
'default_quota_mb' => 'int',
|
|
'max_quota_per_mailbox_mb' => 'int',
|
|
'total_quota_mb' => 'int',
|
|
'rate_limit_per_hour' => 'int',
|
|
'rate_limit_override' => 'bool',
|
|
];
|
|
|
|
protected $appends = ['dns_verified'];
|
|
|
|
public function mailUsers(): HasMany {
|
|
return $this->hasMany(MailUser::class)->orderBy('localpart');
|
|
}
|
|
public function mailAliases(): HasMany {
|
|
return $this->hasMany(MailAlias::class)->orderBy('local');
|
|
}
|
|
|
|
public function getDnsVerifiedAttribute(): bool
|
|
{
|
|
// Dummy-Regel: als verifiziert, wenn DKIM-Key existiert (nur Beispiel)
|
|
return $this->dkimKeys()->where('is_active', true)->exists();
|
|
}
|
|
// Praktisch fürs Listing
|
|
public function scopeWithMailStats($q) {
|
|
return $q->withCount(['mailUsers','mailAliases'])
|
|
->with(['mailUsers','mailAliases'])
|
|
->orderBy('domain');
|
|
}
|
|
|
|
// public function mailAliases(): HasMany
|
|
// {
|
|
// return $this->hasMany(MailAlias::class);
|
|
// }
|
|
//
|
|
// public function mailUsers(): HasMany
|
|
// {
|
|
// return $this->hasMany(MailUser::class);
|
|
// }
|
|
|
|
public function dkimKeys(): HasMany
|
|
{
|
|
return $this->hasMany(DkimKey::class);
|
|
}
|
|
|
|
public function spf(): HasMany
|
|
{
|
|
return $this->hasMany(SpfRecord::class);
|
|
}
|
|
|
|
public function dmarc(): HasMany
|
|
{
|
|
return $this->hasMany(DmarcRecord::class);
|
|
}
|
|
|
|
public function tlsaRecords()
|
|
{
|
|
return $this->hasMany(TlsaRecord::class);
|
|
}
|
|
|
|
public function getTagObjectsAttribute(): array
|
|
{
|
|
$raw = $this->tags ?? [];
|
|
$items = [];
|
|
foreach ($raw as $t) {
|
|
if (!is_array($t)) continue;
|
|
$label = trim((string)($t['label'] ?? '')); if ($label === '') continue;
|
|
$color = $this->normalizeHex($t['color'] ?? '') ?? '#22c55e';
|
|
$items[] = ['label'=>$label,'color'=>$color,
|
|
'bg'=>$this->toRgba($color,0.12),'border'=>$this->toRgba($color,0.35)];
|
|
}
|
|
return $items;
|
|
}
|
|
private function toRgba(string $hex, float $a): string { $hex=ltrim($hex,'#'); $r=hexdec(substr($hex,0,2)); $g=hexdec(substr($hex,2,2)); $b=hexdec(substr($hex,4,2)); return "rgba($r,$g,$b,$a)"; }
|
|
private function normalizeHex(?string $hex): ?string { $hex=trim((string)$hex); if($hex==='')return null; if($hex[0]!=='#')$hex="#$hex"; return preg_match('/^#[0-9a-fA-F]{6}$/',$hex)?strtolower($hex):null; }
|
|
}
|