From 0f4ce2d7b06392ca3424395dbc1d5d14a39d4399 Mon Sep 17 00:00:00 2001 From: miguel456 Date: Fri, 19 Aug 2022 01:13:40 +0100 Subject: [PATCH] refactor: simplify suspension method, better support for permanent suspensions --- app/Http/Controllers/UserController.php | 18 +++++++++++------- app/Services/AccountSuspensionService.php | 18 ++++++------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index efec9db..62743c5 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -471,14 +471,18 @@ class UserController extends Controller $this->authorize('create', [Ban::class, $user]); $this->disable(); - if (!$suspensionService->isSuspended($user)) { + if ($suspensionService->isSuspended($user)) + { + return redirect() + ->back() + ->with('error', __('Account already suspended.')); + } - $suspensionService->suspend($user, $request->reason, $user, $request->suspensionType); - $request->session()->flash('success', __('Account suspended.')); - - } else { - - $request->session()->flash('error', __('Account already suspended!')); + if ($request->suspensionType = "on") { + $suspensionService->suspend($user, $request->reason, $request->duration); + } + else { + $suspensionService->suspend($user, $request->reason); } return redirect()->back(); diff --git a/app/Services/AccountSuspensionService.php b/app/Services/AccountSuspensionService.php index bff8ba2..1598219 100755 --- a/app/Services/AccountSuspensionService.php +++ b/app/Services/AccountSuspensionService.php @@ -16,18 +16,14 @@ class AccountSuspensionService /** * Suspends a user account, with given $reason. - * - * This method will take the target user and add a suspension to the database, - * effectively banning the user from the app. Suspensions may be temporary or permanent. - * Suspensions also block registration attempts. + * Permanent if no duration given. * * @param string $reason Suspension reason. * @param int $duration Duration in days * @param User $target Who to suspend. - * @param string $type Permanent or temporary? * @return Ban The ban itself */ - public function suspend(User $target, string $reason, int $duration = 0, string $type = "on"): Ban { + public function suspend(User $target, string $reason, int $duration = 0): Ban { Log::alert("An user account has just been suspended.", [ 'taget_email' => $target->email, @@ -35,19 +31,17 @@ class AccountSuspensionService 'reason' => $reason ]); - if ($type == "on") { + if ($duration > 0) { $expiryDate = now()->addDays($duration); } - $ban = Ban::create([ + return Ban::create([ 'userID' => $target->id, 'reason' => $reason, - 'bannedUntil' => ($type == "on") ? $expiryDate->format('Y-m-d H:i:s') : null, + 'bannedUntil' => ($duration > 0) ? $expiryDate->format('Y-m-d H:i:s') : null, 'authorUserID' => Auth::user()->id, - 'isPermanent' => ($type == "off") ? true : false + 'isPermanent' => ($duration == 0) ? true : false ]); - - return $ban; } /**