2021-08-18 20:18:39 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2022-10-03 22:59:29 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-08-18 20:18:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-10-03 22:59:29 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2021-08-18 20:18:39 +02:00
|
|
|
)
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
// RegisteredUsers finds all registered users.
|
|
|
|
func RegisteredUsers() (result entity.Users) {
|
|
|
|
if err := Db().Where("id > 0").Find(&result).Error; err != nil {
|
|
|
|
log.Errorf("users: %s", err)
|
2021-08-18 20:18:39 +02:00
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
|
|
|
return result
|
2021-08-18 20:18:39 +02:00
|
|
|
}
|
2022-10-03 22:59:29 +02:00
|
|
|
|
|
|
|
// Users finds users and returns them.
|
|
|
|
func Users(limit, offset int, sortOrder, search string) (result entity.Users, err error) {
|
|
|
|
result = entity.Users{}
|
|
|
|
stmt := Db()
|
|
|
|
|
|
|
|
search = strings.TrimSpace(search)
|
|
|
|
|
|
|
|
if search == "all" {
|
|
|
|
stmt = stmt.Where("sess_expires > 0 AND sess_expires < ?", entity.UnixTime())
|
|
|
|
} else if id := txt.Int(search); id != 0 {
|
|
|
|
stmt = stmt.Where("id = ?", id)
|
|
|
|
} else if rnd.IsUID(search, entity.UserUID) {
|
|
|
|
stmt = stmt.Where("user_uid = ?", search)
|
|
|
|
} else if search != "" {
|
|
|
|
stmt = stmt.Where("user_name LIKE ? OR user_email LIKE ? OR display_name LIKE ?", search+"%", search+"%", search+"%")
|
|
|
|
} else {
|
|
|
|
stmt = stmt.Where("id > 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sortOrder == "" {
|
|
|
|
sortOrder = "id"
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
stmt = stmt.Limit(limit)
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
stmt = stmt.Offset(offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stmt.Order(sortOrder).Find(&result).Error
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|