Skip to content

Commit

Permalink
[5.x] When a user changes their password, delete any password reset t…
Browse files Browse the repository at this point in the history
…okens (#10694)
  • Loading branch information
duncanmcclean authored Sep 3, 2024
1 parent 6a1ba7b commit 121c5e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Http/Controllers/CP/Users/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password as PasswordFacade;
use Illuminate\Validation\Rules\Password;
use Statamic\Events\UserPasswordChanged;
use Statamic\Exceptions\NotFoundHttpException;
Expand Down Expand Up @@ -36,6 +37,8 @@ public function update(Request $request, $user)
Auth::login($user);
}

PasswordFacade::deleteToken($user);

UserPasswordChanged::dispatch($user);

return response('', 204);
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Controllers/User/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Http\Controllers\User;

use Illuminate\Support\Facades\Password;
use Statamic\Facades\User;
use Statamic\Http\Requests\UserPasswordRequest;

Expand All @@ -11,9 +12,9 @@ public function __invoke(UserPasswordRequest $request)
{
$user = User::current();

$user->password($request->password);
$user->password($request->password)->save();

$user->save();
Password::deleteToken($user);

return $this->successfulResponse();
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Tags/User/PasswordFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Tags\User;

use Illuminate\Support\Facades\Password;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Parse;
use Statamic\Facades\User;
Expand Down Expand Up @@ -312,4 +313,25 @@ public function it_handles_precognitive_requests()

$response->assertStatus(422);
}

#[Test]
public function it_will_delete_any_password_reset_tokens_when_updating_password()
{
$user = tap(User::make()->email('[email protected]')->password('mypassword'))->save();

$token = Password::createToken($user);

$this->assertTrue(Password::tokenExists($user, $token));

$this
->actingAs($user)
->post('/!/auth/password', [
'current_password' => 'mypassword',
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertSessionHasNoErrors();

$this->assertFalse(Password::tokenExists($user, $token));
}
}

0 comments on commit 121c5e2

Please sign in to comment.