Skip to content

Commit

Permalink
[11.x] Adding PasswordResetLinkSent event (#51253)
Browse files Browse the repository at this point in the history
* Adding PasswordResetLinkSent event

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Muffinman and taylorotwell authored May 6, 2024
1 parent 451247f commit 5710fdb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Illuminate/Auth/Events/PasswordResetLinkSent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Auth\Events;

use Illuminate\Queue\SerializesModels;

class PasswordResetLinkSent
{
use SerializesModels;

/**
* The user instance.
*
* @var \Illuminate\Contracts\Auth\CanResetPassword
*/
public $user;

/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
}
17 changes: 16 additions & 1 deletion src/Illuminate/Auth/Passwords/PasswordBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Illuminate\Auth\Passwords;

use Closure;
use Illuminate\Auth\Events\PasswordResetLinkSent;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use UnexpectedValueException;

Expand All @@ -25,17 +27,26 @@ class PasswordBroker implements PasswordBrokerContract
*/
protected $users;

/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* Create a new password broker instance.
*
* @param \Illuminate\Auth\Passwords\TokenRepositoryInterface $tokens
* @param \Illuminate\Contracts\Auth\UserProvider $users
* @param \Illuminate\Contracts\Events\Dispatcher $users

This comment has been minimized.

Copy link
@Muffinman

Muffinman May 6, 2024

Author Contributor

Whoops spotted a mistake in the docbloc here. I can PR a fix.

* @return void
*/
public function __construct(TokenRepositoryInterface $tokens, UserProvider $users)
public function __construct(TokenRepositoryInterface $tokens, UserProvider $users, ?Dispatcher $dispatcher = null)
{
$this->users = $users;
$this->tokens = $tokens;
$this->events = $dispatcher;
}

/**
Expand Down Expand Up @@ -71,6 +82,10 @@ public function sendResetLink(array $credentials, ?Closure $callback = null)
// the current URI having nothing set in the session to indicate errors.
$user->sendPasswordResetNotification($token);

if ($this->events) {
$this->events->dispatch(new PasswordResetLinkSent($user));
}

return static::RESET_LINK_SENT;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Auth/Passwords/PasswordBrokerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected function resolve($name)
// aggregate service of sorts providing a convenient interface for resets.
return new PasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'] ?? null)
$this->app['auth']->createUserProvider($config['provider'] ?? null),
$this->app['events'] ?? null,
);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Auth/ForgotPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Tests\Integration\Auth;

use Illuminate\Auth\Events\PasswordResetLinkSent;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -66,6 +68,25 @@ function (ResetPassword $notification, $channels) use ($user) {
);
}

public function testItCanTriggerPasswordResetSentEvent()
{
Event::fake([PasswordResetLinkSent::class]);

UserFactory::new()->create();

$user = AuthenticationTestUser::first();

Password::broker()->sendResetLink([
'email' => $user->email,
]);

Event::assertDispatched(PasswordResetLinkSent::class, function ($event) {
$this->assertEquals(1, $event->user->id);

return true;
});
}

public function testItCanSendForgotPasswordEmailViaCreateUrlUsing()
{
Notification::fake();
Expand Down

0 comments on commit 5710fdb

Please sign in to comment.