'string', 'user_id' => 'string', 'aliasable_id' => 'string', 'aliasable_type' => 'string', 'active' => 'boolean' ]; public static function boot() { parent::boot(); // Deactivate the alias when it is deleted Alias::deleting(function ($alias) { if ($alias->active) { $alias->deactivate(); } }); // Activate the alias when it is restored Alias::restoring(function ($alias) { $alias->activate(); }); } public function setLocalPartAttribute($value) { $this->attributes['local_part'] = strtolower($value); } public function setDomainAttribute($value) { $this->attributes['domain'] = strtolower($value); } public function setEmailAttribute($value) { $this->attributes['email'] = strtolower($value); } /** * Get the user for the email alias. */ public function user() { return $this->belongsTo(User::class); } /** * Get the owning aliasable model. */ public function aliasable() { return $this->morphTo(); } /** * Get the recipients for the email alias. */ public function recipients() { return $this->belongsToMany(Recipient::class, 'alias_recipients')->withPivot('id')->using(AliasRecipient::class); } /** * Get all of the aliases' failed deliveries. */ public function failedDeliveries() { return $this->hasMany(FailedDelivery::class); } /** * Get all of the verified recipients for the email alias. */ public function verifiedRecipients() { return $this->recipients()->whereNotNull('email_verified_at'); } /** * Get the verified recipients for the email alias or the default recipient if none are set. */ public function verifiedRecipientsOrDefault() { if ($this->verifiedRecipients()->count() === 0) { // If the alias is for a custom domain or additional username that has a default recipient set. if (isset($this->aliasable->defaultRecipient)) { return $this->aliasable->defaultRecipient(); } return $this->user->defaultRecipient(); } return $this ->verifiedRecipients() ->get(); } /** * Deactivate the alias. */ public function deactivate() { $this->update(['active' => false]); } /** * Activate the alias. */ public function activate() { $this->update(['active' => true]); } public function isUuid() { return $this->id === $this->local_part; } public function hasSharedDomain() { return in_array($this->domain, config('anonaddy.all_domains')); } public function isCustomDomain() { return $this->aliasable_type === 'App\Models\Domain'; } public function parentDomain() { return collect(config('anonaddy.all_domains')) ->filter(function ($name) { return Str::endsWith($this->domain, $name); }) ->first(); } }