fix: make sure user searches are always case-insensitive

See https://kolaente.dev/vikunja/frontend/issues/2196#issuecomment-33698
Resolves https://github.com/go-vikunja/frontend/issues/29
This commit is contained in:
kolaente 2022-10-01 17:39:04 +02:00
parent 36265fcedf
commit c076f73a87
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 23 additions and 2 deletions

View file

@ -369,6 +369,16 @@ func TestListUsers(t *testing.T) {
assert.True(t, len(all) > 0)
assert.Equal(t, all[0].Username, "user1")
})
t.Run("case insensitive", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
all, err := ListUsers(s, "uSEr1", nil)
assert.NoError(t, err)
assert.True(t, len(all) > 0)
assert.Equal(t, all[0].Username, "user1")
})
t.Run("all users", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()

View file

@ -19,8 +19,11 @@ package user
import (
"strings"
"code.vikunja.io/api/pkg/db"
"xorm.io/builder"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
type ListUserOpts struct {
@ -45,14 +48,22 @@ func ListUsers(s *xorm.Session, search string, opts *ListUserOpts) (users []*Use
if search != "" {
for _, queryPart := range strings.Split(search, ",") {
var usernameCond builder.Cond = builder.Eq{"username": queryPart}
if db.Type() == schemas.POSTGRES {
usernameCond = builder.Expr("username ILIKE ?", queryPart)
}
if db.Type() == schemas.SQLITE {
usernameCond = builder.Expr("username = ? COLLATE NOCASE", queryPart)
}
conds = append(conds,
builder.Eq{"username": queryPart},
usernameCond,
builder.And(
builder.Eq{"email": queryPart},
builder.Eq{"discoverable_by_email": true},
),
builder.And(
builder.Like{"name", "%" + queryPart + "%"},
db.ILIKE("name", queryPart),
builder.Eq{"discoverable_by_name": true},
),
)