From 9af3bb2c776a208dd971724a45a91901d36afc19 Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Mon, 9 Sep 2024 16:09:18 +0300 Subject: [PATCH 1/3] Reset password --- .../Resources/UserResource/Pages/ViewUser.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/Filament/Resources/UserResource/Pages/ViewUser.php b/app/Filament/Resources/UserResource/Pages/ViewUser.php index 64282e0..ca2b471 100644 --- a/app/Filament/Resources/UserResource/Pages/ViewUser.php +++ b/app/Filament/Resources/UserResource/Pages/ViewUser.php @@ -5,9 +5,37 @@ namespace App\Filament\Resources\UserResource\Pages; use App\Filament\Resources\UserResource; +use Filament\Notifications\Notification; +use Filament\Pages\Actions\Action; +use Filament\Pages\Actions\EditAction; use Filament\Resources\Pages\ViewRecord; +use Illuminate\Support\Facades\Password; class ViewUser extends ViewRecord { protected static string $resource = UserResource::class; + + protected function getActions(): array + { + return [ + Action::make('reset_password') + ->label('Reset Password') + ->outlined() + ->action(function () { + $response = Password::broker(config('filament-breezy.reset_broker', config('auth.defaults.passwords')))->sendResetLink(['email' => $this->getRecord()->email]); + if ($response == Password::RESET_LINK_SENT) { + Notification::make()->title(__('filament-breezy::default.reset_password.notification_success'))->success()->send(); + + $this->hasBeenSent = true; + } else { + Notification::make()->title(match ($response) { + 'passwords.throttled' => __('passwords.throttled'), + 'passwords.user' => __('passwords.user') + })->danger()->send(); + } + }), + + EditAction::make(), + ]; + } } From 8c460476511e95fb181e189b360a8d9b8728538e Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Mon, 9 Sep 2024 21:43:38 +0300 Subject: [PATCH 2/3] Reset password action --- .../Actions/ResetPasswordAction.php | 57 +++++++++++++++++++ .../Resources/UserResource/Pages/ViewUser.php | 22 +------ lang/ro/general.php | 4 ++ lang/ro/user.php | 4 ++ 4 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php diff --git a/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php new file mode 100644 index 0000000..ac35c9c --- /dev/null +++ b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php @@ -0,0 +1,57 @@ +label(__('user.actions.reset_password')); + $this->outlined(); + $this->action(function (User $record) { + $key = $this->getRateLimiterKey($record); + $maxAttempts = 1; + + if (RateLimiter::tooManyAttempts($key, $maxAttempts)) { + Notification::make() + ->title(__('general.warnings.reset_password_too_many_attempts')) + ->danger() + ->send(); + + return; + } + + RateLimiter::increment($key, 3600); + + $response = Password::broker(config('filament-breezy.reset_broker', config('auth.defaults.passwords')))->sendResetLink(['email' => $record->email]); + if ($response == Password::RESET_LINK_SENT) { + Notification::make()->title(__('filament-breezy::default.reset_password.notification_success'))->success()->send(); + } else { + Notification::make()->title(match ($response) { + 'passwords.throttled' => __('passwords.throttled'), + 'passwords.user' => __('passwords.user') + })->danger()->send(); + } + }); + } + + private function getRateLimiterKey(User $user): string + { + return 'reset-password:' . $user->id; + } +} diff --git a/app/Filament/Resources/UserResource/Pages/ViewUser.php b/app/Filament/Resources/UserResource/Pages/ViewUser.php index ca2b471..6e18463 100644 --- a/app/Filament/Resources/UserResource/Pages/ViewUser.php +++ b/app/Filament/Resources/UserResource/Pages/ViewUser.php @@ -5,11 +5,9 @@ namespace App\Filament\Resources\UserResource\Pages; use App\Filament\Resources\UserResource; -use Filament\Notifications\Notification; -use Filament\Pages\Actions\Action; +use App\Filament\Resources\UserResource\Actions\ResetPasswordAction; use Filament\Pages\Actions\EditAction; use Filament\Resources\Pages\ViewRecord; -use Illuminate\Support\Facades\Password; class ViewUser extends ViewRecord { @@ -18,22 +16,8 @@ class ViewUser extends ViewRecord protected function getActions(): array { return [ - Action::make('reset_password') - ->label('Reset Password') - ->outlined() - ->action(function () { - $response = Password::broker(config('filament-breezy.reset_broker', config('auth.defaults.passwords')))->sendResetLink(['email' => $this->getRecord()->email]); - if ($response == Password::RESET_LINK_SENT) { - Notification::make()->title(__('filament-breezy::default.reset_password.notification_success'))->success()->send(); - - $this->hasBeenSent = true; - } else { - Notification::make()->title(match ($response) { - 'passwords.throttled' => __('passwords.throttled'), - 'passwords.user' => __('passwords.user') - })->danger()->send(); - } - }), + ResetPasswordAction::make() + ->record($this->getRecord()), EditAction::make(), ]; diff --git a/lang/ro/general.php b/lang/ro/general.php index d3fab65..64e0376 100644 --- a/lang/ro/general.php +++ b/lang/ro/general.php @@ -15,4 +15,8 @@ 'date_from' => 'Dată început', 'date_until' => 'Dată sfârșit', ], + + 'warnings' => [ + 'reset_password_too_many_attempts' => 'Numărul maxim de email-uri trimise pentru resetarea parolei pentru acest utilizator a fost atins! Încercați din nou mai târziu.', + ], ]; diff --git a/lang/ro/user.php b/lang/ro/user.php index 4f5d569..93eff47 100644 --- a/lang/ro/user.php +++ b/lang/ro/user.php @@ -23,4 +23,8 @@ 'platform_coordinator' => 'Coordonator local IGSU/ISUJ', 'org_admin' => 'Admin organizație', ], + + 'actions' => [ + 'reset_password' => 'Resetează parola', + ], ]; From c891ef05e79976fa7fa4a63cbaf5799aac6d5e7d Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe Date: Mon, 23 Sep 2024 13:10:18 +0300 Subject: [PATCH 3/3] small improvments --- .../Actions/ResetPasswordAction.php | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php index ac35c9c..383c10f 100644 --- a/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php +++ b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\RateLimiter; +const HOUR_IN_SECONDS = 3600; class ResetPasswordAction extends Action { public static function getDefaultName(): ?string @@ -36,17 +37,23 @@ protected function setUp(): void return; } - RateLimiter::increment($key, 3600); + RateLimiter::increment($key, HOUR_IN_SECONDS); $response = Password::broker(config('filament-breezy.reset_broker', config('auth.defaults.passwords')))->sendResetLink(['email' => $record->email]); - if ($response == Password::RESET_LINK_SENT) { - Notification::make()->title(__('filament-breezy::default.reset_password.notification_success'))->success()->send(); - } else { - Notification::make()->title(match ($response) { - 'passwords.throttled' => __('passwords.throttled'), - 'passwords.user' => __('passwords.user') - })->danger()->send(); + if ($response === Password::RESET_LINK_SENT) { + Notification::make() + ->title(__('filament-breezy::default.reset_password.notification_success')) + ->success() + ->send(); + + return; } + Notification::make()->title(match ($response) { + 'passwords.throttled' => __('passwords.throttled'), + 'passwords.user' => __('passwords.user') + }) + ->danger() + ->send(); }); }