Merge branch 'pr/606' into 1.x

This commit is contained in:
Dan Harrin
2021-09-26 11:26:25 +01:00
7 changed files with 83 additions and 24 deletions
+21
View File
@@ -96,6 +96,27 @@ public function isFilamentAdmin()
}
```
Finally, for the correct reset password URL to be sent, you should implement a `sendPasswordResetNotification($token)` function. You can use the `Filament\Models\Concerns\SendsFilamentPasswordResetNotification` trait to add this functionality to your User model, fully integrated with Filament:
```php
<?php
namespace App\Models;
use Filament\Models\Concerns\IsFilamentUser;
use Filament\Models\Concerns\SendsFilamentPasswordResetNotification;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements FilamentUser
{
use IsFilamentUser;
use SendsFilamentPasswordResetNotification;
// ...
}
```
### Disabling the Default Migrations
You may wish to prevent the migration for the default users table from being registered. You may do this by calling:
+4 -1
View File
@@ -36,7 +36,10 @@ class RequestPassword extends Component
return;
}
$requestStatus = Password::broker('filament_users')->sendResetLink($this->validate());
// Use the `filament_users` broker only with the `filament` guard.
$broker = config('filament.auth.guard') === 'filament' ? 'filament_users' : null;
$requestStatus = Password::broker($broker)->sendResetLink($this->validate());
if (Password::RESET_LINK_SENT !== $requestStatus) {
$this->addError('email', __("filament::auth/request-password.messages.{$requestStatus}"));
+5 -2
View File
@@ -43,7 +43,10 @@ class ResetPassword extends Component
{
$this->validate();
$resetStatus = Password::broker('filament_users')
// Use the `filament_users` broker only with the `filament` guard.
$broker = config('filament.auth.guard') === 'filament' ? 'filament_users' : null;
$resetStatus = Password::broker($broker)
->reset(
$this->only(['email', 'password', 'token']),
function ($user, $password) {
@@ -55,7 +58,7 @@ class ResetPassword extends Component
);
if (Password::PASSWORD_RESET !== $resetStatus) {
$this->addError('email', __("filament::auth/request-password.messages.{$resetStatus}"));
$this->addError('email', __("filament::auth/reset-password.messages.{$resetStatus}"));
return;
}
@@ -0,0 +1,26 @@
<?php
namespace Filament\Models\Concerns;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Support\Facades\URL;
trait SendsFilamentPasswordResetNotification
{
public function sendPasswordResetNotification($token)
{
$notification = new ResetPasswordNotification($token);
$notification->createUrlUsing(function ($notifiable, $token) {
return URL::signedRoute(
'filament.auth.password.reset',
[
'email' => $notifiable->getEmailForPasswordReset(),
'token' => $token,
],
now()->addMinutes(config('auth.passwords.filament_users.expire')),
);
});
$this->notify($notification);
}
}
+2 -19
View File
@@ -4,18 +4,18 @@ namespace Filament\Models;
use Filament\Database\Factories\UserFactory;
use Filament\Models\Concerns\IsFilamentUser;
use Filament\Models\Concerns\SendsFilamentPasswordResetNotification;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\URL;
class User extends Authenticatable implements FilamentUser
{
use HasFactory;
use IsFilamentUser;
use Notifiable;
use SendsFilamentPasswordResetNotification;
public static $filamentAdminColumn = 'is_admin';
@@ -37,23 +37,6 @@ class User extends Authenticatable implements FilamentUser
protected $table = 'filament_users';
public function sendPasswordResetNotification($token)
{
$notification = new ResetPasswordNotification($token);
$notification->createUrlUsing(function ($notifiable, $token) {
return URL::signedRoute(
'filament.auth.password.reset',
[
'email' => $notifiable->getEmailForPasswordReset(),
'token' => $token,
],
now()->addMinutes(config('auth.passwords.filament_users.expire')),
);
});
$this->notify($notification);
}
protected static function newFactory()
{
return UserFactory::new();
@@ -6,6 +6,7 @@ use Filament\Http\Livewire\Auth\RequestPassword;
use Filament\Models\User;
use Filament\Tests\TestCase;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
@@ -27,6 +28,15 @@ class RequestPasswordTest extends TestCase
Notification::assertSentTo($user, ResetPasswordNotification::class);
}
/** @test */
public function can_request_password_reset_with_custom_user_model()
{
Config::set('filament.auth.guard', 'web');
Config::set('auth.providers.users.model', User::class);
$this->can_request_password_reset();
}
/** @test */
public function can_view_password_reset_request_page()
{
+15 -2
View File
@@ -6,6 +6,7 @@ use Filament\Filament;
use Filament\Http\Livewire\Auth\ResetPassword;
use Filament\Models\User;
use Filament\Tests\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
@@ -29,7 +30,7 @@ class ResetPasswordTest extends TestCase
->assertHasNoErrors()
->assertRedirect(route('filament.dashboard'));
$this->assertAuthenticatedAs($user);
$this->assertAuthenticatedAs($user, config('filament.auth.guard'));
$this->assertTrue(Filament::auth()->attempt([
'email' => $user->email,
@@ -37,6 +38,15 @@ class ResetPasswordTest extends TestCase
]));
}
/** @test */
public function can_reset_password_with_custom_user_model()
{
Config::set('filament.auth.guard', 'web');
Config::set('auth.providers.users.model', User::class);
$this->can_reset_password();
}
/** @test */
public function can_view_password_reset_page()
{
@@ -137,6 +147,9 @@ class ResetPasswordTest extends TestCase
$user = User::factory()->create();
}
return Password::broker('filament_users')->createToken($user);
// Use filament_users broker only when we're using filament guard. Otherwise, use Laravel's default
$broker = config('filament.auth.guard') === 'filament' ? 'filament_users' : null;
return Password::broker($broker)->createToken($user);
}
}