Skip to content

Commit

Permalink
Reset password action
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Sep 9, 2024
1 parent 9af3bb2 commit 8c46047
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\UserResource\Actions;

use App\Models\User;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\RateLimiter;

class ResetPasswordAction extends Action
{
public static function getDefaultName(): ?string
{
return 'reset_password';
}

protected function setUp(): void
{
parent::setUp();

$this->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;
}
}
22 changes: 3 additions & 19 deletions app/Filament/Resources/UserResource/Pages/ViewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(),
];
Expand Down
4 changes: 4 additions & 0 deletions lang/ro/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
],
];
4 changes: 4 additions & 0 deletions lang/ro/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
'platform_coordinator' => 'Coordonator local IGSU/ISUJ',
'org_admin' => 'Admin organizație',
],

'actions' => [
'reset_password' => 'Resetează parola',
],
];

0 comments on commit 8c46047

Please sign in to comment.