diff options
author | yemkareems <yemkareems@gmail.com> | 2024-05-28 16:23:51 +0530 |
---|---|---|
committer | yemkareems <yemkareems@gmail.com> | 2024-07-08 15:42:55 +0530 |
commit | 76c875a5882753e37ef39015a1711b68e7ea0d0b (patch) | |
tree | 15f233f126587ba56b8c58a435e11c8632dc2550 /lib/private/User | |
parent | 33b38c6573cbd197989539601ddfcb4524eb4c48 (diff) | |
download | nextcloud-server-76c875a5882753e37ef39015a1711b68e7ea0d0b.tar.gz nextcloud-server-76c875a5882753e37ef39015a1711b68e7ea0d0b.zip |
fix: change orderBy and sort to sortMode and sortOrder. default it to uid asc. enable email search by changing query
Signed-off-by: yemkareems <yemkareems@gmail.com>
Diffstat (limited to 'lib/private/User')
-rw-r--r-- | lib/private/User/Backend.php | 12 | ||||
-rw-r--r-- | lib/private/User/Database.php | 35 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 30 |
3 files changed, 41 insertions, 36 deletions
diff --git a/lib/private/User/Backend.php b/lib/private/User/Backend.php index d58092feac1..5d06b54c28b 100644 --- a/lib/private/User/Backend.php +++ b/lib/private/User/Backend.php @@ -89,11 +89,11 @@ abstract class Backend implements UserInterface { * @param string $search * @param null|int $limit * @param null|int $offset - * @param string $orderBy - * @param string $sort + * @param string $sortMode + * @param string $sortOrder * @return string[] an array of all uids */ - public function getUsers($search = '', $limit = null, $offset = null, $orderBy = 'lastLogin', $sort = 'DESC') { + public function getUsers($search = '', $limit = null, $offset = null, string $sortMode = 'uid', string $sortOrder = 'asc') { return []; } @@ -130,11 +130,11 @@ abstract class Backend implements UserInterface { * @param string $search * @param int|null $limit * @param int|null $offset - * @param string $orderBy - * @param string $sort + * @param string $sortMode + * @param string $sortOrder * @return array an array of all displayNames (value) and the corresponding uids (key) */ - public function getDisplayNames($search = '', $limit = null, $offset = null, string $orderBy = 'lastLogin', string $sort = 'DESC'): array { + public function getDisplayNames($search = '', $limit = null, $offset = null, string $sortMode = 'uid', string $sortOrder = 'asc'): array { $displayNames = []; $users = $this->getUsers($search, $limit, $offset); foreach ($users as $user) { diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php index 6a1861e5dba..9a62bcf3e12 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -227,36 +227,43 @@ class Database extends ABackend implements * @param string $search * @param int|null $limit * @param int|null $offset - * @param string $orderBy - * @param string $sort + * @param string $sortMode + * @param string $sortOrder * @return array an array of all displayNames (value) and the corresponding uids (key) */ - public function getDisplayNames($search = '', $limit = null, $offset = null, string $orderBy = 'lastLogin', string $sort = 'DESC'): array { + public function getDisplayNames($search = '', $limit = null, $offset = null, string $sortMode = 'uid', string $sortOrder = 'asc'): array { $limit = $this->fixLimit($limit); $this->fixDI(); $query = $this->dbConn->getQueryBuilder(); - $appId = 'settings'; - $configKey = 'email'; - if($orderBy == 'lastLogin') { - $appId = 'login'; - $configKey = 'lastLogin'; + if ($sortMode === 'lastLogin') { + $lastLoginSubSelect = $this->dbConn->getQueryBuilder(); + $lastLoginSubSelect->select('configvalue') + ->from('preferences', 'p2') + ->where($lastLoginSubSelect->expr()->andX( + $lastLoginSubSelect->expr()->eq('p2.userid', 'uid'), + $lastLoginSubSelect->expr()->eq('p2.appid', $lastLoginSubSelect->expr()->literal('login')), + $lastLoginSubSelect->expr()->eq('p2.configkey', $lastLoginSubSelect->expr()->literal('lastLogin')), + )); + $orderByExpression = $query->createFunction('(' . $lastLoginSubSelect->getSQL() .')'); + } else { + $orderByExpression = $query->func()->lower('displayname'); } - $query->select('uid', 'displayname') + $query->select('uid', 'displayname', $orderByExpression) ->from($this->table, 'u') ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( $query->expr()->eq('userid', 'uid'), - $query->expr()->eq('appid', $query->expr()->literal($appId)), - $query->expr()->eq('configkey', $query->expr()->literal($configKey))) + $query->expr()->eq('appid', $query->expr()->literal('settings')), + $query->expr()->eq('configkey', $query->expr()->literal('email'))) ) // sqlite doesn't like re-using a single named parameter here ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) - ->orderBy($query->func()->lower('configvalue'), $sort) + ->orderBy($orderByExpression, $sortOrder) ->addOrderBy('uid_lower', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); @@ -394,11 +401,9 @@ class Database extends ABackend implements $limit = $this->fixLimit($limit); $users = $this->getDisplayNames($search, $limit, $offset, $orderBy, $sort); - $userIds = array_map(function ($uid) { + return array_map(function ($uid) { return (string)$uid; }, array_keys($users)); - sort($userIds, SORT_STRING | SORT_FLAG_CASE); - return $userIds; } /** diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index b28adb39774..49f96c25a05 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -262,38 +262,38 @@ class Manager extends PublicEmitter implements IUserManager { * @return IUser[] * @deprecated since 27.0.0, use searchDisplayName instead */ - public function search($pattern, $limit = null, $offset = null, $orderBy = 'uid', $sort = 'ASC'): array { + public function search($pattern, $limit = null, $offset = null, $sortMode = 'uid', $sortOrder = 'asc'): array { $users = []; foreach ($this->backends as $backend) { - $backendUsers = $backend->getUsers($pattern, $limit, $offset, $orderBy, $sort); + $backendUsers = $backend->getUsers($pattern, $limit, $offset, $sortMode, $sortOrder); if (is_array($backendUsers)) { foreach ($backendUsers as $uid) { $users[$uid] = new LazyUser($uid, $this, null, $backend); } } } - switch ($orderBy.' '.$sort) { - case 'uid DESC': + switch ($sortMode.' '.$sortOrder) { + case 'uid desc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($b->getUID(), $a->getUID()); }); break; - case 'lastLogin ASC': + case 'lastLogin asc': uasort($users, function (IUser $a, IUser $b) { return $a->getLastLogin() - $b->getLastLogin(); }); break; - case 'lastLogin DESC': + case 'lastLogin desc': uasort($users, function (IUser $a, IUser $b) { return $b->getLastLogin() - $a->getLastLogin(); }); break; - case 'displayName ASC': + case 'displayName asc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($a->getDisplayName(), $b->getDisplayName()); }); break; - case 'displayName DESC': + case 'displayName desc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($b->getDisplayName(), $a->getDisplayName()); }); @@ -316,7 +316,7 @@ class Manager extends PublicEmitter implements IUserManager { * @param int $offset * @return IUser[] */ - public function searchDisplayName($pattern, $limit = null, $offset = null, $orderBy = 'displayName', $sort = 'ASC') { + public function searchDisplayName($pattern, $limit = null, $offset = null, $sortMode = 'uid', $sortOrder = 'asc') { $users = []; foreach ($this->backends as $backend) { $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); @@ -327,28 +327,28 @@ class Manager extends PublicEmitter implements IUserManager { } } - switch ($orderBy.' '.$sort) { - case 'uid ASC': + switch ($sortMode.' '.$sortOrder) { + case 'uid asc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($a->getUID(), $b->getUID()); }); break; - case 'uid DESC': + case 'uid desc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($b->getUID(), $a->getUID()); }); break; - case 'lastLogin ASC': + case 'lastLogin asc': uasort($users, function (IUser $a, IUser $b) { return $a->getLastLogin() - $b->getLastLogin(); }); break; - case 'lastLogin DESC': + case 'lastLogin desc': uasort($users, function (IUser $a, IUser $b) { return $b->getLastLogin() - $a->getLastLogin(); }); break; - case 'displayName DESC': + case 'displayName asc': uasort($users, function (IUser $a, IUser $b) { return strcasecmp($b->getDisplayName(), $a->getDisplayName()); }); |