mailwolt/app/Models/MailUser.php

62 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class MailUser extends Model
{
// protected $table = 'mail_users';
protected $fillable = [
'domain_id','localpart','email','display_name','password_hash',
'is_active','quota_mb','is_system'
];
protected $hidden = ['password_hash'];
protected $casts = [
'is_active'=>'bool',
'is_system' => 'boolean',
'quota_mb'=>'int',
'last_login_at'=>'datetime',
];
public function domain(): BelongsTo {
return $this->belongsTo(Domain::class);
}
public function setPasswordAttribute(string $plain): void {
// optional: allow 'password' virtual attribute
$this->attributes['password_hash'] = password_hash($plain, PASSWORD_BCRYPT);
}
// Ausgabe: vollständige Adresse
public function getEmailAttribute(): string {
return "{$this->local_part}@{$this->domain->name}";
}
public function getAddressAttribute(): string
{
$local = (string)($this->attributes['localpart'] ?? $this->localpart ?? '');
// bevorzugt den in Queries selektierten Alias `dom`, sonst Relation, sonst Roh-Attribut
$dom = (string)(
$this->attributes['dom']
?? ($this->relationLoaded('domain') ? ($this->domain->domain ?? '') : '')
?? ($this->attributes['domain'] ?? '')
);
if ($local !== '' && $dom !== '') return "{$local}@{$dom}";
if (($this->attributes['email'] ?? null) !== null) return (string)$this->attributes['email'];
return $dom !== '' ? "@{$dom}" : '';
}
// Scopes
public function scopeActive($q) { return $q->where('is_active', true); }
public function scopeSystem($q) { return $q->where('is_system', true); }
public function scopeByEmail($q, string $email) { return $q->where('email', $email); }
}