Skip to content

fix: UserModel::assignIdentities() always produces a DB query #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ parameters:
ignoreErrors:
- '#Call to an undefined method CodeIgniter\\Database\\ConnectionInterface::[A-Za-z].+\(\)#'
- '#Cannot access property [\$a-z_]+ on (array|object)#'
-
- '#Call to an undefined method CodeIgniter\\Shield\\Models\\UserModel::getLastQuery\(\)#'
-
message: '#Call to deprecated function random_string\(\):#'
paths:
- src/Authentication/Actions/Email2FA.php
Expand Down
5 changes: 5 additions & 0 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public function getIdentities(string $type = 'all'): array
return $identities;
}

public function setIdentities(array $identities): void
{
$this->identities = $identities;
}

/**
* Creates a new identity for this user with an email/password
* combination.
Expand Down
17 changes: 10 additions & 7 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ protected function fetchIdentities(array $data): array
*/
private function assignIdentities(array $data, array $identities): array
{
$mappedUsers = [];
$mappedUsers = [];
$userIdentities = [];

$users = $data['singleton'] ? [$data['data']] : $data['data'];

Expand All @@ -122,15 +123,17 @@ private function assignIdentities(array $data, array $identities): array
}
unset($users);

// Now assign the identities to the user
// Now group the identities by user
foreach ($identities as $identity) {
$userId = $identity->user_id;

$newIdentities = $mappedUsers[$userId]->identities;
$newIdentities[] = $identity;
$userIdentities[$identity->user_id][] = $identity;
}
unset($identities);

$mappedUsers[$userId]->identities = $newIdentities;
// Now assign the identities to the user
foreach ($userIdentities as $userId => $identityArray) {
$mappedUsers[$userId]->identities = $identityArray;
}
unset($userIdentities);

return $mappedUsers;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ public function testModelFindByIdWithIdentities(): void
$this->assertCount(2, $user->identities);
}

public function testModelFindAllWithIdentitiesWhereInQuery(): void
{
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'password']);
fake(UserIdentityModel::class, ['user_id' => $this->user->id, 'type' => 'access_token']);

// Grab the user again, using the model's identity helper
$users = model(UserModel::class)->withIdentities()->findAll();

$identities = [];

foreach ($users as $user) {
if ($user->id !== $this->user->id) {
continue;
}

$identities = $user->identities;

// Check the last query and see if a proper type of query was used
$query = (string) model(UserModel::class)->getLastQuery();
$this->assertMatchesRegularExpression(
'/WHERE\s+.*\s+IN\s+\([^)]+\)/i',
$query,
'Identities were not obtained with the single query (missing "WHERE ... IN" condition)'
);
}

$this->assertCount(2, $identities);
}

public function testModelFindByIdWithIdentitiesUserNotExists(): void
{
$user = model(UserModel::class)->where('active', 0)->withIdentities()->findById(1);
Expand Down