Skip to content

Commit 2b11f95

Browse files
authored
Merge pull request #622 from kenjis/refactor-UserIdentityModel-update
refactor: UserIdentityModel::update() throws Exception
2 parents 890729d + 83fd399 commit 2b11f95

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/Models/UserIdentityModel.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use CodeIgniter\Shield\Entities\User;
1414
use CodeIgniter\Shield\Entities\UserIdentity;
1515
use CodeIgniter\Shield\Exceptions\LogicException;
16+
use CodeIgniter\Shield\Exceptions\ValidationException;
1617
use Faker\Generator;
1718

1819
class UserIdentityModel extends Model
@@ -362,6 +363,26 @@ public function forceGlobalPasswordReset(): void
362363
$this->checkQueryReturn($return);
363364
}
364365

366+
/**
367+
* Override the Model's `update()` method.
368+
* Throws an Exception when it fails.
369+
*
370+
* @param array|int|string|null $id
371+
* @param array|object|null $data
372+
*
373+
* @return true if the update is successful
374+
*
375+
* @throws ValidationException
376+
*/
377+
public function update($id = null, $data = null): bool
378+
{
379+
$result = parent::update($id, $data);
380+
381+
$this->checkQueryReturn($result);
382+
383+
return true;
384+
}
385+
365386
public function fake(Generator &$faker): UserIdentity
366387
{
367388
return new UserIdentity([

src/Traits/Resettable.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ trait Resettable
1919
*/
2020
public function requiresPasswordReset(): bool
2121
{
22-
$identity_model = model(UserIdentityModel::class);
23-
$identity = $identity_model->getIdentityByType($this, Session::ID_TYPE_EMAIL_PASSWORD);
22+
$identityModel = model(UserIdentityModel::class);
23+
$identity = $identityModel->getIdentityByType($this, Session::ID_TYPE_EMAIL_PASSWORD);
2424

2525
return $identity->force_reset;
2626
}
@@ -35,11 +35,7 @@ public function forcePasswordReset(): void
3535
return;
3636
}
3737

38-
// Set force_reset to true
39-
$identity_model = model(UserIdentityModel::class);
40-
$identity_model->set('force_reset', 1);
41-
$identity_model->where(['user_id' => $this->id, 'type' => Session::ID_TYPE_EMAIL_PASSWORD]);
42-
$identity_model->update();
38+
$this->setForceReset(true);
4339
}
4440

4541
/**
@@ -52,10 +48,19 @@ public function undoForcePasswordReset(): void
5248
return;
5349
}
5450

55-
// Set force_reset to false
56-
$identity_model = model(UserIdentityModel::class);
57-
$identity_model->set('force_reset', 0);
58-
$identity_model->where(['user_id' => $this->id, 'type' => Session::ID_TYPE_EMAIL_PASSWORD]);
59-
$identity_model->update();
51+
$this->setForceReset(false);
52+
}
53+
54+
/**
55+
* Set force_reset
56+
*/
57+
private function setForceReset(bool $value): void
58+
{
59+
$value = (int) $value;
60+
61+
$identityModel = model(UserIdentityModel::class);
62+
$identityModel->set('force_reset', $value);
63+
$identityModel->where(['user_id' => $this->id, 'type' => Session::ID_TYPE_EMAIL_PASSWORD]);
64+
$identityModel->update();
6065
}
6166
}

0 commit comments

Comments
 (0)