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 24 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
2 changes: 1 addition & 1 deletion app/Actions/Article/CreateArticleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public function execute(CreateArticleData $articleData): Article

return $article;
}
}
}
20 changes: 20 additions & 0 deletions app/Actions/User/BanUserAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Actions\User;

use App\Models\User;
use App\Events\UserBannedEvent;

final class BanUserAction
{
public function execute(User $user, string $reason): void
{
if($user->banned_at == null) {
$user->banned_at = now();
$user->banned_reason = $reason;
$user->save();

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

namespace App\Actions\User;

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

final class UnBanUserAction
{
public function execute(User $user) : void
{
$user->banned_at = null;
$user->banned_reason = null;
$user->save();

event(new UserUnbannedEvent($user));
}
}
12 changes: 0 additions & 12 deletions app/Events/UserBannedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,4 @@ final class UserBannedEvent
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public User $user){}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('user-banned'),
];
}
}
12 changes: 0 additions & 12 deletions app/Events/UserUnbannedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,4 @@ final class UserUnbannedEvent
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public User $user){}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('unban-user'),
];
}
}
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
89 changes: 37 additions & 52 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use App\Events\UserBannedEvent;
use Filament\Resources\Resource;
use App\Events\UserUnbannedEvent;
use App\Actions\User\BanUserAction;
use App\Actions\User\UnBanUserAction;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -56,16 +58,16 @@ 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(__('global.ban.label.validate_email'))
->label(__('user.validate_email'))
->placeholder('N/A')
->date(),
Tables\Columns\TextColumn::make(name: 'created_at')
->label(__('global.ban.label.inscription'))
->label(__('use.inscription'))
->date(),
])
->filters([
Tables\Filters\TernaryFilter::make('email_verified_at')
->label(__('global.ban.label.email_verified'))
->label(__('user.email_verified'))
->nullable(),
])
->actions([
Expand All @@ -74,26 +76,44 @@ public static function table(Table $table): Table
->icon('untitledui-archive')
->color('warning')
->visible(fn ($record) => $record->banned_at == null)
->modalHeading(__('global.ban.heading'))
->modalDescription(__('global.ban.description'))
->modalHeading(__('user.ban.heading'))
->modalDescription(__('user.ban.description'))
->form([

TextInput::make('banned_reason')
->label(__('global.ban.reason'))
->label(__('user.ban.reason'))
->required(),
])
->action(function (User $record, array $data) {
if (!self::canBanUser($record)) {
Notification::make()
->warning()
->title(__('notifications.user.cannot.title'))
->body(__('notifications.user.cannot.ban_admin'))
->title(__('notifications.user.cannot_ban_title'))
->body(__('notifications.user.cannot_ban_admin'))
->duration(5000)
->send();

return;
}
self::BanUserAction($record, $data['banned_reason']);

if ($record->banned_at !== null) {
Notification::make()
->warning()
->title(__('notifications.user.cannot_ban_title'))
->body(__('notifications.user.cannot_ban_body'))
->send();

return;
}

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(),

Expand All @@ -103,7 +123,14 @@ public static function table(Table $table): Table
->color('success')
->visible(fn ($record) => $record->banned_at !== null)
->action(function (User $record) {
self::UnbanUserAction($record);
app(UnBanUserAction::class)->execute($record);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ajouter une Policy pour se rassurer de qui peut ban ou unban


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

Expand All @@ -121,48 +148,6 @@ public static function getPages(): array
];
}

public static function BanUserAction(User $record, $reason): void
{
if ($record->banned_at !== null) {
Notification::make()
->warning()
->title(__('notifications.user.cannot.title'))
->body(__('notifications.user.cannot.body'))
->send();

return;
}

$record->banned_at = Carbon::now();
$record->banned_reason = $reason;
$record->save();

Notification::make()
->success()
->duration(5000)
->title(__('notifications.user.banned.title'))
->body(__('notifications.user.banned.body'))
->send();

event(new UserBannedEvent($record));
}

public static function UnbanUserAction(User $record): void
{
$record->banned_at = null;
$record->banned_reason = null;
$record->save();

Notification::make()
->success()
->title(__('notifications.user.unbanned.title'))
->duration(5000)
->body(__('notifications.user.unbanned.body'))
->send();

event(new UserUnbannedEvent($record));
}

public static function canBanUser(User $record): bool
{
return !$record->hasRole('admin');
Expand Down
18 changes: 9 additions & 9 deletions app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ final class ListUsers extends ListRecords
public function getTabs() : array
{
return [
__('global.ban.all') => Tab::make(__('global.ban.all')),
__('global.ban.banned') => Tab::make(__('global.ban.banned'))
->modifyQueryUsing(function ($query) {
return $query->isBanned();
}),
__('global.ban.not_banned') => Tab::make(__('global.ban.not_banned'))
->modifyQueryUsing(function ($query) {
return $query->isNotBanned();
}),
'all' => Tab::make(__('global.all')),
'banned' => Tab::make(__('global.banned'))
->modifyQueryUsing(function ($query) {
return $query->isBanned();
}),
'unbanned' => Tab::make(__('global.unbanned'))
->modifyQueryUsing(function ($query) {
return $query->isNotBanned();
}),
];
}
}
17 changes: 11 additions & 6 deletions app/Http/Middleware/CheckIfBanned.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -13,12 +14,16 @@ final class CheckIfBanned
{
public function handle(Request $request, Closure $next): Response
{
if (Auth::check() && Auth::user()->banned_at) {
Auth::logout();

return redirect()->route('login')->withErrors([
'email' => __('global.ban.message'),
]);
if (Auth::check()) {
$user = Auth::user();

if ($user && $user->banned_at) {
Auth::logout();

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

return $next($request);
Expand Down
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
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
11 changes: 7 additions & 4 deletions app/Livewire/Components/Slideovers/ArticleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ final class ArticleForm extends SlideOverComponent implements HasForms

public function mount(?int $articleId = null): void
{
$this->article = $articleId
/** @var Article $article */
$article = $articleId
? Article::query()->findOrFail($articleId)
: new Article;

$this->form->fill(array_merge($this->article->toArray(), [
'is_draft' => ! $this->article->published_at,
'published_at' => $this->article->published_at,
$this->form->fill(array_merge($article->toArray(), [
'is_draft' => ! $article->published_at,
'published_at' => $article->published_at,
]));

$this->article = $article;
}

public static function panelMaxWidth(): string
Expand Down
Loading
Loading