1: <?php
2:
3: namespace Aurora\Modules\Mail\Models;
4:
5: use Aurora\Modules\Core\Models\Tenant;
6: use Aurora\Modules\Mail\Models\MailAccount;
7: use Aurora\System\Classes\Model;
8:
9: /**
10: * Aurora\Modules\Mail\Models\Server
11: *
12: * @property integer $Id
13: * @property integer $TenantId
14: * @property string $Name
15: * @property string $IncomingServer
16: * @property integer $IncomingPort
17: * @property boolean $IncomingUseSsl
18: * @property string $OutgoingServer
19: * @property integer $OutgoingPort
20: * @property boolean $OutgoingUseSsl
21: * @property string $SmtpAuthType
22: * @property string $SmtpLogin
23: * @property string $SmtpPassword
24: * @property string $OwnerType
25: * @property string|null $Domains
26: * @property boolean $EnableSieve
27: * @property integer $SievePort
28: * @property boolean $EnableThreading
29: * @property boolean $UseFullEmailAddressAsLogin
30: * @property boolean $SetExternalAccessServers
31: * @property string $ExternalAccessImapServer
32: * @property integer $ExternalAccessImapPort
33: * @property integer $ExternalAccessImapAlterPort
34: * @property string $ExternalAccessSmtpServer
35: * @property integer $ExternalAccessSmtpPort
36: * @property integer $ExternalAccessSmtpAlterPort
37: * @property string $ExternalAccessPop3Server
38: * @property integer $ExternalAccessPop3Port
39: * @property integer $ExternalAccessPop3AlterPort
40: * @property boolean $OAuthEnable
41: * @property string $OAuthName
42: * @property string $OAuthType
43: * @property string $OAuthIconUrl
44: * @property \Illuminate\Support\Carbon|null $CreatedAt
45: * @property \Illuminate\Support\Carbon|null $UpdatedAt
46: * @property boolean $ExternalAccessImapUseSsl
47: * @property boolean $ExternalAccessPop3UseSsl
48: * @property boolean $ExternalAccessSmtpUseSsl
49: * @property-read \Illuminate\Database\Eloquent\Collection<int, MailAccount> $MailAccounts
50: * @property-read int|null $mail_accounts_count
51: * @property-read mixed $entity_id
52: * @property-read mixed $server_id
53: * @method static int count(string $columns = '*')
54: * @method static \Illuminate\Database\Eloquent\Builder|Server find(int|string $id, array|string $columns = ['*'])
55: * @method static \Illuminate\Database\Eloquent\Builder|Server findOrFail(int|string $id, mixed $id, Closure|array|string $columns = ['*'], Closure $callback = null)
56: * @method static \Illuminate\Database\Eloquent\Builder|Server first(array|string $columns = ['*'])
57: * @method static \Illuminate\Database\Eloquent\Builder|Server firstWhere(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
58: * @method static \Illuminate\Database\Eloquent\Builder|Server newModelQuery()
59: * @method static \Illuminate\Database\Eloquent\Builder|Server newQuery()
60: * @method static \Illuminate\Database\Eloquent\Builder|Server query()
61: * @method static \Illuminate\Database\Eloquent\Builder|Server leftJoin(string $table, \Closure|string $first, string|null $operator = null, string|null $second = null)
62: * @method static \Illuminate\Database\Eloquent\Builder|Server where(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
63: * @method static \Illuminate\Database\Eloquent\Builder|Server whereIn(string $column, mixed $values, string $boolean = 'and', bool $not = false)
64: * @method static \Illuminate\Database\Eloquent\Builder|Server orWhere(\Closure|array|string|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null)
65: * @method static \Illuminate\Database\Eloquent\Builder|Server whereCreatedAt($value)
66: * @method static \Illuminate\Database\Eloquent\Builder|Server whereDomains($value)
67: * @method static \Illuminate\Database\Eloquent\Builder|Server whereEnableSieve($value)
68: * @method static \Illuminate\Database\Eloquent\Builder|Server whereEnableThreading($value)
69: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessImapAlterPort($value)
70: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessImapPort($value)
71: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessImapServer($value)
72: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessImapUseSsl($value)
73: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessPop3AlterPort($value)
74: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessPop3Port($value)
75: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessPop3Server($value)
76: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessPop3UseSsl($value)
77: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessSmtpAlterPort($value)
78: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessSmtpPort($value)
79: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessSmtpServer($value)
80: * @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalAccessSmtpUseSsl($value)
81: * @method static \Illuminate\Database\Eloquent\Builder|Server whereId($value)
82: * @method static \Illuminate\Database\Eloquent\Builder|Server whereIncomingPort($value)
83: * @method static \Illuminate\Database\Eloquent\Builder|Server whereIncomingServer($value)
84: * @method static \Illuminate\Database\Eloquent\Builder|Server whereIncomingUseSsl($value)
85: * @method static \Illuminate\Database\Eloquent\Builder|Server whereName($value)
86: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOAuthEnable($value)
87: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOAuthIconUrl($value)
88: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOAuthName($value)
89: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOAuthType($value)
90: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOutgoingPort($value)
91: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOutgoingServer($value)
92: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOutgoingUseSsl($value)
93: * @method static \Illuminate\Database\Eloquent\Builder|Server whereOwnerType($value)
94: * @method static \Illuminate\Database\Eloquent\Builder|Server whereSetExternalAccessServers($value)
95: * @method static \Illuminate\Database\Eloquent\Builder|Server whereSievePort($value)
96: * @method static \Illuminate\Database\Eloquent\Builder|Server whereSmtpAuthType($value)
97: * @method static \Illuminate\Database\Eloquent\Builder|Server whereSmtpLogin($value)
98: * @method static \Illuminate\Database\Eloquent\Builder|Server whereSmtpPassword($value)
99: * @method static \Illuminate\Database\Eloquent\Builder|Server whereTenantId($value)
100: * @method static \Illuminate\Database\Eloquent\Builder|Server whereUpdatedAt($value)
101: * @method static \Illuminate\Database\Eloquent\Builder|Server whereUseFullEmailAddressAsLogin($value)
102: * @mixin \Eloquent
103: */
104: class Server extends Model
105: {
106: protected $table = 'mail_servers';
107:
108: protected $foreignModel = Tenant::class;
109: protected $foreignModelIdColumn = 'TenantId'; // Column that refers to an external table
110:
111: /**
112: * The attributes that are mass assignable.
113: *
114: * @var array
115: */
116: protected $fillable = [
117: 'Id',
118: 'TenantId',
119: 'Name',
120: 'IncomingServer',
121: 'IncomingPort',
122: 'IncomingUseSsl',
123: 'OutgoingServer',
124: 'OutgoingPort',
125: 'OutgoingUseSsl',
126: 'SmtpAuthType',
127: 'SmtpLogin',
128: 'SmtpPassword',
129: 'OwnerType',
130: 'Domains',
131: 'EnableSieve',
132: 'SievePort',
133: 'EnableThreading',
134: 'UseFullEmailAddressAsLogin',
135:
136: 'SetExternalAccessServers',
137: 'ExternalAccessImapServer',
138: 'ExternalAccessImapPort',
139: 'ExternalAccessImapAlterPort',
140: 'ExternalAccessImapUseSsl',
141: 'ExternalAccessPop3Server',
142: 'ExternalAccessPop3Port',
143: 'ExternalAccessPop3AlterPort',
144: 'ExternalAccessPop3UseSsl',
145: 'ExternalAccessSmtpServer',
146: 'ExternalAccessSmtpPort',
147: 'ExternalAccessSmtpAlterPort',
148: 'ExternalAccessSmtpUseSsl',
149:
150: 'OAuthEnable',
151: 'OAuthName',
152: 'OAuthType',
153: 'OAuthIconUrl',
154: ];
155:
156: /**
157: * The attributes that should be hidden for arrays.
158: *
159: * @var array
160: */
161: protected $hidden = [
162: ];
163:
164: protected $casts = [
165: 'Properties' => 'array',
166: 'IncomingUseSsl' => 'boolean',
167: 'OutgoingUseSsl' => 'boolean',
168: 'EnableSieve' => 'boolean',
169: 'EnableThreading' => 'boolean',
170: 'UseFullEmailAddressAsLogin' => 'boolean',
171: 'SetExternalAccessServers' => 'boolean',
172: 'OAuthEnable' => 'boolean',
173: 'SmtpPassword' => \Aurora\System\Casts\Encrypt::class,
174: 'ExternalAccessImapUseSsl' => 'boolean',
175: 'ExternalAccessPop3UseSsl' => 'boolean',
176: 'ExternalAccessSmtpUseSsl' => 'boolean',
177: ];
178:
179: protected $attributes = [
180: ];
181:
182: protected $appends = [
183: 'EntityId',
184: 'ServerId',
185: ];
186:
187: public function getServerIdAttribute()
188: {
189: return $this->Id;
190: }
191:
192: public function MailAccounts()
193: {
194: return $this->hasMany(MailAccount::class, 'ServerId', 'Id');
195: }
196:
197: public function toResponseArray()
198: {
199: $aResponse = parent::toResponseArray();
200: $aResponse['ServerId'] = $this->Id;
201:
202: $aArgs = [];
203: \Aurora\System\Api::GetModule('Mail')->broadcastEvent(
204: 'ServerToResponseArray',
205: $aArgs,
206: $aResponse
207: );
208: return $aResponse;
209: }
210:
211: public function getOrphanIds()
212: {
213: if (!$this->foreignModel || !$this->foreignModelIdColumn) {
214: return ['status' => -1, 'message' => 'Foreign field doesn\'t exist'];
215: }
216: $tableName = $this->getTable();
217: $foreignObject = new $this->foreignModel();
218: $foreignTable = $foreignObject->getTable();
219: $foreignPK = $foreignObject->primaryKey;
220:
221: // DB::enableQueryLog();
222: $oAccount = new MailAccount();
223: $accountTable = $oAccount->getTable();
224:
225: $serversWithoutAccount = self::leftJoin($accountTable, "$accountTable.ServerId", '=', "$tableName.$this->primaryKey")->where('OwnerType', '=', 'account')->whereNull("$accountTable.Id")->groupBy("$tableName.$this->primaryKey")->pluck("$tableName.$this->primaryKey")->all();
226: $orphanIds = self::where('OwnerType', '=', 'tenant')->pluck($this->primaryKey)->diff(
227: self::leftJoin($foreignTable, "$tableName.$this->foreignModelIdColumn", '=', "$foreignTable.$foreignPK")->whereNotNull("$foreignTable.$foreignPK")->pluck("$tableName.$this->primaryKey")
228: )->union($serversWithoutAccount)->all();
229: $message = $orphanIds ? "$tableName table has orphans." : "Orphans were not found.";
230: $oResult = ['status' => $orphanIds ? 1 : 0, 'message' => $message, 'orphansIds' => $orphanIds];
231: // dd(DB::getQueryLog());
232:
233: return $oResult;
234: }
235: }
236: