| 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: | class Server extends Model |
| 10: | { |
| 11: | protected $table = 'mail_servers'; |
| 12: | |
| 13: | protected $foreignModel = Tenant::class; |
| 14: | protected $foreignModelIdColumn = 'TenantId'; |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | protected $fillable = [ |
| 22: | 'Id', |
| 23: | 'TenantId', |
| 24: | 'Name', |
| 25: | 'IncomingServer', |
| 26: | 'IncomingPort', |
| 27: | 'IncomingUseSsl', |
| 28: | 'OutgoingServer', |
| 29: | 'OutgoingPort', |
| 30: | 'OutgoingUseSsl', |
| 31: | 'SmtpAuthType', |
| 32: | 'SmtpLogin', |
| 33: | 'SmtpPassword', |
| 34: | 'OwnerType', |
| 35: | 'Domains', |
| 36: | 'EnableSieve', |
| 37: | 'SievePort', |
| 38: | 'EnableThreading', |
| 39: | 'UseFullEmailAddressAsLogin', |
| 40: | |
| 41: | 'SetExternalAccessServers', |
| 42: | 'ExternalAccessImapServer', |
| 43: | 'ExternalAccessImapPort', |
| 44: | 'ExternalAccessImapAlterPort', |
| 45: | 'ExternalAccessImapUseSsl', |
| 46: | 'ExternalAccessPop3Server', |
| 47: | 'ExternalAccessPop3Port', |
| 48: | 'ExternalAccessPop3AlterPort', |
| 49: | 'ExternalAccessPop3UseSsl', |
| 50: | 'ExternalAccessSmtpServer', |
| 51: | 'ExternalAccessSmtpPort', |
| 52: | 'ExternalAccessSmtpAlterPort', |
| 53: | 'ExternalAccessSmtpUseSsl', |
| 54: | |
| 55: | 'OAuthEnable', |
| 56: | 'OAuthName', |
| 57: | 'OAuthType', |
| 58: | 'OAuthIconUrl', |
| 59: | ]; |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | protected $hidden = [ |
| 67: | ]; |
| 68: | |
| 69: | protected $casts = [ |
| 70: | 'IncomingUseSsl' => 'boolean', |
| 71: | 'OutgoingUseSsl' => 'boolean', |
| 72: | 'EnableSieve' => 'boolean', |
| 73: | 'EnableThreading' => 'boolean', |
| 74: | 'UseFullEmailAddressAsLogin' => 'boolean', |
| 75: | 'SetExternalAccessServers' => 'boolean', |
| 76: | 'OAuthEnable' => 'boolean', |
| 77: | 'SmtpPassword' => \Aurora\System\Casts\Encrypt::class, |
| 78: | 'ExternalAccessImapUseSsl' => 'boolean', |
| 79: | 'ExternalAccessPop3UseSsl' => 'boolean', |
| 80: | 'ExternalAccessSmtpUseSsl' => 'boolean', |
| 81: | ]; |
| 82: | |
| 83: | protected $attributes = [ |
| 84: | ]; |
| 85: | |
| 86: | protected $appends = [ |
| 87: | 'EntityId', |
| 88: | 'ServerId', |
| 89: | ]; |
| 90: | |
| 91: | public function getServerIdAttribute() |
| 92: | { |
| 93: | return $this->Id; |
| 94: | } |
| 95: | |
| 96: | public function MailAccounts() |
| 97: | { |
| 98: | return $this->hasMany(MailAccount::class, 'ServerId', 'Id'); |
| 99: | } |
| 100: | |
| 101: | public function toResponseArray() |
| 102: | { |
| 103: | $aResponse = parent::toResponseArray(); |
| 104: | $aResponse['ServerId'] = $this->Id; |
| 105: | |
| 106: | $aArgs = []; |
| 107: | \Aurora\System\Api::GetModule('Mail')->broadcastEvent( |
| 108: | 'ServerToResponseArray', |
| 109: | $aArgs, |
| 110: | $aResponse |
| 111: | ); |
| 112: | return $aResponse; |
| 113: | } |
| 114: | |
| 115: | public function getOrphanIds() |
| 116: | { |
| 117: | if (!$this->foreignModel || !$this->foreignModelIdColumn) { |
| 118: | return ['status' => -1, 'message' => 'Foreign field doesn\'t exist']; |
| 119: | } |
| 120: | $tableName = $this->getTable(); |
| 121: | $foreignObject = new $this->foreignModel(); |
| 122: | $foreignTable = $foreignObject->getTable(); |
| 123: | $foreignPK = $foreignObject->primaryKey; |
| 124: | |
| 125: | |
| 126: | $oAccount = new MailAccount(); |
| 127: | $accountTable = $oAccount->getTable(); |
| 128: | $serversWithoutAccount = self::leftJoin($accountTable, "$accountTable.ServerId", '=', "$tableName.$this->primaryKey")->where('OwnerType', '=', 'account')->whereNull("$accountTable.Id")->groupBy("$tableName.$this->primaryKey")->pluck("$tableName.$this->primaryKey")->all(); |
| 129: | $orphanIds = self::where('OwnerType', '=', 'tenant')->pluck($this->primaryKey)->diff( |
| 130: | self::leftJoin($foreignTable, "$tableName.$this->foreignModelIdColumn", '=', "$foreignTable.$foreignPK")->whereNotNull("$foreignTable.$foreignPK")->pluck("$tableName.$this->primaryKey") |
| 131: | )->union($serversWithoutAccount)->all(); |
| 132: | $message = $orphanIds ? "$tableName table has orphans." : "Orphans were not found."; |
| 133: | $oResult = ['status' => $orphanIds ? 1 : 0, 'message' => $message, 'orphansIds' => $orphanIds]; |
| 134: | |
| 135: | |
| 136: | return $oResult; |
| 137: | } |
| 138: | } |
| 139: | |