refactor: simplify suspension method, better support for permanent suspensions

This commit is contained in:
2022-08-19 01:13:40 +01:00
parent 1ed2a0f5e1
commit 0f4ce2d7b0
2 changed files with 17 additions and 19 deletions

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