Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/user_management/forcing_password_reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ the following methods to do so.

### Check if a User Requires Password Reset

When you need to check if a user requires password reset, you can do so using the `requiresPasswordReset()` method on the `User` entity. Returns boolean `true`/`false`.
Call `requiresPasswordReset()` on the `User` entity to check whether its
email/password identity requires a password reset. The method returns `false`
for users without an email/password identity. These users can continue through
the `force-reset` filter.

```php
if ($user->requiresPasswordReset()) {
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Resettable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
trait Resettable
{
/**
* Returns true|false based on the value of the
* force reset column of the user's identity.
* Checks whether the user's email/password identity requires a password reset.
* A user without that identity does not require a Shield password reset.
*/
public function requiresPasswordReset(): bool
{
$identityModel = model(UserIdentityModel::class);
$identity = $identityModel->getIdentityByType($this, Session::ID_TYPE_EMAIL_PASSWORD);

return $identity->force_reset;
return $identity->force_reset ?? false;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Authentication/ForcePasswordResetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
$this->assertTrue($user->requiresPasswordReset());
}

public function testUserWithoutEmailPasswordIdentityDoesNotRequirePasswordReset(): void
{
$user = fake(UserModel::class);

$this->assertFalse($user->requiresPasswordReset());
}

public function testForceResetFilterAllowsUserWithoutEmailPasswordIdentity(): void
{
$user = fake(UserModel::class);

$result = $this->actingAs($user)->get('profile');

$result->assertStatus(200);
$result->assertSee('Profile');
}

public function testForcePasswordResetOnUser(): void
{
$user = fake(UserModel::class);
Expand All @@ -87,7 +104,7 @@
$user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']);
$user->forcePasswordReset();

$result = $this->actingAs($user)->get('profile');

Check failure on line 107 in tests/Authentication/ForcePasswordResetTest.php

View workflow job for this annotation

GitHub Actions / psalm / Psalm Analysis

InvalidArgument

tests/Authentication/ForcePasswordResetTest.php:107:35: InvalidArgument: Argument 1 of CodeIgniter\Shield\Tests\Authentication\ForcePasswordResetTest::actingAs expects CodeIgniter\Shield\Entities\User, but array<string, mixed>|object provided (see https://psalm.dev/004)

// This should redirect to the stated url in Auth redirects
$result->assertStatus(302);
Expand Down
Loading