1: <?php
2:
3: namespace Aurora\Modules\Mail\Models;
4:
5: use Aurora\Modules\Core\Models\User;
6: use Aurora\Modules\Mail\Module;
7: use Aurora\System\Classes\Account as SystemAccount;
8:
9: /**
10: * Aurora\Modules\Mail\Models\MailAccount
11: *
12: * @property integer $Id
13: * @property boolean $IsDisabled
14: * @property integer $IdUser
15: * @property boolean $UseToAuthorize
16: * @property string $Email
17: * @property string $FriendlyName
18: * @property string $IncomingLogin
19: * @property string $IncomingPassword
20: * @property boolean $IncludeInUnifiedMailbox
21: * @property boolean $UseSignature
22: * @property mixed|null $Signature
23: * @property integer $ServerId
24: * @property string|null $FoldersOrder
25: * @property boolean $UseThreading
26: * @property boolean $SaveRepliesToCurrFolder
27: * @property boolean $ShowUnifiedMailboxLabel
28: * @property string $UnifiedMailboxLabelText
29: * @property string $UnifiedMailboxLabelColor
30: * @property string|null $XOAuth
31: * @property \Illuminate\Support\Carbon|null $CreatedAt
32: * @property \Illuminate\Support\Carbon|null $UpdatedAt
33: * @property array|null $Properties
34: * @property-read \Aurora\Modules\Mail\Models\Server $Server
35: * @property-read mixed $entity_id
36: * @method static int count(string $columns = '*')
37: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount find(int|string $id, array|string $columns = ['*'])
38: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount findOrFail(int|string $id, mixed $id, Closure|array|string $columns = ['*'], Closure $callback = null)
39: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount first(array|string $columns = ['*'])
40: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount firstWhere(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
41: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount newModelQuery()
42: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount newQuery()
43: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount query()
44: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount where(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
45: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereCreatedAt($value)
46: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereEmail($value)
47: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereFoldersOrder($value)
48: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereFriendlyName($value)
49: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereId($value)
50: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereIdUser($value)
51: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Mail\Models\MailAccount whereIn(string $column, mixed $values, string $boolean = 'and', bool $not = false)
52: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereIncludeInUnifiedMailbox($value)
53: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereIncomingLogin($value)
54: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereIncomingPassword($value)
55: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereIsDisabled($value)
56: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereProperties($value)
57: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereSaveRepliesToCurrFolder($value)
58: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereServerId($value)
59: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereShowUnifiedMailboxLabel($value)
60: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereSignature($value)
61: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUnifiedMailboxLabelColor($value)
62: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUnifiedMailboxLabelText($value)
63: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUpdatedAt($value)
64: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUseSignature($value)
65: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUseThreading($value)
66: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereUseToAuthorize($value)
67: * @method static \Illuminate\Database\Eloquent\Builder|MailAccount whereXOAuth($value)
68: * @mixin \Eloquent
69: */
70: class MailAccount extends SystemAccount
71: {
72: /**
73: * The attributes that are mass assignable.
74: *
75: * @var array
76: */
77: protected $foreignModel = User::class;
78: protected $foreignModelIdColumn = 'IdUser'; // Column that refers to an external table
79: protected $fillable = [
80: 'Id',
81: 'IsDisabled',
82: 'IdUser',
83: 'UseToAuthorize',
84: 'Email',
85: 'FriendlyName',
86: 'IncomingLogin',
87: 'IncomingPassword',
88: 'UseSignature',
89: 'Signature',
90: 'ServerId',
91: 'FoldersOrder',
92: 'UseThreading',
93: 'SaveRepliesToCurrFolder',
94: 'IncludeInUnifiedMailbox',
95: 'ShowUnifiedMailboxLabel',
96: 'UnifiedMailboxLabelText',
97: 'UnifiedMailboxLabelColor',
98: 'XOAuth'
99: ];
100:
101: /**
102: * The attributes that should be hidden for arrays.
103: *
104: * @var array
105: */
106: protected $hidden = [
107: ];
108:
109: protected $casts = [
110: 'Properties' => 'array',
111: 'IncomingPassword' => \Aurora\System\Casts\Encrypt::class,
112: 'IsDisabled' => 'boolean',
113: 'UseToAuthorize' => 'boolean',
114: 'IncludeInUnifiedMailbox' => 'boolean',
115: 'UseSignature' => 'boolean',
116: 'ShowUnifiedMailboxLabel' => 'boolean',
117: 'UseThreading' => 'boolean',
118: 'SaveRepliesToCurrFolder' => 'boolean',
119: 'FoldersOrder' => 'string'
120: ];
121:
122: protected $attributes = [
123: ];
124:
125: protected $appends = [
126: 'EntityId'
127: ];
128:
129: public function setPassword($sPassword)
130: {
131: $this->IncomingPassword = $sPassword;
132: }
133:
134: public function getPassword()
135: {
136: return $this->IncomingPassword;
137: }
138:
139: private function canBeUsedToAuthorize()
140: {
141: $oMailModule = \Aurora\System\Api::GetModule('Mail');
142: if ($oMailModule instanceof Module) {
143: return !$oMailModule->getAccountsManager()->useToAuthorizeAccountExists($this->Email, $this->Id);
144: } else {
145: return false;
146: }
147: }
148:
149: public function getDefaultTimeOffset()
150: {
151: return 0;
152: }
153:
154: public function toResponseArray()
155: {
156: $aResponse = parent::toResponseArray();
157: $aResponse['AccountID'] = $this->Id;
158: $aResponse['AllowFilters'] = false;
159: $aResponse['AllowForward'] = false;
160: $aResponse['AllowAutoresponder'] = false;
161: $aResponse['EnableAllowBlockLists'] = false;
162:
163: if (!isset($aResponse['Signature'])) {
164: $aResponse['Signature'] = '';
165: }
166:
167: $oServer = $this->getServer();
168: if ($oServer instanceof Server) {
169: $oMailModule = Module::getInstance();
170: if ($oServer->EnableSieve && $oMailModule) {
171: $aResponse['AllowFilters'] = $oMailModule->oModuleSettings->AllowFilters;
172: $aResponse['AllowForward'] = $oMailModule->oModuleSettings->AllowForward;
173: $aResponse['AllowAutoresponder'] = $oMailModule->oModuleSettings->AllowAutoresponder;
174: $aResponse['EnableAllowBlockLists'] = $oMailModule->oModuleSettings->EnableAllowBlockLists;
175: }
176: }
177:
178: $aResponse['CanBeUsedToAuthorize'] = $this->canBeUsedToAuthorize();
179:
180: $aArgs = ['Account' => $this];
181: \Aurora\System\Api::GetModule('Core')->broadcastEvent(
182: 'Mail::Account::ToResponseArray',
183: $aArgs,
184: $aResponse
185: );
186:
187: return $aResponse;
188: }
189:
190: public function getServer()
191: {
192: return $this->Server;
193: }
194:
195: public function getLogin()
196: {
197: $oServer = $this->getServer();
198: if ($oServer && !$oServer->UseFullEmailAddressAsLogin) {
199: return $this->Email;
200: }
201: return $this->IncomingLogin;
202: }
203:
204: public function Server()
205: {
206: return $this->belongsTo(Server::class, 'ServerId', 'Id');
207: }
208: }
209: