diff --git a/src/Resources/FilamentUserResource.php b/src/Resources/FilamentUserResource.php index ce61841..5769851 100644 --- a/src/Resources/FilamentUserResource.php +++ b/src/Resources/FilamentUserResource.php @@ -2,7 +2,6 @@ namespace Chiiya\FilamentAccessControl\Resources; -use Chiiya\FilamentAccessControl\Services\PasswordResetService; use Carbon\Carbon; use Carbon\CarbonImmutable; use Chiiya\FilamentAccessControl\Contracts\AccessControlUser; @@ -13,6 +12,7 @@ use Chiiya\FilamentAccessControl\Resources\FilamentUserResource\Pages\EditFilamentUser; use Chiiya\FilamentAccessControl\Resources\FilamentUserResource\Pages\ListFilamentUsers; use Chiiya\FilamentAccessControl\Resources\FilamentUserResource\Pages\ViewFilamentUser; +use Chiiya\FilamentAccessControl\Services\AuthService; use Chiiya\FilamentAccessControl\Traits\HasExtendableSchema; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Grid; @@ -35,7 +35,6 @@ use Illuminate\Database\Eloquent\Builder; use Livewire\Component; - class FilamentUserResource extends Resource { use HasExtendableSchema; @@ -118,9 +117,9 @@ public static function table(Table $table): Table ->icon('heroicon-o-key') ->label(__('filament-access-control::default.actions.reset_password')) ->requiresConfirmation() - ->action(function ($record) { - return (new PasswordResetService())->sendResetLink($record); - }), + ->action( + fn (AccessControlUser $record) => resolve(AuthService::class)->sendResetLink($record), + ), ], Feature::enabled(Feature::ACCOUNT_EXPIRY) ? [ @@ -130,8 +129,8 @@ public static function table(Table $table): Table ->requiresConfirmation() ->icon('heroicon-o-clock'), ] - : [] - )) + : [], + )), ]) ->bulkActions([ BulkActionGroup::make([ diff --git a/src/Services/AuthService.php b/src/Services/AuthService.php index 93095e7..e801bde 100644 --- a/src/Services/AuthService.php +++ b/src/Services/AuthService.php @@ -6,9 +6,13 @@ use Chiiya\FilamentAccessControl\Exceptions\InvalidCodeException; use Chiiya\FilamentAccessControl\Exceptions\InvalidUserModelException; use Chiiya\FilamentAccessControl\Exceptions\UserNotFoundException; +use Exception; use Filament\Facades\Filament; +use Filament\Notifications\Auth\ResetPassword as ResetPasswordNotification; +use Filament\Notifications\Notification; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Password; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -108,6 +112,29 @@ public function performTwoFactorChallenge(string $code): void $this->login($user, session()->get('filament.remember', false)); } + /** + * Manually send a password reset link to the user. + */ + public function sendResetLink(AccessControlUser $user): void + { + try { + $token = Password::broker('filament')->createToken($user); + $notification = new ResetPasswordNotification($token); + $notification->url = Filament::getResetPasswordUrl($token, $user); + $user->notify($notification); + + Notification::make() + ->title(__('filament-access-control::default.messages.password_reset_link_sent')) + ->success() + ->send(); + } catch (Exception $e) { + Notification::make() + ->title($e->getMessage()) + ->danger() + ->send(); + } + } + private function getUserModel(): Model { /** @var class-string $model */ diff --git a/src/Services/PasswordResetService.php b/src/Services/PasswordResetService.php deleted file mode 100644 index b8a925e..0000000 --- a/src/Services/PasswordResetService.php +++ /dev/null @@ -1,37 +0,0 @@ -createToken($user); - - // Create the notification and set the URL - $notification = new ResetPasswordNotification($token); - $notification->url = Filament::getResetPasswordUrl($token, $user); - - // Send the notification - $user->notify($notification); - - Notification::make() - ->title(__('filament-access-control::default.messages.password_reset_link_sent')) - ->success() - ->send(); - } catch (Exception $e) { - Notification::make() - ->title($e->getMessage()) - ->danger() - ->send(); - } - } -}