Skip to content

Commit e817020

Browse files
committed
Database: Fix overly greedy ban search query
1 parent 4efc2f1 commit e817020

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

‎database/login.go‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ import (
1212
)
1313

1414
const (
15-
SearchUserBan = `WITH known_ng_device_ids AS (
15+
SearchUserBan = `
16+
WITH known_ng_device_ids AS (
1617
WITH RECURSIVE device_tree AS (
1718
SELECT unnest(ng_device_id) AS device_id
18-
FROM users
19-
WHERE ng_device_id && $1
19+
FROM users
20+
WHERE allow_default_keys = FALSE AND ng_device_id && $1
2021
UNION
2122
SELECT unnest(ng_device_id)
22-
FROM users
23-
JOIN device_tree dt
24-
ON ng_device_id && array[dt.device_id]
23+
FROM users
24+
JOIN device_tree dt
25+
ON allow_default_keys = FALSE AND ng_device_id && array[dt.device_id]
2526
) SELECT array_agg(DISTINCT device_id) FROM device_tree
26-
)
27-
SELECT has_ban, ban_tos, ng_device_id, ban_reason
28-
FROM users
29-
WHERE has_ban = true
30-
AND ((profile_id = $2 OR allow_default_keys = FALSE)
31-
OR ng_device_id && (SELECT * FROM known_ng_device_ids)
32-
OR last_ip_address = $3
33-
OR ($4 != '' AND last_ip_address = $4))
34-
AND (ban_expires IS NULL OR ban_expires > $5)
35-
ORDER BY ban_tos DESC LIMIT 1`
27+
) SELECT has_ban, ban_tos, ng_device_id, ban_reason
28+
FROM users
29+
WHERE has_ban = true
30+
AND (profile_id = $2
31+
OR (allow_default_keys = FALSE AND ng_device_id && (SELECT * FROM known_ng_device_ids))
32+
OR last_ip_address = $3
33+
OR ($4 != '' AND last_ip_address = $4))
34+
AND (ban_expires IS NULL OR ban_expires > $5)
35+
ORDER BY ban_tos DESC LIMIT 1`
3636
)
3737

3838
var (

0 commit comments

Comments
 (0)