2019-01-05 22:44:33 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2022-07-05 14:55:31 +00:00
|
|
|
"fmt"
|
2019-01-05 22:44:33 +00:00
|
|
|
"reflect"
|
|
|
|
|
2022-05-03 20:48:45 +00:00
|
|
|
"github.com/asdine/storm/v3"
|
2020-05-31 23:12:36 +00:00
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usersBackend struct {
|
|
|
|
db *storm.DB
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:03:52 +00:00
|
|
|
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:
|
|
|
|
return nil, errors.ErrInvalidDataType
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 02:03:52 +00:00
|
|
|
err = st.db.One(arg, i, user)
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-08 02:03:52 +00:00
|
|
|
if err == storm.ErrNotFound {
|
|
|
|
return nil, errors.ErrNotExist
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:03:52 +00:00
|
|
|
return
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st usersBackend) Gets() ([]*users.User, error) {
|
2020-05-31 23:12:36 +00:00
|
|
|
var allUsers []*users.User
|
|
|
|
err := st.db.All(&allUsers)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err == storm.ErrNotFound {
|
|
|
|
return nil, errors.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-31 23:12:36 +00:00
|
|
|
return allUsers, err
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
return allUsers, err
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st usersBackend) Update(user *users.User, fields ...string) error {
|
|
|
|
if len(fields) == 0 {
|
|
|
|
return st.Save(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range fields {
|
2022-07-05 14:55:31 +00:00
|
|
|
userField := reflect.ValueOf(user).Elem().FieldByName(field)
|
|
|
|
if !userField.IsValid() {
|
|
|
|
return fmt.Errorf("invalid field: %s", field)
|
|
|
|
}
|
|
|
|
val := userField.Interface()
|
2019-01-05 22:44:33 +00:00
|
|
|
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)
|
|
|
|
if err == storm.ErrAlreadyExists {
|
|
|
|
return errors.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 {
|
2019-01-08 02:03:52 +00:00
|
|
|
user, err := st.GetBy(username)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.db.DeleteStruct(user)
|
|
|
|
}
|