Skip to content

Commit

Permalink
Added feature to send reset password links in admin panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeminiDev1 committed Oct 12, 2024
1 parent ffabc67 commit eff9f21
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `filament-access-control` will be documented in this file.

## v1.8.2 - 2024-10-12

### What's Changed

- Feature "Send Password Link" added to the admin panel.

## v1.8.1 - 2023-06-19

### What's Changed
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Chiiya\FilamentAccessControl\Http\Livewire\AccountExpired;
use Chiiya\FilamentAccessControl\Http\Livewire\TwoFactorChallenge;
use Chiiya\FilamentAccessControl\Http\Controllers\PasswordResetController;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Route;

Expand All @@ -21,3 +22,6 @@
}
}
});

// Password reset routes
Route::get('/password/reset', [PasswordResetController::class])->name('password.reset');
49 changes: 49 additions & 0 deletions src/Http/Controllers/PasswordResetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Chiiya\FilamentAccessControl\Http\Controllers;

use Exception;
use Filament\Facades\Filament;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Filament\Notifications\Auth\ResetPassword as ResetPasswordNotification;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Auth\CanResetPassword;

class PasswordResetController
{
public function sendResetLink(Request $request)
{
// Validate the incoming request data
$request->validate([
'email' => 'required|email',
]);

// Attempt to send the reset link
$response = Password::sendResetLink($request->only('email'), function (CanResetPassword $user, string $token): void {
if (! method_exists($user, 'notify')) {
$userClass = $user::class;

throw new Exception("Model [{$userClass}] does not have a [notify()] method.");
}

$notification = new ResetPasswordNotification($token);
$notification->url = Filament::getResetPasswordUrl($token, $user);

$user->notify($notification);
});

// Check the response status
if ($response === Password::RESET_LINK_SENT) {
Notification::make()
->title(__('Password reset link sent!'))
->success()
->send();
} else {
Notification::make()
->title(__('Failed to send password reset link.'))
->danger()
->send();
}
}
}
12 changes: 11 additions & 1 deletion src/Resources/FilamentUserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Chiiya\FilamentAccessControl\Resources;

use Chiiya\FilamentAccessControl\Http\Controllers\PasswordResetController;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Chiiya\FilamentAccessControl\Contracts\AccessControlUser;
Expand All @@ -19,6 +20,7 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\CreateAction;
Expand All @@ -30,8 +32,10 @@
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Livewire\Component;


class FilamentUserResource extends Resource
{
use HasExtendableSchema;
Expand Down Expand Up @@ -105,7 +109,13 @@ public static function table(Table $table): Table
),
...static::insertAfterTableSchema(),
])
->actions([EditAction::make(), ViewAction::make()])
->actions([EditAction::make(), ViewAction::make(), Action::make('reset_password')
->action(function ($record) {
// Call the method to send reset link
return (new PasswordResetController())->sendResetLink(new Request([
'email' => $record->email, // Pass the user's email
]));
})])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
Expand Down

0 comments on commit eff9f21

Please sign in to comment.