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:
@@ -30,13 +30,13 @@ class Ban extends Model
|
||||
'userID',
|
||||
'reason',
|
||||
'bannedUntil',
|
||||
'userAgent',
|
||||
'isPermanent',
|
||||
'authorUserID',
|
||||
|
||||
];
|
||||
|
||||
public $dates = [
|
||||
'bannedUntil',
|
||||
'suspendedUntil',
|
||||
];
|
||||
|
||||
public function user()
|
||||
|
@@ -21,7 +21,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\CleanBans;
|
||||
use App\Jobs\ProcessDueSuspensions;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@@ -50,7 +50,7 @@ class Kernel extends ConsoleKernel
|
||||
->daily();
|
||||
// Production value: Every day
|
||||
|
||||
$schedule->job(new CleanBans)
|
||||
$schedule->job(new ProcessDueSuspensions)
|
||||
->daily();
|
||||
// Production value: Every day
|
||||
}
|
||||
|
@@ -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!'));
|
||||
}
|
||||
|
@@ -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.'));
|
||||
|
@@ -45,8 +45,15 @@ class BanUserRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'reason' => 'required|string',
|
||||
'durationOperand' => 'nullable|string',
|
||||
'durationOperator' => 'nullable|string',
|
||||
'suspensionType' => 'required|string',
|
||||
'duration' => 'required_if:suspensionType,on|nullable|integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'duration.required_if' => __('You must provide a duration if the suspension is temporary.')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -30,7 +30,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CleanBans implements ShouldQueue
|
||||
class ProcessDueSuspensions implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
@@ -52,15 +52,15 @@ class CleanBans implements ShouldQueue
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
Log::debug('Running automatic ban cleaner...');
|
||||
Log::debug('Running automatic suspension cleaner...');
|
||||
$bans = Ban::all();
|
||||
|
||||
if (! is_null($bans)) {
|
||||
foreach ($this->bans as $ban) {
|
||||
$bannedUntil = Carbon::parse($ban->bannedUntil);
|
||||
|
||||
if ($bannedUntil->equalTo(now())) {
|
||||
Log::debug('Deleted ban '.$ban->id.' belonging to '.$ban->user->name);
|
||||
if ($bannedUntil->isToday()) {
|
||||
Log::debug('Lifted expired suspension ID '.$ban->id.' for '.$ban->user->name);
|
||||
$ban->delete();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user