4
4
5
5
namespace App \Filament \Resources ;
6
6
7
+ use Carbon \Carbon ;
7
8
use App \Models \User ;
8
9
use Filament \Tables ;
9
10
use Filament \Tables \Table ;
11
+ use App \Events \UserBannedEvent ;
10
12
use Filament \Resources \Resource ;
11
- use Filament \ Tables \ Actions \ Action ;
13
+ use App \ Events \ UserUnbannedEvent ;
12
14
use Filament \Forms \Components \TextInput ;
13
- use Filament \Tables \ Actions \ ActionGroup ;
15
+ use Filament \Notifications \ Notification ;
14
16
use Illuminate \Database \Eloquent \Builder ;
15
17
use App \Filament \Resources \UserResource \Pages ;
16
18
use Awcodes \FilamentBadgeableColumn \Components \Badge ;
@@ -67,36 +69,45 @@ public static function table(Table $table): Table
67
69
->nullable (),
68
70
])
69
71
->actions ([
70
- ActionGroup::make ([
71
- Action::make ('ban ' )
72
+ Tables \Actions \Action::make ('ban ' )
72
73
->label (__ ('actions.ban ' ))
73
74
->icon ('untitledui-archive ' )
74
75
->color ('warning ' )
75
76
->visible (fn ($ record ) => $ record ->banned_at == null )
76
77
->modalHeading (__ ('Bannir l \'utilisateur ' ))
77
78
->modalDescription (__ ('Veuillez entrer la raison du bannissement. ' ))
78
79
->form ([
79
- TextInput::make ('banned_reason ' )
80
+
81
+ TextInput::make ('banned_reason ' )
80
82
->label (__ ('Raison du bannissement ' ))
81
83
->required (),
82
84
])
83
- ->action (function ($ record , array $ data ) {
84
- $ record ->ban ($ data ['banned_reason ' ]);
85
+ ->action (function (User $ record , array $ data ) {
86
+ if (!self ::canBanUser ($ record )) {
87
+ Notification::make ()
88
+ ->warning ()
89
+ ->title (__ ('Impossible de bannir ' ))
90
+ ->body (__ ('Vous ne pouvez pas bannir un administrateur. ' ))
91
+ ->duration (5000 )
92
+ ->send ();
93
+
94
+ return ;
95
+ }
96
+ self ::BanUserAction ($ record , $ data ['banned_reason ' ]);
85
97
})
86
98
->requiresConfirmation (),
87
99
88
- Action::make ('unban ' )
100
+ Tables \ Actions \ Action::make ('unban ' )
89
101
->label (__ ('actions.unban ' ))
90
102
->icon ('heroicon-o-check-circle ' )
91
103
->color ('success ' )
92
104
->visible (fn ($ record ) => $ record ->banned_at !== null )
93
- ->action (function ($ record ) {
94
- $ record-> unban ( );
105
+ ->action (function (User $ record ) {
106
+ self :: UnbanUserAction ( $ record );
95
107
})
96
108
->requiresConfirmation (),
97
109
98
110
Tables \Actions \DeleteAction::make (),
99
- ])->icon ('heroicon-m-ellipsis-horizontal ' ),
100
111
])
101
112
->bulkActions ([
102
113
Tables \Actions \DeleteBulkAction::make (),
@@ -109,4 +120,41 @@ public static function getPages(): array
109
120
'index ' => Pages \ListUsers::route ('/ ' ),
110
121
];
111
122
}
123
+
124
+ public static function BanUserAction (User $ record , $ reason ): void
125
+ {
126
+ $ record ->banned_at = Carbon::now ();
127
+ $ record ->banned_reason = $ reason ;
128
+ $ record ->save ();
129
+
130
+ Notification::make ()
131
+ ->success ()
132
+ ->duration (5000 )
133
+ ->title (__ ('L \'utilisateur à été banni ' ))
134
+ ->body (__ ('L \'utilisateur à été notifier qu \'il à été banni ' ))
135
+ ->send ();
136
+
137
+ event (new UserBannedEvent ($ record ));
138
+ }
139
+
140
+ public static function UnbanUserAction (User $ record ): void
141
+ {
142
+ $ record ->banned_at = null ;
143
+ $ record ->banned_reason = null ;
144
+ $ record ->save ();
145
+
146
+ Notification::make ()
147
+ ->success ()
148
+ ->title (__ ('L \'utilisateur à été dé-banni ' ))
149
+ ->duration (5000 )
150
+ ->body (__ ('L \'utilisateur à été notifier qu \'il peut de nouveau se connecter ' ))
151
+ ->send ();
152
+
153
+ event (new UserUnbannedEvent ($ record ));
154
+ }
155
+
156
+ public static function canBanUser (User $ record ): bool
157
+ {
158
+ return !$ record ->hasRole ('admin ' );
159
+ }
112
160
}
0 commit comments