Refactored ban system

Implemented a Reddit-like account suspension system (similar to subreddit bans). This makes it easier to ban users from the app, and the code has also been cleaned up.

The interface was also revamped.
This commit is contained in:
2021-07-20 22:35:49 +01:00
parent 6cda1fe183
commit cbcc1f025a
10 changed files with 118 additions and 69 deletions

View File

@@ -34,48 +34,26 @@ class BanController extends Controller
{
$this->authorize('create', [Ban::class, $user]);
// FIXME: Needs refactoring to a simpler format, e.g. parse the user's given date directly.
if (is_null($user->bans)) {
$duration = $request->duration;
$reason = $request->reason;
$duration = strtolower($request->durationOperator);
$durationOperand = $request->durationOperand;
$type = $request->suspensionType; // ON: Temporary | OFF: Permanent
$expiryDate = now();
if (! empty($duration)) {
switch ($duration) {
case 'days':
$expiryDate->addDays($durationOperand);
break;
case 'weeks':
$expiryDate->addWeeks($durationOperand);
break;
case 'months':
$expiryDate->addMonths($durationOperand);
break;
case 'years':
$expiryDate->addYears($durationOperand);
break;
}
} else {
// Essentially permanent
$expiryDate->addYears(40);
if ($type == "on") {
$expiryDate = now()->addDays($duration);
}
$ban = Ban::create([
'userID' => $user->id,
'reason' => $reason,
'bannedUntil' => $expiryDate->format('Y-m-d H:i:s'),
'userAgent' => 'Unknown',
'bannedUntil' => ($type == "on") ? $expiryDate->format('Y-m-d H:i:s') : null,
'authorUserID' => Auth::user()->id,
'isPermanent' => ($type == "off") ? true : false
]);
event(new UserBannedEvent($user, $ban));
$request->session()->flash('success', __('Account suspended. Suspension ID #:susId', ['susId', $ban->id]));
$request->session()->flash('success', __('Account suspended.'));
} else {
$request->session()->flash('error', __('Account already suspended!'));
}

View File

@@ -71,6 +71,17 @@ class ProfileController extends Controller
}
}
$suspensionInfo = null;
if ($user->isBanned())
{
$suspensionInfo = [
'isPermanent' => $user->bans->isPermanent,
'reason' => $user->bans->reason,
'bannedUntil' => $user->bans->bannedUntil
];
}
if (Auth::user()->is($user) || Auth::user()->can('profiles.view.others')) {
return view('dashboard.user.profile.displayprofile')
->with([
@@ -82,6 +93,7 @@ class ProfileController extends Controller
'since' => $createdDate->englishMonth.' '.$createdDate->year,
'ipInfo' => IP::lookup($user->originalIP),
'roles' => $roleList,
'suspensionInfo' => $suspensionInfo
]);
} else {
abort(403, __('You cannot view someone else\'s profile.'));