diff options
author | yemkareems <yemkareems@gmail.com> | 2024-05-08 14:43:06 +0530 |
---|---|---|
committer | yemkareems <yemkareems@gmail.com> | 2024-07-08 15:42:55 +0530 |
commit | c8c68c3510d1d2ccd73ff5a351ac3b1e5c5a2c5a (patch) | |
tree | 3ce970ffcbad0b29dfc01cd21daa10bf94f651a8 | |
parent | 87a8013ee3d81a3ca8fb300db9fbb8d587c4ed38 (diff) | |
download | nextcloud-server-c8c68c3510d1d2ccd73ff5a351ac3b1e5c5a2c5a.tar.gz nextcloud-server-c8c68c3510d1d2ccd73ff5a351ac3b1e5c5a2c5a.zip |
fix: sort the user getDisplayNames based on lastLogin. Default sort order is lastLogin DESC
Signed-off-by: yemkareems <yemkareems@gmail.com>
-rw-r--r-- | apps/provisioning_api/lib/Controller/UsersController.php | 4 | ||||
-rw-r--r-- | lib/private/User/Backend.php | 8 | ||||
-rw-r--r-- | lib/private/User/Database.php | 19 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 12 | ||||
-rw-r--r-- | lib/public/UserInterface.php | 6 |
5 files changed, 31 insertions, 18 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index fe8a17a6641..e0b5b7b28ac 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -94,7 +94,7 @@ class UsersController extends AUserData { * * 200: Users returned */ - public function getUsers(string $search = '', ?int $limit = null, int $offset = 0, string $orderBy = 'last_login', string $sort = 'DESC'): DataResponse { + public function getUsers(string $search = '', ?int $limit = null, int $offset = 0, string $orderBy = 'lastLogin', string $sort = 'DESC'): DataResponse { $user = $this->userSession->getUser(); $users = []; @@ -137,7 +137,7 @@ class UsersController extends AUserData { * * 200: Users details returned */ - public function getUsersDetails(string $search = '', ?int $limit = null, int $offset = 0, string $orderBy = 'last_login', string $sort = 'DESC'): DataResponse { + public function getUsersDetails(string $search = '', ?int $limit = null, int $offset = 0, string $orderBy = 'lastLogin', string $sort = 'DESC'): DataResponse { $currentUser = $this->userSession->getUser(); $users = []; diff --git a/lib/private/User/Backend.php b/lib/private/User/Backend.php index 98b291e5dee..b21db079d54 100644 --- a/lib/private/User/Backend.php +++ b/lib/private/User/Backend.php @@ -89,9 +89,11 @@ abstract class Backend implements UserInterface { * @param string $search * @param null|int $limit * @param null|int $offset + * @param string $orderBy + * @param string $sort * @return string[] an array of all uids */ - public function getUsers($search = '', $limit = null, $offset = null) { + public function getUsers($search = '', $limit = null, $offset = null, $orderBy = 'lastLogin', $sort = 'DESC'): array { return []; } @@ -128,9 +130,11 @@ abstract class Backend implements UserInterface { * @param string $search * @param int|null $limit * @param int|null $offset + * @param string $orderBy + * @param string $sort * @return array an array of all displayNames (value) and the corresponding uids (key) */ - public function getDisplayNames($search = '', $limit = null, $offset = null) { + public function getDisplayNames($search = '', $limit = null, $offset = null, string $orderBy = 'lastLogin', string $sort = 'DESC'): 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 cc7050f2da8..cb7988783e3 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -227,27 +227,34 @@ class Database extends ABackend implements * @param string $search * @param int|null $limit * @param int|null $offset + * @param string $orderBy + * @param string $sort * @return array an array of all displayNames (value) and the corresponding uids (key) */ - public function getDisplayNames($search = '', $limit = null, $offset = null) { + public function getDisplayNames($search = '', $limit = null, $offset = null, string $orderBy = 'lastLogin', string $sort = 'DESC'): array { $limit = $this->fixLimit($limit); $this->fixDI(); $query = $this->dbConn->getQueryBuilder(); + $appId = 'settings'; $configKey = 'email'; + if($orderBy == 'lastLogin') { + $appId = 'login'; $configKey = 'lastLogin'; + } + $query->select('uid', 'displayname') ->from($this->table, 'u') ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( $query->expr()->eq('userid', 'uid'), - $query->expr()->eq('appid', $query->expr()->literal('settings')), - $query->expr()->eq('configkey', $query->expr()->literal('email'))) + $query->expr()->eq('appid', $query->expr()->literal($appId)), + $query->expr()->eq('configkey', $query->expr()->literal($configKey))) ) // 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('displayname'), 'ASC') + ->orderBy($query->func()->lower('configvalue'), $sort) ->addOrderBy('uid_lower', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); @@ -381,10 +388,10 @@ class Database extends ABackend implements * @param null|int $offset * @return string[] an array of all uids */ - public function getUsers($search = '', $limit = null, $offset = null) { + public function getUsers($search = '', $limit = null, $offset = null, $orderBy = 'lastLogin', $sort = 'DESC'): array { $limit = $this->fixLimit($limit); - $users = $this->getDisplayNames($search, $limit, $offset); + $users = $this->getDisplayNames($search, $limit, $offset, $orderBy, $sort); $userIds = array_map(function ($uid) { return (string)$uid; }, array_keys($users)); diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 87aa859408d..b28adb39774 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -262,10 +262,10 @@ 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') { + public function search($pattern, $limit = null, $offset = null, $orderBy = 'uid', $sort = 'ASC'): array { $users = []; foreach ($this->backends as $backend) { - $backendUsers = $backend->getUsers($pattern, $limit, $offset); + $backendUsers = $backend->getUsers($pattern, $limit, $offset, $orderBy, $sort); if (is_array($backendUsers)) { foreach ($backendUsers as $uid) { $users[$uid] = new LazyUser($uid, $this, null, $backend); @@ -278,12 +278,12 @@ class Manager extends PublicEmitter implements IUserManager { return strcasecmp($b->getUID(), $a->getUID()); }); break; - case 'last_login ASC': + case 'lastLogin ASC': uasort($users, function (IUser $a, IUser $b) { return $a->getLastLogin() - $b->getLastLogin(); }); break; - case 'last_login DESC': + case 'lastLogin DESC': uasort($users, function (IUser $a, IUser $b) { return $b->getLastLogin() - $a->getLastLogin(); }); @@ -338,12 +338,12 @@ class Manager extends PublicEmitter implements IUserManager { return strcasecmp($b->getUID(), $a->getUID()); }); break; - case 'last_login ASC': + case 'lastLogin ASC': uasort($users, function (IUser $a, IUser $b) { return $a->getLastLogin() - $b->getLastLogin(); }); break; - case 'last_login DESC': + case 'lastLogin DESC': uasort($users, function (IUser $a, IUser $b) { return $b->getLastLogin() - $a->getLastLogin(); }); diff --git a/lib/public/UserInterface.php b/lib/public/UserInterface.php index 34e7a09feb7..3f42d17383a 100644 --- a/lib/public/UserInterface.php +++ b/lib/public/UserInterface.php @@ -45,7 +45,7 @@ interface UserInterface { * @return string[] an array of all uids * @since 4.5.0 */ - public function getUsers($search = '', $limit = null, $offset = null); + public function getUsers($search = '', $limit = null, $offset = null, $orderBy = 'lastLogin', $sort = 'DESC'): array; /** * check if a user exists @@ -69,10 +69,12 @@ interface UserInterface { * @param string $search * @param int|null $limit * @param int|null $offset + * @param string $orderBy + * @param string $sort * @return array an array of all displayNames (value) and the corresponding uids (key) * @since 4.5.0 */ - public function getDisplayNames($search = '', $limit = null, $offset = null); + public function getDisplayNames($search = '', $limit = null, $offset = null, string $orderBy = 'lastLogin', string $sort = 'DESC'); /** * Check if a user list is available or not |