codeigniter4 / shield

Authentication and Authorization for CodeIgniter 4
https://shield.codeigniter.com
MIT License
366 stars 133 forks source link

Bug: UserModel cannot save an array #471

Closed MGatner closed 2 years ago

MGatner commented 2 years ago

PHP Version

8.1.2

CodeIgniter4 Version

4.2.7

Shield Version

develop

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MySQL 8

Did you customize Shield?

No

What happened?

Calling model(UserModel::class)->save() with a array representing an existing user causes $tempUser to be null: https://github.com/codeigniter4/shield/blob/f4cdfb672b600a032a6f0bfc0b7735411bee0cae/src/Models/UserModel.php#L250

Which makes the call to saveEmailIdentity() fail: https://github.com/codeigniter4/shield/blob/f4cdfb672b600a032a6f0bfc0b7735411bee0cae/src/Models/UserModel.php#L261

Steps to Reproduce

This is obviously contrived but should cause the error:

$user = fake(UserModel::class);
$user->email = 'test@example.com';

model(UserModel::class)->save($user->toArray());

Expected Output

No exception

Anything else?

Trace:

1) Tests\Integration\UserShieldTest::testSave
Error: Call to a member function saveEmailIdentity() on null

vendor/codeigniter4/shield/src/Models/UserModel.php:261
vendor/codeigniter4/framework/system/BaseModel.php:656
vendor/codeigniter4/shield/src/Models/UserModel.php:284
kenjis commented 2 years ago

The code in Steps to Reproduce runs without errors. So I can't write test code, but the fix is the following:

--- a/src/Models/UserModel.php
+++ b/src/Models/UserModel.php
@@ -253,6 +253,10 @@ public function update($id = null, $data = null): bool
             /** @throws DataException */
             $result = parent::update($id, $data);
         } catch (DataException $e) {
+            if ($this->tempUser === null) {
+                return true;
+            }
+
             $messages = [
                 lang('Database.emptyDataset', ['update']),
             ];