Skip to content

Commit

Permalink
Do not reset Default Redirect settings for users when saving Mailbox …
Browse files Browse the repository at this point in the history
…Permissions - closes #4339
  • Loading branch information
freescout-help-desk committed Nov 12, 2024
1 parent 2105320 commit 94c1c01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 27 additions & 1 deletion app/Http/Controllers/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Conversation;
use App\Folder;
use App\Mailbox;
use App\MailboxUser;
use App\Thread;
use App\User;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -269,8 +270,24 @@ public function permissionsSave($id, Request $request)
$this->authorize('updatePermissions', $mailbox);

$user = auth()->user();
$user_ids = \Eventy::filter('mailbox.permission_users', $request->users ?? [], $id);

// Get pivot data from mailbox_user to pass to sync() not to loose values.
// https://github.com/freescout-help-desk/freescout/issues/4339
$users_with_settings = [];
$mailbox_users = MailboxUser::where('mailbox_id', $id)
//->whereIn('user_id', $user_ids)
->get()
->keyBy('user_id')
->toArray();
foreach ($user_ids as $user_id) {
$users_with_settings[$user_id] = $mailbox_users[$user_id] ?? [];
}

// Settings for admins are being reset here.
// So we restore them below.
$mailbox->users()->sync($users_with_settings);

$mailbox->users()->sync(\Eventy::filter('mailbox.permission_users', $request->users, $id) ?: []);
$mailbox->syncPersonalFolders($request->users);

// Save admins settings.
Expand All @@ -282,6 +299,15 @@ public function permissionsSave($id, Request $request)
$admin->mailboxes()->attach($id);
$mailbox_user = $admin->mailboxesWithSettings()->where('mailbox_id', $id)->first();
}
// Restore settings for admins.
if (!empty($mailbox_users[$admin->id])) {
$admin_settings = $mailbox_users[$admin->id];
foreach (MailboxUser::$pivot_settings as $pivot_parameter) {
if (isset($admin_settings[$pivot_parameter])) {
$mailbox_user->settings->$pivot_parameter = $admin_settings[$pivot_parameter];
}
}
}
$mailbox_user->settings->hide = (isset($request->managers[$admin->id]['hide']) ? (int)$request->managers[$admin->id]['hide'] : false);
$mailbox_user->settings->save();
}
Expand Down
7 changes: 7 additions & 0 deletions app/MailboxUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class MailboxUser extends Model

public $timestamps = false;

public static $pivot_settings = [
'after_send',
'hide',
'mute',
'access',
];

// Does not work as we receive it via pivot
// protected $casts = [
// 'access' => 'array',
Expand Down

0 comments on commit 94c1c01

Please sign in to comment.