diff --git a/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php new file mode 100644 index 0000000..383c10f --- /dev/null +++ b/app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php @@ -0,0 +1,64 @@ +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, 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(); + + return; + } + 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 64282e0..6e18463 100644 --- a/app/Filament/Resources/UserResource/Pages/ViewUser.php +++ b/app/Filament/Resources/UserResource/Pages/ViewUser.php @@ -5,9 +5,21 @@ namespace App\Filament\Resources\UserResource\Pages; use App\Filament\Resources\UserResource; +use App\Filament\Resources\UserResource\Actions\ResetPasswordAction; +use Filament\Pages\Actions\EditAction; use Filament\Resources\Pages\ViewRecord; class ViewUser extends ViewRecord { protected static string $resource = UserResource::class; + + protected function getActions(): array + { + return [ + 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', + ], ];