QueryProvider: use left joins and fix the use of the "disabled" column.

The code used union selects; left joins at least are easier to read. In
some places it was also not so clear that the WHERE part for the
disabled column really worked.

The query FIND_GROUP_USERS also needs to take the disabled column in to
account.

Signed-off-by: Claus-Justus Heine <himself@claus-justus-heine.de>
This commit is contained in:
Claus-Justus Heine
2023-08-22 11:10:16 +02:00
parent df5a550fbd
commit 639b9cf995

View File

@@ -129,10 +129,13 @@ class QueryProvider implements \ArrayAccess
$this->queries = [ $this->queries = [
Query::BELONGS_TO_ADMIN => Query::BELONGS_TO_ADMIN =>
"SELECT COUNT(g.$gGID) > 0 AS admin " . "SELECT COUNT(g.$gGID) > 0 AS admin " .
"FROM $group g, $userGroup ug " . "FROM $group g " .
"LEFT JOIN $userGroup ug ON ug.$ugGID = g.$gGID " .
(empty($uDisabled) ? "" : "LEFT JOIN $user u ON u.$uUID = ug.$ugUID ") .
"WHERE ug.$ugGID = g.$gGID " . "WHERE ug.$ugGID = g.$gGID " .
"AND ug.$ugUID = :$uidParam " . "AND ug.$ugUID = :$uidParam " .
"AND g.$gAdmin", "AND g.$gAdmin" .
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::COUNT_GROUPS => Query::COUNT_GROUPS =>
"SELECT COUNT(DISTINCT ug.$ugUID) " . "SELECT COUNT(DISTINCT ug.$ugUID) " .
@@ -154,8 +157,10 @@ class QueryProvider implements \ArrayAccess
Query::FIND_GROUP_USERS => Query::FIND_GROUP_USERS =>
"SELECT DISTINCT ug.$ugUID AS uid " . "SELECT DISTINCT ug.$ugUID AS uid " .
"FROM $userGroup ug " . "FROM $userGroup ug " .
"LEFT JOIN $user u ON u.$uUID = ug.$ugUID " .
"WHERE ug.$ugGID LIKE :$gidParam " . "WHERE ug.$ugGID LIKE :$gidParam " .
"AND ug.$ugUID LIKE :$searchParam " . "AND ug.$ugUID LIKE :$searchParam " .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") .
"ORDER BY ug.$ugUID", "ORDER BY ug.$ugUID",
Query::FIND_GROUPS => Query::FIND_GROUPS =>
@@ -168,38 +173,38 @@ class QueryProvider implements \ArrayAccess
Query::FIND_USER_BY_UID => Query::FIND_USER_BY_UID =>
"SELECT $userColumns " . "SELECT $userColumns " .
"FROM $user u " . "FROM $user u " .
"WHERE u.$uUID = :$uidParam " . "WHERE u.$uUID = :$uidParam" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"), (empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::FIND_USER_BY_USERNAME => Query::FIND_USER_BY_USERNAME =>
"SELECT $userColumns, u.$uPassword AS password " . "SELECT $userColumns, u.$uPassword AS password " .
"FROM $user u " . "FROM $user u " .
"WHERE u.$uUsername = :$usernameParam " . "WHERE u.$uUsername = :$usernameParam" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"), (empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::FIND_USER_BY_USERNAME_CASE_INSENSITIVE => Query::FIND_USER_BY_USERNAME_CASE_INSENSITIVE =>
"SELECT $userColumns, u.$uPassword AS password " . "SELECT $userColumns, u.$uPassword AS password " .
"FROM $user u " . "FROM $user u " .
"WHERE lower(u.$uUsername) = lower(:$usernameParam) " . "WHERE lower(u.$uUsername) = lower(:$usernameParam)" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"), (empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::FIND_USER_BY_USERNAME_OR_EMAIL => Query::FIND_USER_BY_USERNAME_OR_EMAIL =>
"SELECT $userColumns, u.$uPassword AS password " . "SELECT $userColumns, u.$uPassword AS password " .
"FROM $user u " . "FROM $user u " .
"WHERE u.$uUsername = :$usernameParam OR u.$uEmail = :$emailParam " . "WHERE u.$uUsername = :$usernameParam OR u.$uEmail = :$emailParam" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"), (empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE => Query::FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE =>
"SELECT $userColumns, u.$uPassword AS password " . "SELECT $userColumns, u.$uPassword AS password " .
"FROM $user u " . "FROM $user u " .
"WHERE lower(u.$uUsername) = lower(:$usernameParam) OR lower(u.$uEmail) = lower(:$emailParam) " . "WHERE lower(u.$uUsername) = lower(:$usernameParam) OR lower(u.$uEmail) = lower(:$emailParam)" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"), (empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
Query::FIND_USER_GROUPS => Query::FIND_USER_GROUPS =>
"SELECT $groupColumns " . "SELECT $groupColumns " .
"FROM $group g, $userGroup ug " . "FROM $group g " .
"WHERE ug.$ugGID = g.$gGID " . "LEFT JOIN $userGroup ug ON ug.$ugGID = g.$gGID " .
"AND ug.$ugUID = :$uidParam " . "WHERE ug.$ugUID = :$uidParam " .
"ORDER BY g.$gGID", "ORDER BY g.$gGID",
Query::FIND_USERS => Query::FIND_USERS =>
@@ -210,7 +215,7 @@ class QueryProvider implements \ArrayAccess
(empty($uName) ? "" : "OR u.$uName LIKE :$searchParam ") . (empty($uName) ? "" : "OR u.$uName LIKE :$searchParam ") .
(empty($uEmail) ? "" : "OR u.$uEmail LIKE :$searchParam ") . (empty($uEmail) ? "" : "OR u.$uEmail LIKE :$searchParam ") .
")" . ")" .
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") . (empty($uDisabled) ? "" : " AND NOT u.$uDisabled ") .
"ORDER BY u.$uUID", "ORDER BY u.$uUID",
Query::UPDATE_DISPLAY_NAME => Query::UPDATE_DISPLAY_NAME =>