From 5e4712357443f4be69efc593f4e78d52ddb4dbd1 Mon Sep 17 00:00:00 2001 From: Dev <48183390+GeminiDev1@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:33:17 +0100 Subject: [PATCH] Added a feature to send reset password links in admin panel. (#62) * Added feature to send reset password links in admin panel. * Removed changes from changelog, as that is automatically updated. * Removed reset password route as it was unnecessary. Renamed PasswordResetController -> PasswordResetService. Simplified sendResetLink. * Wrapped "Reset password" action into an action group. * Removed "use Illuminate\Http\Request;" as it was not needed. * Added password reset link translations. Enabled account expiry extension in actions group. Show singular message when a single user is extended. * German translation correction. --- resources/lang/ar/default.php | 3 ++ resources/lang/de/default.php | 3 ++ resources/lang/en/default.php | 3 ++ resources/lang/id/default.php | 3 ++ resources/lang/ja/default.php | 3 ++ routes/web.php | 2 +- src/Resources/FilamentUserResource.php | 29 ++++++++++++++- .../Pages/ListFilamentUsers.php | 11 ++++-- src/Services/PasswordResetService.php | 37 +++++++++++++++++++ 9 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/Services/PasswordResetService.php diff --git a/resources/lang/ar/default.php b/resources/lang/ar/default.php index dfaded4..7729713 100644 --- a/resources/lang/ar/default.php +++ b/resources/lang/ar/default.php @@ -36,10 +36,12 @@ 'permissions_view' => 'أذونات مباشرة وكذلك أذونات من خلال دورهم.', 'account_expired' => 'هذا الحساب منتهي الصلاحية. الرجاء الاتصال بالمسؤول.', 'accounts_extended' => 'تم تمديد الحسابات المختارة.', + 'account_extended' => 'تم تمديد الحساب المحدد.', 'invalid_user' => 'مستخدم غير صالح ، يرجى المحاولة مرة أخرى.', 'code_expired' => 'انتهت صلاحية رمز التحقق هذا. الرجاء استخدام الرمز الجديد الذي أرسلناه لك للتو.', 'invalid_code' => 'رمز التحقق غير صالح.', 'enter_code' => 'لتأكيد تسجيل الدخول الخاص بك ، يرجى إدخال رمز التحقق المرسل إلى عنوان بريدك الإلكتروني.', + 'password_reset_link_sent' => 'تم إرسال رابط إعادة تعيين كلمة المرور!', ], 'pages' => [ 'reset_password' => 'استعادة كلمة المرور', @@ -75,5 +77,6 @@ ], 'actions' => [ 'extend' => 'تمديد تاريخ انتهاء الصلاحية', + 'reset_password' => 'إعادة تعيين كلمة المرور', ], ]; diff --git a/resources/lang/de/default.php b/resources/lang/de/default.php index c45aa5a..151a396 100644 --- a/resources/lang/de/default.php +++ b/resources/lang/de/default.php @@ -36,10 +36,12 @@ 'permissions_view' => 'Direkte Berechtigungen sowie Berechtigungen über die Rolle.', 'account_expired' => 'Dieser Account ist abgelaufen. Bitte kontaktieren Sie einen Administrator.', 'accounts_extended' => 'Die ausgewählten Accounts wurden erfolgreich verlängert.', + 'account_extended' => 'Der ausgewählte Account wurde verlängert.', 'invalid_user' => 'Ungültiger Benutzer, bitte versuchen Sie es erneut.', 'code_expired' => 'Dieser Verifizierungscode ist abgelaufen. Bitte verwenden Sie den neuen Code, den wir Ihnen gerade geschickt haben.', 'invalid_code' => 'Ungültiger Verifizierungscode.', 'enter_code' => 'Bitte geben Sie zur Bestätigung Ihres Logins den Verifizierungscode ein, der an Ihre E-Mail Adresse geschickt wurde.', + 'password_reset_link_sent' => 'Link zum Zurücksetzen des Passworts gesendet!', ], 'pages' => [ 'reset_password' => 'Passwort zurücksetzen', @@ -75,5 +77,6 @@ ], 'actions' => [ 'extend' => 'Account verlängern', + 'reset_password' => 'Passwort zurücksetzen', ], ]; diff --git a/resources/lang/en/default.php b/resources/lang/en/default.php index 38852fb..d4fbda8 100644 --- a/resources/lang/en/default.php +++ b/resources/lang/en/default.php @@ -36,10 +36,12 @@ 'permissions_view' => 'Direct permissions as well as permissions through their role.', 'account_expired' => 'This account is expired. Please contact an administrator.', 'accounts_extended' => 'The selected accounts have been extended.', + 'account_extended' => 'The selected account has been extended.', 'invalid_user' => 'Invalid user, please try again.', 'code_expired' => 'This verification code has expired. Please use the new code that we have just sent you.', 'invalid_code' => 'Invalid verification code.', 'enter_code' => 'To confirm your login, please enter the verification code sent to your email address.', + 'password_reset_link_sent' => 'Password reset link sent!', ], 'pages' => [ 'reset_password' => 'Reset password', @@ -75,5 +77,6 @@ ], 'actions' => [ 'extend' => 'Extend expiry date', + 'reset_password' => 'Reset password', ], ]; diff --git a/resources/lang/id/default.php b/resources/lang/id/default.php index dc0de96..e50dca1 100644 --- a/resources/lang/id/default.php +++ b/resources/lang/id/default.php @@ -36,10 +36,12 @@ 'permissions_view' => 'Izin langsung serta izin melalui peran mereka.', 'account_expired' => 'Akun ini telah kedaluwarsa. Harap hubungi seorang administrator.', 'accounts_extended' => 'Akun yang dipilih telah diperpanjang.', + 'account_extended' => 'Akun yang dipilih telah diperpanjang.', 'invalid_user' => 'Pengguna tidak valid, harap coba lagi.', 'code_expired' => 'Kode verifikasi ini telah kedaluwarsa. Harap gunakan kode baru yang baru saja kami kirimkan kepada Anda.', 'invalid_code' => 'Kode verifikasi tidak valid.', 'enter_code' => 'Untuk mengonfirmasi login Anda, harap masukkan kode verifikasi yang dikirimkan ke alamat email Anda.', + 'password_reset_link_sent' => 'Tautan setel ulang kata sandi terkirim!', ], 'pages' => [ 'reset_password' => 'Atur Ulang Kata Sandi', @@ -75,5 +77,6 @@ ], 'actions' => [ 'extend' => 'Perpanjang Tanggal Kadaluarsa', + 'reset_password' => 'Setel ulang kata sandi', ], ]; diff --git a/resources/lang/ja/default.php b/resources/lang/ja/default.php index 0e934c1..939b845 100644 --- a/resources/lang/ja/default.php +++ b/resources/lang/ja/default.php @@ -36,10 +36,12 @@ 'permissions_view' => '直接のパーミッションだけでなく、ロールによるパーミッションも表示されます。', 'account_expired' => 'このアカウントは有効期限が切れています。管理者に連絡してください。', 'accounts_extended' => '選択されたアカウントは延長されています。', + 'account_extended' => '選択したアカウントは延長されました。', 'invalid_user' => '無効なユーザーです、もう一度お試しください。', 'code_expired' => 'この認証コードは有効期限が切れています。先ほどお送りした新しいコードをお使いください。', 'invalid_code' => '認証コードが無効です。', 'enter_code' => 'ログインを確認するには、あなたのメールアドレスに送信された認証コードを入力してください。', + 'password_reset_link_sent' => 'パスワードリセットリンクを送信しました!', ], 'pages' => [ 'reset_password' => 'パスワードのリセット', @@ -75,5 +77,6 @@ ], 'actions' => [ 'extend' => '有効期限を延長する', + 'reset_password' => 'パスワードをリセットする', ], ]; diff --git a/routes/web.php b/routes/web.php index 3fbd73f..ef1b390 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,4 +20,4 @@ }); } } -}); +}); \ No newline at end of file diff --git a/src/Resources/FilamentUserResource.php b/src/Resources/FilamentUserResource.php index 4f182dd..ce61841 100644 --- a/src/Resources/FilamentUserResource.php +++ b/src/Resources/FilamentUserResource.php @@ -2,6 +2,7 @@ namespace Chiiya\FilamentAccessControl\Resources; +use Chiiya\FilamentAccessControl\Services\PasswordResetService; use Carbon\Carbon; use Carbon\CarbonImmutable; use Chiiya\FilamentAccessControl\Contracts\AccessControlUser; @@ -19,6 +20,8 @@ use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Filament\Resources\Resource; +use Filament\Tables\Actions\Action; +use Filament\Tables\Actions\ActionGroup; use Filament\Tables\Actions\BulkAction; use Filament\Tables\Actions\BulkActionGroup; use Filament\Tables\Actions\CreateAction; @@ -32,6 +35,7 @@ use Illuminate\Database\Eloquent\Builder; use Livewire\Component; + class FilamentUserResource extends Resource { use HasExtendableSchema; @@ -105,7 +109,30 @@ public static function table(Table $table): Table ), ...static::insertAfterTableSchema(), ]) - ->actions([EditAction::make(), ViewAction::make()]) + ->actions([ + EditAction::make(), + ViewAction::make(), + ActionGroup::make(array_merge( + [ + Action::make('reset_password') + ->icon('heroicon-o-key') + ->label(__('filament-access-control::default.actions.reset_password')) + ->requiresConfirmation() + ->action(function ($record) { + return (new PasswordResetService())->sendResetLink($record); + }), + ], + Feature::enabled(Feature::ACCOUNT_EXPIRY) + ? [ + Action::make('extend') + ->label(__('filament-access-control::default.actions.extend')) + ->action('extendUsers') + ->requiresConfirmation() + ->icon('heroicon-o-clock'), + ] + : [] + )) + ]) ->bulkActions([ BulkActionGroup::make([ DeleteBulkAction::make(), diff --git a/src/Resources/FilamentUserResource/Pages/ListFilamentUsers.php b/src/Resources/FilamentUserResource/Pages/ListFilamentUsers.php index 530a7de..7494a5f 100644 --- a/src/Resources/FilamentUserResource/Pages/ListFilamentUsers.php +++ b/src/Resources/FilamentUserResource/Pages/ListFilamentUsers.php @@ -19,9 +19,14 @@ public function extendUsers(Collection $users): void { $users->each->extend(); - Notification::make()->title( - __('filament-access-control::default.messages.accounts_extended'), - )->success()->send(); + $message = $users->count() === 1 + ? __('filament-access-control::default.messages.account_extended') + : __('filament-access-control::default.messages.accounts_extended'); + + Notification::make() + ->title($message) + ->success() + ->send(); } protected function getHeaderActions(): array diff --git a/src/Services/PasswordResetService.php b/src/Services/PasswordResetService.php new file mode 100644 index 0000000..b8a925e --- /dev/null +++ b/src/Services/PasswordResetService.php @@ -0,0 +1,37 @@ +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(); + } + } +}