|
6 | 6 | use CodeIgniter\Model; |
7 | 7 | use CodeIgniter\Shield\Authentication\Authenticators\Session; |
8 | 8 | use CodeIgniter\Shield\Entities\User; |
| 9 | +use CodeIgniter\Shield\Entities\UserIdentity; |
9 | 10 | use CodeIgniter\Shield\Exceptions\InvalidArgumentException; |
10 | 11 | use CodeIgniter\Shield\Exceptions\ValidationException; |
11 | 12 | use Faker\Generator; |
@@ -82,27 +83,44 @@ protected function fetchIdentities(array $data): array |
82 | 83 | return $data; |
83 | 84 | } |
84 | 85 |
|
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 | + { |
86 | 104 | $mappedUsers = []; |
87 | | - $users = $data['singleton'] |
88 | | - ? $data |
89 | | - : $data['data']; |
| 105 | + |
| 106 | + $users = $data['singleton'] ? [$data['data']] : $data['data']; |
90 | 107 |
|
91 | 108 | foreach ($users as $user) { |
92 | 109 | $mappedUsers[$user->id] = $user; |
93 | 110 | } |
94 | 111 | unset($users); |
95 | 112 |
|
96 | 113 | // 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; |
102 | 116 |
|
103 | | - $data['data'] = $mappedUsers; |
| 117 | + $newIdentities = $mappedUsers[$userId]->identities; |
| 118 | + $newIdentities[] = $identity; |
104 | 119 |
|
105 | | - return $data; |
| 120 | + $mappedUsers[$userId]->identities = $newIdentities; |
| 121 | + } |
| 122 | + |
| 123 | + return $mappedUsers; |
106 | 124 | } |
107 | 125 |
|
108 | 126 | /** |
|
0 commit comments