filebrowser/storage/bolt/users.go

96 lines
1.7 KiB
Go
Raw Normal View History

package bolt
import (
2024-04-02 00:24:06 +08:00
"errors"
"fmt"
"reflect"
"github.com/asdine/storm/v3"
2020-06-01 07:12:36 +08:00
2024-04-02 00:24:06 +08:00
fbErrors "github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/users"
)
type usersBackend struct {
db *storm.DB
}
func (st usersBackend) GetBy(i interface{}) (user *users.User, err error) {
user = &users.User{}
var arg string
switch i.(type) {
case uint:
arg = "ID"
case string:
arg = "Username"
default:
2024-04-02 00:24:06 +08:00
return nil, fbErrors.ErrInvalidDataType
}
err = st.db.One(arg, i, user)
if err != nil {
2024-04-02 00:24:06 +08:00
if errors.Is(err, storm.ErrNotFound) {
return nil, fbErrors.ErrNotExist
}
return nil, err
}
return
}
func (st usersBackend) Gets() ([]*users.User, error) {
2020-06-01 07:12:36 +08:00
var allUsers []*users.User
err := st.db.All(&allUsers)
2024-04-02 00:24:06 +08:00
if errors.Is(err, storm.ErrNotFound) {
return nil, fbErrors.ErrNotExist
}
if err != nil {
2020-06-01 07:12:36 +08:00
return allUsers, err
}
2020-06-01 07:12:36 +08:00
return allUsers, err
}
func (st usersBackend) Update(user *users.User, fields ...string) error {
if len(fields) == 0 {
return st.Save(user)
}
for _, field := range fields {
userField := reflect.ValueOf(user).Elem().FieldByName(field)
if !userField.IsValid() {
return fmt.Errorf("invalid field: %s", field)
}
val := userField.Interface()
if err := st.db.UpdateField(user, field, val); err != nil {
return err
}
}
return nil
}
func (st usersBackend) Save(user *users.User) error {
err := st.db.Save(user)
2024-04-02 00:24:06 +08:00
if errors.Is(err, storm.ErrAlreadyExists) {
return fbErrors.ErrExist
}
return err
}
func (st usersBackend) DeleteByID(id uint) error {
return st.db.DeleteStruct(&users.User{ID: id})
}
func (st usersBackend) DeleteByUsername(username string) error {
user, err := st.GetBy(username)
if err != nil {
return err
}
return st.db.DeleteStruct(user)
}