forked from anonaddy/anonaddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alias.php
153 lines (128 loc) · 3.34 KB
/
Alias.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace App;
use App\Traits\HasEncryptedAttributes;
use App\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Alias extends Model
{
use SoftDeletes, HasUuid, HasEncryptedAttributes;
public $incrementing = false;
protected $keyType = 'string';
protected $encrypted = [
'description'
];
protected $fillable = [
'id',
'active',
'description',
'email',
'local_part',
'domain',
'aliasable_id',
'aliasable_type'
];
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
protected $casts = [
'id' => 'string',
'user_id' => 'string',
'aliasable_id' => 'string',
'aliasable_type' => 'string',
'active' => 'boolean'
];
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 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\Domain';
}
public function parentDomain()
{
return collect(config('anonaddy.all_domains'))
->filter(function ($name) {
return Str::endsWith($this->domain, $name);
})
->first();
}
}