Skip to content

feat: (LAR-86) add bannished sysem #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9647e17
feat: (LAR-107) Modification du formulaire de mot de passe oublié
Stephen2304 Nov 11, 2024
cd4c813
fix: (LAR-107) correction des conflits
Stephen2304 Nov 12, 2024
e3dfba4
feat: (LAR-107) Mise a jour
Stephen2304 Nov 11, 2024
583a633
feat: (LAR-107) Suppression des commentaire
Stephen2304 Nov 11, 2024
23882cf
fix: (LAR-107) suppresion du fichier auth-session
Stephen2304 Nov 11, 2024
a8cd309
fix: (LAR-107) correction des espaces
Stephen2304 Nov 12, 2024
374f8fa
feat: (LAR-86) add bannished sysem
StevyMarlino Nov 10, 2024
bf10e0c
feat(test): (LAR-86) add test
StevyMarlino Nov 12, 2024
9687fde
feat: (LAR-107) Authentification avec Breeze (#216)
Stephen2304 Nov 12, 2024
a602c5a
feat(test): (LAR-86) adding test for bannished user
StevyMarlino Nov 12, 2024
00009db
refact: (LAR-86) refactoring of the mail ban and unban
StevyMarlino Nov 12, 2024
c977b1c
fix: [LAR-116] publish scope on single tag articles list
mckenziearts Nov 12, 2024
4bd49e1
feat(translation): (LAR-86) add translation for UserResource
StevyMarlino Nov 12, 2024
af8674d
fix: [LAR-116] publish scope on single tag articles list (#220)
StevyMarlino Nov 12, 2024
6c2fe5b
fix (LAR-107) correction du fichier verify-email (#221)
Stephen2304 Nov 12, 2024
bc721e4
refactor: (LAR-86) refactoring test and translation 🥲
StevyMarlino Nov 13, 2024
13e5282
feat: (LAR-86) add bannished sysem
StevyMarlino Nov 10, 2024
10f30ba
feat(test): (LAR-86) add test
StevyMarlino Nov 12, 2024
a66589b
feat(test): (LAR-86) adding test for bannished user
StevyMarlino Nov 12, 2024
a2579b3
refact: (LAR-86) refactoring of the mail ban and unban
StevyMarlino Nov 12, 2024
23691c1
feat(translation): (LAR-86) add translation for UserResource
StevyMarlino Nov 12, 2024
7eef7be
refactor: (LAR-86) refactoring test and translation 🥲
StevyMarlino Nov 13, 2024
7cdf064
Merge branch 'feature/lar-86-mettre-en-place-un-systeme-pour-bannir-u…
StevyMarlino Nov 13, 2024
9133988
fix: (LAR-86) fixing phpstan error
StevyMarlino Nov 13, 2024
2a9af9a
fix: (LAR-86) fix testing error 500
StevyMarlino Nov 13, 2024
8c31f0f
Revert "fix: (LAR-86) fix testing error 500"
StevyMarlino Nov 13, 2024
3cd63fd
fix: (LAR-86) update test file
StevyMarlino Nov 13, 2024
381385f
fix(test): (LAR-86) fixing test bug
StevyMarlino Nov 13, 2024
8f64dc0
refact: (LAR-86) change key message ban
StevyMarlino Nov 13, 2024
a3633d7
feat: (LAR-86) improvement to Banning function
StevyMarlino Nov 13, 2024
02b039b
feat(test): (LAR-86) updating test and other file
StevyMarlino Nov 14, 2024
0c221dc
fix(pint): (LAR-86) fixing laravel pint format
StevyMarlino Nov 14, 2024
18eefd6
refact: (LAR-86) refactoring test email and other some files
StevyMarlino Nov 14, 2024
d6c731f
refact: (LAR-86) refactoring add ban migration
StevyMarlino Nov 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/Actions/User/BanUserAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Actions\User;

use App\Events\UserBannedEvent;
use App\Exceptions\CannotBanAdminException;
use App\Exceptions\UserAlreadyBannedException;
use App\Models\User;

final class BanUserAction
{
public function execute(User $user, string $reason): void
{
if ($user->isAdmin() || $user->isModerator()) {
throw new CannotBanAdminException('Impossible de bannir un administrateur.');
}

if ($user->isBanned()) {
throw new UserAlreadyBannedException('Impossible de bannir cet utilisateur car il est déjà banni.');
}

$user->update([
'banned_at' => now(),
'banned_reason' => $reason,
]);

event(new UserBannedEvent($user));
}
}
21 changes: 21 additions & 0 deletions app/Actions/User/UnBanUserAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Actions\User;

use App\Events\UserUnbannedEvent;
use App\Models\User;

final class UnBanUserAction
{
public function execute(User $user): void
{
$user->update([
'banned_at' => null,
'banned_reason' => null,
]);

event(new UserUnbannedEvent($user));
}
}
17 changes: 17 additions & 0 deletions app/Events/UserBannedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

final class UserBannedEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public User $user) {}
}
17 changes: 17 additions & 0 deletions app/Events/UserUnbannedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

final class UserUnbannedEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public User $user) {}
}
12 changes: 12 additions & 0 deletions app/Exceptions/CannotBanAdminException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use Exception;

