2020-01-26 18:08:06 +01:00
|
|
|
// Copyright2018-2020 Vikunja and contriubtors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This file is part of Vikunja.
|
|
|
|
//
|
|
|
|
// Vikunja is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Vikunja is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
)
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
// ListUsers returns a list with all users, filtered by an optional searchstring
|
2020-12-22 00:13:15 +01:00
|
|
|
func ListUsers(searchterm string) (users []*User, err error) {
|
2020-01-26 18:08:06 +01:00
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) > 0 {
|
2020-01-26 18:08:06 +01:00
|
|
|
err = x.
|
2020-12-22 00:13:15 +01:00
|
|
|
In("id", ids).
|
2020-01-26 18:08:06 +01:00
|
|
|
Find(&users)
|
2020-12-22 00:13:15 +01:00
|
|
|
return
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
if searchterm == "" {
|
|
|
|
err = x.Find(&users)
|
|
|
|
return
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
err = x.
|
|
|
|
Where("username LIKE ?", "%"+searchterm+"%").
|
|
|
|
Find(&users)
|
|
|
|
return
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|