| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Models; |
| 4 | 4 | |
| 5 | +use Exception; | |
| 6 | +use FluentSupport\App\Modules\PermissionManager; | |
| 7 | +use FluentSupport\App\Services\EmailNotification\Settings; | |
| 8 | +use FluentSupport\App\Services\Helper; | |
| 5 | 9 | use FluentSupport\Framework\Support\Arr; |
| 6 | 10 | |
| 7 | 11 | class MailBox extends Model |
| 8 | 12 | { |
| @@ -29,8 +33,10 @@ | ||
| 29 | 33 | ]; |
| 30 | 34 | |
| 31 | 35 | public static function boot() |
| 32 | 36 | { |
| 37 | + parent::boot(); | |
| 38 | + | |
| 33 | 39 | static::creating(function ($model) { |
| 34 | 40 | $model->slug = static::slugify($model->name); |
| 35 | 41 | $model->settings = $model->settings ?: [ |
| 36 | 42 | 'admin_email_address' => '' |
| @@ -44,11 +50,46 @@ | ||
| 44 | 50 | } |
| 45 | 51 | |
| 46 | 52 | public function getSettingsAttribute($value) |
| 47 | 53 | { |
| 48 | - return \maybe_unserialize($this->attributes['settings']); | |
| 54 | + if (array_key_exists('settings', $this->attributes)) { | |
| 55 | + return Helper::safeUnserialize($this->attributes['settings']); | |
| 56 | + } | |
| 57 | + | |
| 58 | + return []; | |
| 49 | 59 | } |
| 50 | 60 | |
| 61 | + /** | |
| 62 | + * Returns the mailboxes the current agent is allowed to know about. | |
| 63 | + * | |
| 64 | + * Restricted inboxes are filtered out, so the `settings` blob only ever describes | |
| 65 | + * an inbox the agent already works in. Note that the restriction list is the only | |
| 66 | + * gate here: an agent with no restrictions receives `settings` — including | |
| 67 | + * `admin_email_address` — for every inbox, regardless of their other permissions. | |
| 68 | + * | |
| 69 | + * @param bool $withEmail Include the inbox address. Only pass true once the caller | |
| 70 | + * has confirmed the current user holds `fst_sensitive_data`. | |
| 71 | + * @return \FluentSupport\Framework\Database\Orm\Collection | |
| 72 | + */ | |
| 73 | + public static function getAccessibleBoxes($withEmail = false) | |
| 74 | + { | |
| 75 | + $columns = ['id', 'name', 'settings']; | |
| 76 | + | |
| 77 | + if ($withEmail) { | |
| 78 | + $columns[] = 'email'; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $query = MailBox::select($columns); | |
| 82 | + | |
| 83 | + $restrictedBoxes = PermissionManager::getRestrictedMailboxIds(); | |
| 84 | + | |
| 85 | + if ($restrictedBoxes) { | |
| 86 | + $query->whereNotIn('id', $restrictedBoxes); | |
| 87 | + } | |
| 88 | + | |
| 89 | + return $query->get(); | |
| 90 | + } | |
| 91 | + | |
| 51 | 92 | public static function slugify($title) |
| 52 | 93 | { |
| 53 | 94 | $slug = sanitize_title($title, 'business', 'display'); |
| 54 | 95 | if (MailBox::where('slug', $slug)->first()) { |
| @@ -70,8 +111,9 @@ | ||
| 70 | 111 | } |
| 71 | 112 | |
| 72 | 113 | // Set Reply-To Header |
| 73 | 114 | $headers[] = 'Reply-To: '. $fromString; |
| 115 | + $headers[] = 'In-Reply-To: '. $fromString; | |
| 74 | 116 | |
| 75 | 117 | return $headers; |
| 76 | 118 | } |
| 77 | 119 | |
| @@ -84,9 +126,9 @@ | ||
| 84 | 126 | ->where('key', $key) |
| 85 | 127 | ->first(); |
| 86 | 128 | |
| 87 | 129 | if($meta) { |
| 88 | - return maybe_unserialize($meta->value); | |
| 130 | + return Helper::safeUnserialize($meta->value); | |
| 89 | 131 | } |
| 90 | 132 | |
| 91 | 133 | return $default; |
| 92 | 134 | } |
| @@ -131,6 +173,61 @@ | ||
| 131 | 173 | |
| 132 | 174 | $meta = Meta::where('object_type', $class) |
| 133 | 175 | ->where('object_id', $this->id) |
| 134 | 176 | ->delete(); |
| 177 | + } | |
| 178 | + /** | |
| 179 | + * This `getMailBox` method is used to get a mailbox by id. | |
| 180 | + * @param int $id | |
| 181 | + * @return MailBox | |
| 182 | + */ | |
| 183 | + public function getMailBox ($id ) | |
| 184 | + { | |
| 185 | + return MailBox::findOrFail($id); | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * This `createMailBox` method is used to create a new MailBox. | |
| 190 | + * @param array $data | |
| 191 | + * @return MailBox | |
| 192 | + */ | |
| 193 | + public function createMailBox ( $data ) | |
| 194 | + { | |
| 195 | + if ($data['box_type'] == 'email') { | |
| 196 | + $data['settings'] = [ | |
| 197 | + 'admin_email_address' => '' | |
| 198 | + ]; | |
| 199 | + } else { | |
| 200 | + $data['settings'] = [ | |
| 201 | + 'admin_email_address' => $data['email'] | |
| 202 | + ]; | |
| 203 | + } | |
| 204 | + | |
| 205 | + if (!MailBox::first()) { | |
| 206 | + $data['is_default'] = 'yes'; | |
| 207 | + } | |
| 208 | + | |
| 209 | + return MailBox::create($data); | |
| 210 | + } | |
| 211 | + | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * This `updateMailBox` method is used to update a mailbox. | |
| 215 | + * @param array $data | |
| 216 | + * @param int $mailBoxId | |
| 217 | + * @return object $mailbox | |
| 218 | + */ | |
| 219 | + public function updateMailBox ($data, $mailBoxId ) | |
| 220 | + { | |
| 221 | + | |
| 222 | + $mailbox = MailBox::findOrFail($mailBoxId); | |
| 223 | + | |
| 224 | + if ($data['box_type'] == 'email' && empty($data['mapped_email'])) { | |
| 225 | + throw new \Exception('Mapped Email Address is required'); | |
| 226 | + } | |
| 227 | + | |
| 228 | + $mailbox->fill($data); | |
| 229 | + $mailbox->save(); | |
| 230 | + | |
| 231 | + return $mailbox; | |
| 135 | 232 | } |
| 136 | 233 | } |