-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9af3bb2
commit 8c46047
Showing
4 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
app/Filament/Resources/UserResource/Actions/ResetPasswordAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters