mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
12b2c21522
Read https://github.com/filebrowser/filebrowser/pull/575.
Former-commit-id: 7aedcaaf72b863033e3f089d6df308d41a3fd00c [formerly bdbe4d49161b901c4adf9c245895a1be2d62e4a7] [formerly acfc1ec67c423e0b3e065a8c1f8897c5249af65b [formerly d309066def
]]
Former-commit-id: 0c7d925a38a68ccabdf2c4bbd8c302ee89b93509 [formerly a6173925a1382955d93b334ded93f70d6dddd694]
Former-commit-id: e032e0804dd051df86f42962de2b39caec5318b7
92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package bolt
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/asdine/storm"
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
)
|
|
|
|
type usersBackend struct {
|
|
db *storm.DB
|
|
}
|
|
|
|
func (st usersBackend) GetByID(id uint) (*users.User, error) {
|
|
user := &users.User{}
|
|
err := st.db.One("ID", id, user)
|
|
if err == storm.ErrNotFound {
|
|
return nil, errors.ErrNotExist
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (st usersBackend) GetByUsername(username string) (*users.User, error) {
|
|
user := &users.User{}
|
|
err := st.db.One("Username", username, user)
|
|
if err == storm.ErrNotFound {
|
|
return nil, errors.ErrNotExist
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (st usersBackend) Gets() ([]*users.User, error) {
|
|
users := []*users.User{}
|
|
err := st.db.All(&users)
|
|
if err == storm.ErrNotFound {
|
|
return nil, errors.ErrNotExist
|
|
}
|
|
|
|
if err != nil {
|
|
return users, err
|
|
}
|
|
|
|
return users, err
|
|
}
|
|
|
|
func (st usersBackend) Update(user *users.User, fields ...string) error {
|
|
if len(fields) == 0 {
|
|
return st.Save(user)
|
|
}
|
|
|
|
for _, field := range fields {
|
|
val := reflect.ValueOf(user).Elem().FieldByName(field).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)
|
|
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 {
|
|
user, err := st.GetByUsername(username)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return st.db.DeleteStruct(user)
|
|
}
|