Skip to content

Commit 9487136

Browse files
committed
feat: UserModel::save() can save Email Identity again
1 parent f17355e commit 9487136

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

docs/concepts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ While this has the potential to make the system more complex, the `email` and `p
4646
looked up for you when attempting to access them from the User entity. Caution should be used to craft queries that will pull
4747
in the `email` field when you need to display it to the user, as you could easily run into some n+1 slow queries otherwise.
4848

49-
When you `saveWithEmailIdentity($user)` a `User` instance in the `UserModel`, the email/password identity will automatically be updated.
50-
If no email/password identity exists, you must pass both the email and the password to the User instance prior to calling `saveWithEmailIdentity()`.
49+
When you `save($user)` a `User` instance in the `UserModel`, the email/password identity will automatically be updated.
50+
If no email/password identity exists, you must pass both the email and the password to the User instance prior to calling `save()`.
5151

5252
## Password Validators
5353

docs/quickstart.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ $user = new User([
274274
'email' => 'foo.bar@example.com',
275275
'password' => 'secret plain text password',
276276
]);
277-
$users->saveWithEmailIdentity($user);
277+
$users->save($user);
278278

279279
// To get the complete user object with ID, we need to get from the database
280280
$user = $users->findById($users->getInsertID());
@@ -296,7 +296,7 @@ NOTE: The User rows use [soft deletes](https://codeigniter.com/user_guide/models
296296

297297
### Editing A User
298298

299-
The `UserModel::saveWithEmailIdentity()` method ensures that an email or password previously set on the `User` entity will be automatically updated in the correct `UserIdentity` record.
299+
The `UserModel::save()` method has been modified to ensure that an email or password previously set on the `User` entity will be automatically updated in the correct `UserIdentity` record.
300300

301301
```php
302302
$users = model('UserModel');
@@ -307,7 +307,7 @@ $user->fill([
307307
'email' => 'joe.smith@example.com',
308308
'password' => 'secret123'
309309
]);
310-
$users->saveWithEmailIdentity($user);
310+
$users->save($user);
311311
```
312312

313313
If you prefer to use the `update()` method then you will have to update the user's appropriate UserIdentity manually.

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,6 @@ public function recordActiveDate(): void
232232

233233
$this->user->last_active = Time::now();
234234

235-
$this->provider->saveWithEmailIdentity($this->user);
235+
$this->provider->save($this->user);
236236
}
237237
}

src/Authentication/Authenticators/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function check(array $credentials): Result
310310
// logged in.
311311
if ($passwords->needsRehash($user->password_hash)) {
312312
$user->password_hash = $passwords->hash($givenPassword);
313-
$this->provider->saveWithEmailIdentity($user);
313+
$this->provider->save($user);
314314
}
315315

316316
return new Result([
@@ -803,7 +803,7 @@ public function recordActiveDate(): void
803803

804804
$this->user->last_active = Time::now();
805805

806-
$this->provider->saveWithEmailIdentity($this->user);
806+
$this->provider->save($this->user);
807807
}
808808

809809
/**

src/Controllers/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function registerAction(): RedirectResponse
7272
}
7373

7474
try {
75-
$users->saveWithEmailIdentity($user);
75+
$users->save($user);
7676
} catch (ValidationException $e) {
7777
return redirect()->back()->withInput()->with('errors', $users->errors());
7878
}

src/Models/UserModel.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CodeIgniter\Shield\Authentication\Authenticators\Session;
88
use CodeIgniter\Shield\Entities\User;
99
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
10+
use CodeIgniter\Shield\Exceptions\ValidationException;
1011
use Faker\Generator;
1112

1213
class UserModel extends Model
@@ -187,9 +188,29 @@ public function activate(User $user): void
187188
$this->saveWithEmailIdentity($user);
188189
}
189190

191+
/**
192+
* Override the BaseModel's `save()` method to allow
193+
* updating of user email, password, or password_hash fields
194+
* if they've been modified.
195+
*
196+
* @param User $data
197+
*
198+
* @throws ValidationException
199+
*/
200+
public function save($data): bool
201+
{
202+
assert($data instanceof User);
203+
204+
$this->saveWithEmailIdentity($data);
205+
206+
return true;
207+
}
208+
190209
/**
191210
* Save User and its Email Identity (email, password, or password_hash fields)
192211
* if they've been modified.
212+
*
213+
* @throws ValidationException
193214
*/
194215
public function saveWithEmailIdentity(User $data): void
195216
{

tests/Unit/UserModelTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testSaveInsertUser(): void
2828

2929
$user = $this->createNewUser();
3030

31-
$users->saveWithEmailIdentity($user);
31+
$users->save($user);
3232

3333
$user = $users->findByCredentials(['email' => 'foo@bar.com']);
3434
$this->seeInDatabase('auth_identities', [
@@ -56,15 +56,15 @@ public function testSaveUpdateUserWithUserDataToUpdate(): void
5656
{
5757
$users = $this->createUserModel();
5858
$user = $this->createNewUser();
59-
$users->saveWithEmailIdentity($user);
59+
$users->save($user);
6060

6161
$user = $users->findByCredentials(['email' => 'foo@bar.com']);
6262

6363
$user->username = 'bar';
6464
$user->email = 'bar@bar.com';
6565
$user->active = 1;
6666

67-
$users->saveWithEmailIdentity($user);
67+
$users->save($user);
6868

6969
$this->seeInDatabase('auth_identities', [
7070
'user_id' => $user->id,
@@ -80,7 +80,7 @@ public function testSaveUpdateUserWithNoUserDataToUpdate(): void
8080
{
8181
$users = $this->createUserModel();
8282
$user = $this->createNewUser();
83-
$users->saveWithEmailIdentity($user);
83+
$users->save($user);
8484

8585
$user = $users->findByCredentials(['email' => 'foo@bar.com']);
8686

0 commit comments

Comments
 (0)