1
0

Enable searching users by full email or name

This commit is contained in:
kolaente
2021-04-07 18:28:58 +02:00
parent 8ddc00bd29
commit 126f3acdc8
14 changed files with 191 additions and 42 deletions

View File

@ -17,42 +17,40 @@
package user
import (
"strconv"
"strings"
"xorm.io/builder"
"xorm.io/xorm"
"code.vikunja.io/api/pkg/log"
)
// ListUsers returns a list with all users, filtered by an optional searchstring
func ListUsers(s *xorm.Session, searchterm string) (users []*User, err error) {
func ListUsers(s *xorm.Session, search string) (users []*User, err error) {
vals := strings.Split(searchterm, ",")
ids := []int64{}
for _, val := range vals {
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
log.Debugf("User search string part '%s' is not a number: %s", val, err)
continue
}
ids = append(ids, v)
}
// Prevent searching for placeholders
search = strings.ReplaceAll(search, "%", "")
if len(ids) > 0 {
err = s.
In("id", ids).
Find(&users)
return
}
if searchterm == "" {
err = s.Find(&users)
if search == "" || strings.ReplaceAll(search, " ", "") == "" {
return
}
err = s.
Where("username LIKE ?", "%"+searchterm+"%").
Where(builder.Or(
builder.Like{"username", "%" + search + "%"},
builder.And(
builder.Eq{"email": search},
builder.Eq{"discoverable_by_email": true},
),
builder.And(
builder.Like{"name", "%" + search + "%"},
builder.Eq{"discoverable_by_name": true},
),
)).
Find(&users)
return
}
// ListAllUsers returns all users
func ListAllUsers(s *xorm.Session) (users []*User, err error) {
err = s.Find(&users)
return
}