Skip to content

Commit 9c1822c

Browse files
authored
Merge pull request #308 from kenjis/fix-UserModel-findById
fix: UserModel::findById() does not work with withIdentities()
2 parents 00102e1 + 81e3db6 commit 9c1822c

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

src/Models/UserModel.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeIgniter\Model;
77
use CodeIgniter\Shield\Authentication\Authenticators\Session;
88
use CodeIgniter\Shield\Entities\User;
9+
use CodeIgniter\Shield\Entities\UserIdentity;
910
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
1011
use CodeIgniter\Shield\Exceptions\ValidationException;
1112
use Faker\Generator;
@@ -82,27 +83,44 @@ protected function fetchIdentities(array $data): array
8283
return $data;
8384
}
8485

85-
// Map our users by ID to make assigning simpler
86+
$mappedUsers = $this->assignIdentities($data, $identities);
87+
88+
$data['data'] = $data['singleton'] ? $mappedUsers[$data['id']] : $mappedUsers;
89+
90+
return $data;
91+
}
92+
93+
/**
94+
* Map our users by ID to make assigning simpler
95+
*
96+
* @param array $data Event $data
97+
* @param UserIdentity[] $identities
98+
*
99+
* @return User[] UserId => User object
100+
* @phpstan-return array<int|string, User> UserId => User object
101+
*/
102+
private function assignIdentities(array $data, array $identities): array
103+
{
86104
$mappedUsers = [];
87-
$users = $data['singleton']
88-
? $data
89-
: $data['data'];
105+
106+
$users = $data['singleton'] ? [$data['data']] : $data['data'];
90107

91108
foreach ($users as $user) {
92109
$mappedUsers[$user->id] = $user;
93110
}
94111
unset($users);
95112

96113
// Now assign the identities to the user
97-
foreach ($identities as $id) {
98-
$array = $mappedUsers[$id->user_id]->identities;
99-
$array[] = $id;
100-
$mappedUsers[$id->user_id]->identities = $array;
101-
}
114+
foreach ($identities as $identity) {
115+
$userId = $identity->user_id;
102116

103-
$data['data'] = $mappedUsers;
117+
$newIdentities = $mappedUsers[$userId]->identities;
118+
$newIdentities[] = $identity;
104119

105-
return $data;
120+
$mappedUsers[$userId]->identities = $newIdentities;
121+
}
122+
123+
return $mappedUsers;
106124
}
107125

108126
/**

tests/Unit/UserTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testGetIdentitiesByType(): void
5353
$this->assertEmpty($this->user->getIdentities('foo'));
5454
}
5555

56-
public function testModelWithIdentities(): void
56+
public function testModelfindAllWithIdentities(): void
5757
{
5858
fake(UserModel::class);
5959
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'password']);
@@ -67,6 +67,18 @@ public function testModelWithIdentities(): void
6767
$this->assertCount(2, $identities);
6868
}
6969

70+
public function testModelfindByIdWithIdentities(): void
71+
{
72+
fake(UserModel::class);
73+
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'password']);
74+
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'access_token']);
75+
76+
// Grab the user again, using the model's identity helper
77+
$user = model(UserModel::class)->withIdentities()->findById(1);
78+
79+
$this->assertCount(2, $user->identities);
80+
}
81+
7082
public function testLastLogin(): void
7183
{
7284
fake(

0 commit comments

Comments
 (0)