Skip to content

Commit 39aaf14

Browse files
committed
refactor: change UserModel::save() to saveWithEmailIdentity()
1 parent 7308597 commit 39aaf14

File tree

9 files changed

+25
-39
lines changed

9 files changed

+25
-39
lines changed

docs/concepts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ While this has the potential to make the system more complex, the `email` and `p
4545
looked up for you when attempting to access them from the User entity. Caution should be used to craft queries that will pull
4646
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.
4747

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

5151
## Password Validators
5252

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' => '[email protected]',
275275
'password' => 'secret plain text password',
276276
]);
277-
$users->save($user);
277+
$users->saveWithEmailIdentity($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::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.
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.
300300

301301
```php
302302
$users = model('UserModel');
@@ -307,7 +307,7 @@ $user->fill([
307307
'email' => '[email protected]',
308308
'password' => 'secret123'
309309
]);
310-
$users->save($user);
310+
$users->saveWithEmailIdentity($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->save($this->user);
235+
$this->provider->saveWithEmailIdentity($this->user);
236236
}
237237
}

src/Authentication/Authenticators/Session.php

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

317317
return new Result([
@@ -804,7 +804,7 @@ public function recordActiveDate(): void
804804

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

807-
$this->provider->save($this->user);
807+
$this->provider->saveWithEmailIdentity($this->user);
808808
}
809809

810810
/**

src/Controllers/RegisterController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public function registerAction(): RedirectResponse
7070
$user->username = null;
7171
}
7272

73-
if (! $users->save($user)) {
74-
return redirect()->back()->withInput()->with('errors', $users->errors());
75-
}
73+
$users->saveWithEmailIdentity($user);
7674

7775
// To get the complete user object with ID, we need to get from the database
7876
$user = $users->findById($users->getInsertID());

src/Models/UserModel.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,14 @@ public function activate(User $user): void
184184
{
185185
$user->active = true;
186186

187-
$return = $this->save($user);
188-
189-
$this->checkQueryReturn($return);
187+
$this->saveWithEmailIdentity($user);
190188
}
191189

192190
/**
193-
* Override the BaseModel's `save()` method to allow
194-
* updating of user email, password, or password_hash
195-
* fields if they've been modified.
196-
*
197-
* @param array|User $data
198-
*
199-
* @TODO can't change the return type to void.
191+
* Save User and its Email Identity (email, password, or password_hash fields)
192+
* if they've been modified.
200193
*/
201-
public function save($data): bool
194+
public function saveWithEmailIdentity(User $data): void
202195
{
203196
try {
204197
/** @throws DataException */
@@ -210,13 +203,10 @@ public function save($data): bool
210203
];
211204
if (in_array($e->getMessage(), $messages, true)) {
212205
// Save updated email identity
213-
if ($data instanceof User) {
214-
$user = $data;
215-
216-
$user->saveEmailIdentity();
217-
}
206+
$user = $data;
207+
$user->saveEmailIdentity();
218208

219-
return true;
209+
return;
220210
}
221211

222212
throw $e;
@@ -238,7 +228,5 @@ public function save($data): bool
238228

239229
$user->saveEmailIdentity();
240230
}
241-
242-
return true;
243231
}
244232
}

tests/Controllers/ActionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function testEmailActivateVerify(): void
247247

248248
$this->user->active = false;
249249
$model = auth()->getProvider();
250-
$model->save($this->user);
250+
$model->saveWithEmailIdentity($this->user);
251251

252252
$result = $this->actingAs($this->user, true)
253253
->withSession($this->getSessionUserInfo(EmailActivator::class))

tests/Unit/UserModelTest.php

Lines changed: 5 additions & 5 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->save($user);
31+
$users->saveWithEmailIdentity($user);
3232

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

6161
$user = $users->findByCredentials(['email' => '[email protected]']);
6262

6363
$user->username = 'bar';
6464
$user->email = '[email protected]';
6565
$user->active = 1;
6666

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

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

8585
$user = $users->findByCredentials(['email' => '[email protected]']);
8686

8787
$user->email = '[email protected]';
8888

89-
$users->save($user);
89+
$users->saveWithEmailIdentity($user);
9090

9191
$this->seeInDatabase('auth_identities', [
9292
'user_id' => $user->id,

tests/Unit/UserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testUpdateEmail(): void
112112
$this->user->active = 0;
113113

114114
$users = model(UserModel::class);
115-
$users->save($this->user);
115+
$users->saveWithEmailIdentity($this->user);
116116

117117
$user = $users->find($this->user->id);
118118

@@ -134,7 +134,7 @@ public function testUpdatePassword(): void
134134
$this->user->active = 0;
135135

136136
$users = model(UserModel::class);
137-
$users->save($this->user);
137+
$users->saveWithEmailIdentity($this->user);
138138

139139
$user = $users->find($this->user->id);
140140

@@ -153,7 +153,7 @@ public function testUpdatePasswordHash(): void
153153
$this->user->active = 0;
154154

155155
$users = model(UserModel::class);
156-
$users->save($this->user);
156+
$users->saveWithEmailIdentity($this->user);
157157

158158
$user = $users->find($this->user->id);
159159

0 commit comments

Comments
 (0)