1 | <?php |
||
2 | |||
3 | namespace Ikechukwukalu\Dynamicmailconfig\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Illuminate\Support\Facades\Auth; |
||
8 | use Illuminate\Support\Facades\Crypt; |
||
9 | |||
10 | class UserEmailConfiguration extends Model |
||
11 | { |
||
12 | use HasFactory; |
||
13 | |||
14 | protected $hidden = [ |
||
15 | 'name', |
||
16 | 'address', |
||
17 | 'driver', |
||
18 | 'host', |
||
19 | 'port', |
||
20 | 'encryption', |
||
21 | 'username', |
||
22 | 'password' |
||
23 | ]; |
||
24 | |||
25 | public function scopeConfiguredEmail($query) |
||
26 | { |
||
27 | return $query->where('user_id', Auth::user()->id); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
28 | } |
||
29 | |||
30 | public function getEmailConfig(): array |
||
31 | { |
||
32 | return [ |
||
33 | 'driver' => $this->driver, |
||
34 | 'host' => $this->host, |
||
35 | 'port' => $this->port, |
||
36 | 'from' => ['address' => $this->address, 'name' => $this->name], |
||
37 | 'encryption' => $this->encryption, |
||
38 | 'username' => $this->username, |
||
39 | 'password' => $this->password |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | public function setDriverAttribute($value) |
||
44 | { |
||
45 | return $this->encryptField('driver', $value); |
||
46 | } |
||
47 | |||
48 | public function setHostAttribute($value) |
||
49 | { |
||
50 | return $this->encryptField('host', $value); |
||
51 | } |
||
52 | |||
53 | public function setPortAttribute($value) |
||
54 | { |
||
55 | return $this->encryptField('port', $value); |
||
56 | } |
||
57 | |||
58 | public function setEncryptionAttribute($value) |
||
59 | { |
||
60 | return $this->encryptField('encryption', $value); |
||
61 | } |
||
62 | |||
63 | public function setUserNameAttribute($value) |
||
64 | { |
||
65 | return $this->encryptField('username', $value); |
||
66 | } |
||
67 | |||
68 | public function setPasswordAttribute($value) |
||
69 | { |
||
70 | return $this->encryptField('password', $value); |
||
71 | } |
||
72 | |||
73 | public function setNameAttribute($value) |
||
74 | { |
||
75 | return $this->encryptField('name', $value); |
||
76 | } |
||
77 | |||
78 | public function setAddressAttribute($value) |
||
79 | { |
||
80 | return $this->encryptField('address', $value); |
||
81 | } |
||
82 | |||
83 | public function getDriverAttribute($value) |
||
84 | { |
||
85 | return $this->decryptField($value); |
||
86 | } |
||
87 | |||
88 | public function getHostAttribute($value) |
||
89 | { |
||
90 | return $this->decryptField($value); |
||
91 | } |
||
92 | |||
93 | public function getPortAttribute($value) |
||
94 | { |
||
95 | return $this->decryptField($value); |
||
96 | } |
||
97 | |||
98 | public function getEncryptionAttribute($value) |
||
99 | { |
||
100 | return $this->decryptField($value); |
||
101 | } |
||
102 | |||
103 | public function getUserNameAttribute($value) |
||
104 | { |
||
105 | return $this->decryptField($value); |
||
106 | } |
||
107 | |||
108 | public function getPasswordAttribute($value) |
||
109 | { |
||
110 | return $this->decryptField($value); |
||
111 | } |
||
112 | |||
113 | public function getNameAttribute($value) |
||
114 | { |
||
115 | return $this->decryptField($value); |
||
116 | } |
||
117 | |||
118 | public function getAddressAttribute($value) |
||
119 | { |
||
120 | return $this->decryptField($value); |
||
121 | } |
||
122 | |||
123 | private function encryptField(string $field, $value) |
||
124 | { |
||
125 | if (config('dynamicmailconfig.hash', true)) { |
||
126 | return $this->attributes[$field] = Crypt::encryptString($value); |
||
127 | } |
||
128 | |||
129 | return $value; |
||
130 | } |
||
131 | |||
132 | private function decryptField($value) |
||
133 | { |
||
134 | if (config('dynamicmailconfig.hash', true)) { |
||
135 | return Crypt::decryptString($value); |
||
136 | } |
||
137 | |||
138 | return $value; |
||
139 | } |
||
140 | } |
||
141 |