Skip to content

Commit

Permalink
Added a feature to send reset password links in admin panel. (#62)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
GeminiDev1 authored Oct 22, 2024
1 parent ffabc67 commit 5e47123
Showing 9 changed files with 89 additions and 5 deletions.
3 changes: 3 additions & 0 deletions resources/lang/ar/default.php
Original file line number Diff line number Diff line change
@@ -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' => 'إعادة تعيين كلمة المرور',
],
];
3 changes: 3 additions & 0 deletions resources/lang/de/default.php
Original file line number Diff line number Diff line change
@@ -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',
],
];
3 changes: 3 additions & 0 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
@@ -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',
],
];
3 changes: 3 additions & 0 deletions resources/lang/id/default.php
Original file line number Diff line number Diff line change
@@ -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',
],
];
3 changes: 3 additions & 0 deletions resources/lang/ja/default.php
Original file line number Diff line number Diff line change
@@ -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' => 'パスワードをリセットする',
],
];
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -20,4 +20,4 @@
});
}
}
});
});
29 changes: 28 additions & 1 deletion src/Resources/FilamentUserResource.php
Original file line number Diff line number Diff line change
@@ -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(),
11 changes: 8 additions & 3 deletions src/Resources/FilamentUserResource/Pages/ListFilamentUsers.php
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions src/Services/PasswordResetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Chiiya\FilamentAccessControl\Services;

use Chiiya\FilamentAccessControl\Models\FilamentUser;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Password;
use Filament\Notifications\Auth\ResetPassword as ResetPasswordNotification;
use Filament\Notifications\Notification;
use Exception;

class PasswordResetService
{
public function sendResetLink(FilamentUser $user)
{
try {
$token = Password::broker('filament')->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();
}
}
}

0 comments on commit 5e47123

Please sign in to comment.