Skip to content

Commit 856bc2d

Browse files
committed
Always return active, non-pending users first
1 parent 380138e commit 856bc2d

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- User queries now always return active, non-pending users first, unless otherwise specified by `orderBy`. ([#18148](https://github.com/craftcms/cms/issues/18148))
56
- Fixed a bug where all plugin settings were being saved to the project config, rather than just posted settings. ([craftcms/commerce#4006](https://github.com/craftcms/commerce/issues/4006))
67
- Fixed a bug where Matrix fields’ Entry Types settings were partially interactive when admin changes were disallowed. ([#18145](https://github.com/craftcms/cms/pull/18145))
78
- Fixed a bug where users could be unable to sign in if an inactive user account existed with the same email address. ([#18148](https://github.com/craftcms/cms/issues/18148))

src/elements/db/UserQuery.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ class UserQuery extends ElementQuery
5050
/**
5151
* @inheritdoc
5252
*/
53-
protected array $defaultOrderBy = ['users.username' => SORT_ASC];
53+
protected array $defaultOrderBy = [
54+
'users.username' => SORT_ASC,
55+
'users.active' => SORT_DESC,
56+
'users.pending' => SORT_DESC,
57+
];
5458

5559
// General parameters
5660
// -------------------------------------------------------------------------
@@ -1103,6 +1107,21 @@ protected function beforePrepare(): bool
11031107
]);
11041108
}
11051109

1110+
// If there's a custom orderBy, make sure we're showing active, non-pending accounts first
1111+
if (
1112+
is_array($this->orderBy) &&
1113+
empty($this->query->orderBy) &&
1114+
(
1115+
count($this->orderBy) !== 1 ||
1116+
!($this->orderBy[0] ?? null) instanceof OrderByPlaceholderExpression
1117+
)
1118+
) {
1119+
$this->orderBy = array_merge($this->orderBy, [
1120+
'users.active' => SORT_DESC,
1121+
'users.pending' => SORT_DESC,
1122+
]);
1123+
}
1124+
11061125
return true;
11071126
}
11081127

src/services/Users.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@ public function getUserByUsernameOrEmail(string $usernameOrEmail): ?User
294294
]);
295295
}
296296

297-
// order by credentialed users first
298-
$query->orderBy(['users.active' => SORT_DESC, 'users.pending' => SORT_DESC]);
299-
300297
/** @var User|null */
301298
return $query->one();
302299
}

0 commit comments

Comments
 (0)