Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a feature to send reset password links in admin panel. #62

Merged
merged 7 commits into from
Oct 22, 2024
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.

GeminiDev1 marked this conversation as resolved.
Show resolved Hide resolved
## 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');
GeminiDev1 marked this conversation as resolved.
Show resolved Hide resolved
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
chiiya marked this conversation as resolved.
Show resolved Hide resolved
{
public function sendResetLink(Request $request)
{
// Validate the incoming request data
$request->validate([
'email' => 'required|email',
]);
GeminiDev1 marked this conversation as resolved.
Show resolved Hide resolved

// 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);
});
GeminiDev1 marked this conversation as resolved.
Show resolved Hide resolved

// 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
]));
})])
GeminiDev1 marked this conversation as resolved.
Show resolved Hide resolved
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
Expand Down