Skip to content

Commit

Permalink
Create User::deleteUser() method
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Oct 3, 2024
1 parent 3f90a9b commit eb5c8fa
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 89 deletions.
90 changes: 1 addition & 89 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Events\UserDeleted;
use App\Folder;
use App\Mailbox;
use App\Subscription;
Expand Down Expand Up @@ -491,94 +490,7 @@ public function ajax(Request $request)

if (!$response['msg']) {

// We have to process conversations one by one to move them to Unassigned folder,
// as conversations may be in different mailboxes
// $user->conversations()->update(['user_id' => null, 'folder_id' => ]);
$mailbox_unassigned_folders = [];

$user->conversations->each(function ($conversation) use ($auth_user, $request) {
// We don't fire ConversationUserChanged event to avoid sending notifications to users
if (!empty($request->assign_user)
&& !empty($request->assign_user[$conversation->mailbox_id])
&& (int) $request->assign_user[$conversation->mailbox_id] != -1
) {
// Set assignee.
// In this case conversation stays assigned, just assignee changes.
$conversation->user_id = $request->assign_user[$conversation->mailbox_id];

} else {

// Make convesation Unassigned.

// Unset assignee.
// Maybe use changeUser() here.
$conversation->user_id = null;

if ($conversation->isPublished()
&& ($conversation->isActive() || $conversation->isPending())
) {
// Change conversation folder to UNASSIGNED.
$folder_id = null;
if (!empty($mailbox_unassigned_folders[$conversation->mailbox_id])) {
$folder_id = $mailbox_unassigned_folders[$conversation->mailbox_id];
} else {
$folder = $conversation->mailbox->folders()
->where('type', Folder::TYPE_UNASSIGNED)
->first();

if ($folder) {
$folder_id = $folder->id;
$mailbox_unassigned_folders[$conversation->mailbox_id] = $folder_id;
}
}
if ($folder_id) {
$conversation->folder_id = $folder_id;
}
}
}

$conversation->save();

// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_USER_CHANGED;
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $conversation->customer_id;
$thread->created_by_user_id = $auth_user->id;
$thread->save();
});

// Recalculate counters for folders
//if ($user->isAdmin()) {
// Admin has access to all mailboxes
Mailbox::all()->each(function ($mailbox) {
$mailbox->updateFoldersCounters();
});
// } else {
// $user->mailboxes->each(function ($mailbox) {
// $mailbox->updateFoldersCounters();
// });
// }

// Disconnect user from mailboxes.
$user->mailboxes()->sync([]);
$user->folders()->delete();

$user->status = \App\User::STATUS_DELETED;
// Update email.
$email_suffix = User::EMAIL_DELETED_SUFFIX.date('YmdHis');
// We have to truncate email to avoid "Data too long" error.
$user->email = mb_substr($user->email, 0, User::EMAIL_MAX_LENGTH - mb_strlen($email_suffix)).$email_suffix;

$user->save();

event(new UserDeleted($user, $auth_user));
$user->deleteUser($auth_user, $request->assign_user);

\Session::flash('flash_success_floating', __('User deleted').': '.$user->getFullName());

Expand Down
93 changes: 93 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App;

use App\Email;
use App\Events\UserDeleted;
use App\Follower;
use App\Mail\PasswordChanged;
use App\Mail\UserInvite;
Expand Down Expand Up @@ -1234,4 +1235,96 @@ public function canSeeOnlyAssignedConversations()
{
return $this->hasManageMailboxPermission(0, Mailbox::ACCESS_PERM_ASSIGNED);
}

public function deleteUser($auth_user, $assign_user)
{
// We have to process conversations one by one to move them to Unassigned folder,
// as conversations may be in different mailboxes
// $this->conversations()->update(['user_id' => null, 'folder_id' => ]);
$mailbox_unassigned_folders = [];

$this->conversations->each(function ($conversation) use ($auth_user, $assign_user) {
// We don't fire ConversationUserChanged event to avoid sending notifications to users
if (!empty($assign_user)
&& !empty($assign_user[$conversation->mailbox_id])
&& (int) $assign_user[$conversation->mailbox_id] != -1
) {
// Set assignee.
// In this case conversation stays assigned, just assignee changes.
$conversation->user_id = $assign_user[$conversation->mailbox_id];

} else {

// Make convesation Unassigned.

// Unset assignee.
// Maybe use changeUser() here.
$conversation->user_id = null;

if ($conversation->isPublished()
&& ($conversation->isActive() || $conversation->isPending())
) {
// Change conversation folder to UNASSIGNED.
$folder_id = null;
if (!empty($mailbox_unassigned_folders[$conversation->mailbox_id])) {
$folder_id = $mailbox_unassigned_folders[$conversation->mailbox_id];
} else {
$folder = $conversation->mailbox->folders()
->where('type', Folder::TYPE_UNASSIGNED)
->first();

if ($folder) {
$folder_id = $folder->id;
$mailbox_unassigned_folders[$conversation->mailbox_id] = $folder_id;
}
}
if ($folder_id) {
$conversation->folder_id = $folder_id;
}
}
}

$conversation->save();

// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_USER_CHANGED;
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $conversation->customer_id;
$thread->created_by_user_id = $auth_user->id;
$thread->save();
});

// Recalculate counters for folders
//if ($this->isAdmin()) {
// Admin has access to all mailboxes
Mailbox::all()->each(function ($mailbox) {
$mailbox->updateFoldersCounters();
});
// } else {
// $this->mailboxes->each(function ($mailbox) {
// $mailbox->updateFoldersCounters();
// });
// }

// Disconnect user from mailboxes.
$this->mailboxes()->sync([]);
$this->folders()->delete();

$this->status = \App\User::STATUS_DELETED;
// Update email.
$email_suffix = User::EMAIL_DELETED_SUFFIX.date('YmdHis');
// We have to truncate email to avoid "Data too long" error.
$this->email = mb_substr($this->email, 0, User::EMAIL_MAX_LENGTH - mb_strlen($email_suffix)).$email_suffix;

$this->save();

event(new UserDeleted($this, $auth_user));
}
}

0 comments on commit eb5c8fa

Please sign in to comment.