Skip to content

Commit 639b9cf

Browse files
committed
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>
1 parent df5a550 commit 639b9cf

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

lib/Query/QueryProvider.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ private function loadQueries()
129129
$this->queries = [
130130
Query::BELONGS_TO_ADMIN =>
131131
"SELECT COUNT(g.$gGID) > 0 AS admin " .
132-
"FROM $group g, $userGroup ug " .
132+
"FROM $group g " .
133+
"LEFT JOIN $userGroup ug ON ug.$ugGID = g.$gGID " .
134+
(empty($uDisabled) ? "" : "LEFT JOIN $user u ON u.$uUID = ug.$ugUID ") .
133135
"WHERE ug.$ugGID = g.$gGID " .
134136
"AND ug.$ugUID = :$uidParam " .
135-
"AND g.$gAdmin",
137+
"AND g.$gAdmin" .
138+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
136139

137140
Query::COUNT_GROUPS =>
138141
"SELECT COUNT(DISTINCT ug.$ugUID) " .
@@ -154,8 +157,10 @@ private function loadQueries()
154157
Query::FIND_GROUP_USERS =>
155158
"SELECT DISTINCT ug.$ugUID AS uid " .
156159
"FROM $userGroup ug " .
160+
"LEFT JOIN $user u ON u.$uUID = ug.$ugUID " .
157161
"WHERE ug.$ugGID LIKE :$gidParam " .
158162
"AND ug.$ugUID LIKE :$searchParam " .
163+
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") .
159164
"ORDER BY ug.$ugUID",
160165

161166
Query::FIND_GROUPS =>
@@ -168,38 +173,38 @@ private function loadQueries()
168173
Query::FIND_USER_BY_UID =>
169174
"SELECT $userColumns " .
170175
"FROM $user u " .
171-
"WHERE u.$uUID = :$uidParam " .
172-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
176+
"WHERE u.$uUID = :$uidParam" .
177+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
173178

174179
Query::FIND_USER_BY_USERNAME =>
175180
"SELECT $userColumns, u.$uPassword AS password " .
176181
"FROM $user u " .
177-
"WHERE u.$uUsername = :$usernameParam " .
178-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
182+
"WHERE u.$uUsername = :$usernameParam" .
183+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
179184

180185
Query::FIND_USER_BY_USERNAME_CASE_INSENSITIVE =>
181186
"SELECT $userColumns, u.$uPassword AS password " .
182187
"FROM $user u " .
183-
"WHERE lower(u.$uUsername) = lower(:$usernameParam) " .
184-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
188+
"WHERE lower(u.$uUsername) = lower(:$usernameParam)" .
189+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
185190

186191
Query::FIND_USER_BY_USERNAME_OR_EMAIL =>
187192
"SELECT $userColumns, u.$uPassword AS password " .
188193
"FROM $user u " .
189-
"WHERE u.$uUsername = :$usernameParam OR u.$uEmail = :$emailParam " .
190-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
194+
"WHERE u.$uUsername = :$usernameParam OR u.$uEmail = :$emailParam" .
195+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
191196

192197
Query::FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE =>
193198
"SELECT $userColumns, u.$uPassword AS password " .
194199
"FROM $user u " .
195-
"WHERE lower(u.$uUsername) = lower(:$usernameParam) OR lower(u.$uEmail) = lower(:$emailParam) " .
196-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
200+
"WHERE lower(u.$uUsername) = lower(:$usernameParam) OR lower(u.$uEmail) = lower(:$emailParam)" .
201+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled"),
197202

198203
Query::FIND_USER_GROUPS =>
199204
"SELECT $groupColumns " .
200-
"FROM $group g, $userGroup ug " .
201-
"WHERE ug.$ugGID = g.$gGID " .
202-
"AND ug.$ugUID = :$uidParam " .
205+
"FROM $group g " .
206+
"LEFT JOIN $userGroup ug ON ug.$ugGID = g.$gGID " .
207+
"WHERE ug.$ugUID = :$uidParam " .
203208
"ORDER BY g.$gGID",
204209

205210
Query::FIND_USERS =>
@@ -210,7 +215,7 @@ private function loadQueries()
210215
(empty($uName) ? "" : "OR u.$uName LIKE :$searchParam ") .
211216
(empty($uEmail) ? "" : "OR u.$uEmail LIKE :$searchParam ") .
212217
")" .
213-
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") .
218+
(empty($uDisabled) ? "" : " AND NOT u.$uDisabled ") .
214219
"ORDER BY u.$uUID",
215220

216221
Query::UPDATE_DISPLAY_NAME =>

0 commit comments

Comments
 (0)