Skip to content

Commit a3633d7

Browse files
committed
feat: (LAR-86) improvement to Banning function
1 parent 8f64dc0 commit a3633d7

File tree

6 files changed

+45
-37
lines changed

6 files changed

+45
-37
lines changed

app/Actions/User/BanUserAction.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44

55
use App\Models\User;
66
use App\Events\UserBannedEvent;
7+
use App\Exceptions\CannotBanAdminException;
8+
use App\Exceptions\UserAlreadyBannedException;
79

810
final class BanUserAction
911
{
1012
public function execute(User $user, string $reason): void
1113
{
12-
if($user->banned_at == null) {
13-
$user->banned_at = now();
14-
$user->banned_reason = $reason;
15-
$user->save();
16-
17-
event(new UserBannedEvent($user));
14+
if ($user->hasRole('admin')) {
15+
throw new CannotBanAdminException;
16+
}
17+
18+
if($user->banned_at !== null) {
19+
throw new UserAlreadyBannedException;
1820
}
21+
22+
$user->update([
23+
'banned_at' => now(),
24+
'banned_reason' => $reason
25+
]);
26+
27+
event(new UserBannedEvent($user));
1928
}
2029
}

app/Actions/User/UnBanUserAction.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ final class UnBanUserAction
99
{
1010
public function execute(User $user) : void
1111
{
12-
$user->banned_at = null;
13-
$user->banned_reason = null;
14-
$user->save();
12+
$user->update([
13+
'banned_at' => null,
14+
'banned_reason' => null
15+
]);
1516

1617
event(new UserUnbannedEvent($user));
1718
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class CannotBanAdminException extends Exception
8+
{
9+
protected $message = "Impossible de bannir un administrateur.";
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class UserAlreadyBannedException extends Exception
8+
{
9+
protected $message = "Impossible de bannir cet utilisateur car il est déjà banni.";
10+
}

app/Filament/Resources/UserResource.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function table(Table $table): Table
3636
return $table
3737
->modifyQueryUsing(function (Builder $query): void {
3838
$query->whereHas('roles', function (Builder $query): void {
39-
$query->whereNotIn('name', ['admin', 'moderator']);
39+
$query->whereNotIn('name', ['moderator']);
4040
})
4141
->latest();
4242
})
@@ -85,26 +85,6 @@ public static function table(Table $table): Table
8585
->required(),
8686
])
8787
->action(function (User $record, array $data) {
88-
if (!self::canBanUser($record)) {
89-
Notification::make()
90-
->warning()
91-
->title(__('notifications.user.cannot_ban_title'))
92-
->body(__('notifications.user.cannot_ban_admin'))
93-
->duration(5000)
94-
->send();
95-
96-
return;
97-
}
98-
99-
if ($record->banned_at !== null) {
100-
Notification::make()
101-
->warning()
102-
->title(__('notifications.user.cannot_ban_title'))
103-
->body(__('notifications.user.cannot_ban_body'))
104-
->send();
105-
106-
return;
107-
}
10888

10989
app(BanUserAction::class)->execute($record, $data['banned_reason']);
11090

@@ -147,9 +127,4 @@ public static function getPages(): array
147127
'index' => Pages\ListUsers::route('/'),
148128
];
149129
}
150-
151-
public static function canBanUser(User $record): bool
152-
{
153-
return !$record->hasRole('admin');
154-
}
155130
}

tests/Feature/Filament/UserResourceTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use App\Actions\User\UnBanUserAction;
1313
use Illuminate\Support\Facades\Event;
1414
use App\Filament\Resources\UserResource;
15+
use App\Exceptions\UserAlreadyBannedException;
1516

1617
beforeEach(function (): void {
1718
Event::fake();
@@ -63,9 +64,11 @@
6364
// $this->get('/cp')->assertSuccessful();
6465

6566
$user = User::factory()->create(['banned_at' => now()]);
66-
67+
68+
$this->expectException(UserAlreadyBannedException::class);
69+
6770
app(BanUserAction::class)->execute($user, 'Violation des règles');
68-
71+
6972
expect($user->banned_reason)->not->toBe('Violation des règles')
7073
->and($user->banned_at)->not->toBeNull();
7174
});

0 commit comments

Comments
 (0)