Alias.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App;
  3. use App\Traits\HasEncryptedAttributes;
  4. use App\Traits\HasUuid;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. class Alias extends Model
  8. {
  9. use SoftDeletes, HasUuid, HasEncryptedAttributes;
  10. public $incrementing = false;
  11. protected $keyType = 'string';
  12. protected $encrypted = [
  13. 'description'
  14. ];
  15. protected $fillable = [
  16. 'id',
  17. 'active',
  18. 'description',
  19. 'email',
  20. 'local_part',
  21. 'domain',
  22. 'aliasable_id',
  23. 'aliasable_type'
  24. ];
  25. protected $dates = [
  26. 'created_at',
  27. 'updated_at',
  28. 'deleted_at'
  29. ];
  30. protected $casts = [
  31. 'id' => 'string',
  32. 'user_id' => 'string',
  33. 'aliasable_id' => 'string',
  34. 'aliasable_type' => 'string',
  35. 'active' => 'boolean'
  36. ];
  37. public function setLocalPartAttribute($value)
  38. {
  39. $this->attributes['local_part'] = strtolower($value);
  40. }
  41. public function setDomainAttribute($value)
  42. {
  43. $this->attributes['domain'] = strtolower($value);
  44. }
  45. public function setEmailAttribute($value)
  46. {
  47. $this->attributes['email'] = strtolower($value);
  48. }
  49. /**
  50. * Get the user for the email alias.
  51. */
  52. public function user()
  53. {
  54. return $this->belongsTo(User::class);
  55. }
  56. /**
  57. * Get the owning aliasable model.
  58. */
  59. public function aliasable()
  60. {
  61. return $this->morphTo();
  62. }
  63. /**
  64. * Get the recipients for the email alias.
  65. */
  66. public function recipients()
  67. {
  68. return $this->belongsToMany(Recipient::class, 'alias_recipients')->withPivot('id')->using(AliasRecipient::class);
  69. }
  70. /**
  71. * Get all of the verified recipients for the email alias.
  72. */
  73. public function verifiedRecipients()
  74. {
  75. return $this->recipients()->whereNotNull('email_verified_at');
  76. }
  77. /**
  78. * Get the verified recipients for the email alias or the default recipient if none are set.
  79. */
  80. public function verifiedRecipientsOrDefault()
  81. {
  82. if ($this->verifiedRecipients()->count() === 0) {
  83. // If the alias is for a custom domain or additional username that has a default recipient set.
  84. if (isset($this->aliasable->defaultRecipient)) {
  85. return $this->aliasable->defaultRecipient();
  86. }
  87. return $this->user->defaultRecipient();
  88. }
  89. return $this
  90. ->verifiedRecipients()
  91. ->get();
  92. }
  93. /**
  94. * Deactivate the alias.
  95. */
  96. public function deactivate()
  97. {
  98. $this->update(['active' => false]);
  99. }
  100. /**
  101. * Activate the alias.
  102. */
  103. public function activate()
  104. {
  105. $this->update(['active' => true]);
  106. }
  107. }