refactor: simplify suspension method, better support for permanent suspensions

This commit is contained in:
Miguel Nogueira 2022-08-19 01:13:40 +01:00
parent 1ed2a0f5e1
commit 0f4ce2d7b0
No known key found for this signature in database
GPG Key ID: 3C6A7E29AF26D370
2 changed files with 17 additions and 19 deletions

View File

@ -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();

View File

@ -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;
}
/**