/**
* @property string $message
*/
final class CannotBanAdminException extends Exception {}
12 changes: 12 additions & 0 deletions app/Exceptions/UserAlreadyBannedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use Exception;

/**
* @property string $message
*/
final class UserAlreadyBannedException extends Exception {}
5 changes: 5 additions & 0 deletions app/Filament/Resources/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Gate;

final class ArticleResource extends Resource
{
Expand Down Expand Up @@ -86,6 +87,8 @@ public static function table(Table $table): Table
->requiresConfirmation()
->modalIcon('heroicon-s-check')
->action(function ($record): void {
Gate::authorize('approve', $record);

$record->approved_at = now();
$record->save();

Expand All @@ -101,6 +104,8 @@ public static function table(Table $table): Table
->requiresConfirmation()
->modalIcon('heroicon-s-x-mark')
->action(function ($record): void {
Gate::authorize('decline', $record);

$record->declined_at = now();
$record->save();
}),
Expand Down
56 changes: 51 additions & 5 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace App\Filament\Resources;

use App\Actions\User\BanUserAction;
use App\Actions\User\UnBanUserAction;
use App\Filament\Resources\UserResource\Pages;
use App\Models\User;
use Awcodes\FilamentBadgeableColumn\Components\Badge;
use Awcodes\FilamentBadgeableColumn\Components\BadgeableColumn;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
Expand Down Expand Up @@ -51,21 +55,63 @@ public static function table(Table $table): Table
->icon('untitledui-inbox')
->description(fn ($record): ?string => $record->phone_number),
Tables\Columns\TextColumn::make('email_verified_at')
->label('Validation Email')
->label(__('user.validate_email'))
->placeholder('N/A')
->date(),
Tables\Columns\TextColumn::make(name: 'created_at')
->label('Inscription')
->label(__('use.inscription'))
->date(),
])
->filters([
Tables\Filters\TernaryFilter::make('email_verified_at')
->label('Email Vérifiée')
->label(__('user.email_verified'))
->nullable(),
])
->actions([
Tables\Actions\DeleteAction::make()
->iconButton(),
Tables\Actions\Action::make('ban')
->label(__('actions.ban'))
->icon('untitledui-archive')
->color('warning')
->visible(fn ($record) => $record->banned_at == null)
->modalHeading(__('user.ban.heading'))
->modalDescription(__('user.ban.description'))
->authorize('ban', User::class)
->form([
TextInput::make('banned_reason')
->label(__('user.ban.reason'))
->required(),
])
->action(function (User $record, array $data): void {
app(BanUserAction::class)->execute($record, $data['banned_reason']);

Notification::make()
->success()
->duration(5000)
->title(__('notifications.user.banned_title'))
->body(__('notifications.user.banned_body'))
->send();
})
->requiresConfirmation(),

Tables\Actions\Action::make('unban')
->label(__('actions.unban'))
->icon('heroicon-o-check-circle')
->color('success')
->visible(fn ($record) => $record->banned_at !== null)
->authorize('unban', User::class)
->action(function (User $record): void {
app(UnBanUserAction::class)->execute($record);

Notification::make()
->success()
->title(__('notifications.user.unbanned_title'))
->duration(5000)
->body(__('notifications.user.unbanned_body'))
->send();
})
->requiresConfirmation(),

Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
Expand Down
12 changes: 12 additions & 0 deletions app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Resources\Components\Tab;
use Filament\Resources\Pages\ListRecords;

final class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

public function getTabs(): array
{
return [
'all' => Tab::make(__('global.all')),
'banned' => Tab::make(__('global.banned'))
->modifyQueryUsing(fn ($query) => $query->isBanned()),
'unbanned' => Tab::make(__('global.unbanned'))
->modifyQueryUsing(fn ($query) => $query->isNotBanned()),
];
}
}
27 changes: 27 additions & 0 deletions app/Http/Middleware/CheckIfBanned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

final class CheckIfBanned
{
public function handle(Request $request, Closure $next): Response
{
// @phpstan-ignore-next-line
if (Auth::check() && Auth::user()->isBanned()) {
Auth::logout();

return redirect()->route('login')->withErrors([
'email' => __('user.ban.message'),
]);
}

return $next($request);
}
}
3 changes: 1 addition & 2 deletions app/Http/Requests/UpdatePasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Http\Requests;

use App\Rules\PasswordCheck;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;

Expand All @@ -18,7 +17,7 @@ public function authorize(): bool
public function rules(): array
{
return [
'current_password' => ['sometimes', 'required', new PasswordCheck],
'current_password' => ['sometimes', 'required'],
'password' => ['required', 'confirmed', Password::min(8)->uncompromised()],
];
}
Expand Down
25 changes: 25 additions & 0 deletions app/Jobs/SendBanEmailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\User;
use App\Notifications\UserBannedNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

final class SendBanEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(public User $user) {}

public function handle(): void
{
$this->user->notify(new UserBannedNotification($this->user));
}
}
25 changes: 25 additions & 0 deletions app/Jobs/SendUnbanEmailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\User;
use App\Notifications\UserUnBannedNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

final class SendUnbanEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(public User $user) {}

public function handle(): void
{
$this->user->notify(new UserUnBannedNotification($this->user));
}
}
19 changes: 19 additions & 0 deletions app/Listeners/SendBanNotificationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Listeners;

use App\Events\UserBannedEvent;
use App\Jobs\SendBanEmailJob;

final class SendBanNotificationListener
{
/**
* Handle the event.
*/
public function handle(UserBannedEvent $event): void
{
SendBanEmailJob::dispatch($event->user);
}
}
19 changes: 19 additions & 0 deletions app/Listeners/SendUnbanNotificationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Listeners;

use App\Events\UserUnbannedEvent;
use App\Jobs\SendUnbanEmailJob;

final class SendUnbanNotificationListener
{
/**
* Handle the event.
*/
public function handle(UserUnbannedEvent $event): void
{
SendUnbanEmailJob::dispatch($event->user);
}
}
2 changes: 1 addition & 1 deletion app/Livewire/Components/Forum/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function solutionAction(): Action
->authorize('manage', $this->thread)
->action(function (): void {
if ($this->thread->isSolved()) {
undoPoint(new BestReply($this->thread->solutionReply));
undoPoint(new BestReply($this->thread->solutionReply)); // @phpstan-ignore-line
}

$this->thread->markSolution($this->reply, Auth::user()); // @phpstan-ignore-line
Expand Down
Loading
Loading