filebrowser/storage/bolt/importer/users.go
Henrique Dias 367e251a0e fixes: correct users importing id (#645)
@princemaple could you try out this branch to see if it works on your end?

Former-commit-id: 12c782316c411fa3438f167c12634adffb5e1adf [formerly c613ff6624c55292f532d7772e866cf6c81241f5] [formerly 11f03e54dae28fa773227d85259cfd73c5750137 [formerly 9c42269b62]]
Former-commit-id: 04bcf5803397877d00ba48ea6d0b00246dc07b3d [formerly 4c1bb1e95375777aede2c0911bdfa9ce46273b37]
Former-commit-id: 601db641f7d7b01c14903d0cc27085e3d7080c3d
2019-01-26 11:37:33 +00:00

114 lines
2.8 KiB
Go

package importer
import (
"encoding/json"
"fmt"
"github.com/asdine/storm"
"github.com/filebrowser/filebrowser/v2/rules"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users"
"go.etcd.io/bbolt"
)
type oldUser struct {
ID int `storm:"id,increment"`
Admin bool `json:"admin"`
AllowCommands bool `json:"allowCommands"` // Execute commands
AllowEdit bool `json:"allowEdit"` // Edit/rename files
AllowNew bool `json:"allowNew"` // Create files and folders
AllowPublish bool `json:"allowPublish"` // Publish content (to use with static gen)
LockPassword bool `json:"lockPassword"`
Commands []string `json:"commands"`
Locale string `json:"locale"`
Password string `json:"password"`
Rules []*rules.Rule `json:"rules"`
Scope string `json:"filesystem"`
Username string `json:"username" storm:"index,unique"`
ViewMode string `json:"viewMode"`
}
func readOldUsers(db *storm.DB) ([]*oldUser, error) {
users := []*oldUser{}
err := db.Bolt.View(func(tx *bolt.Tx) error {
return tx.Bucket([]byte("User")).ForEach(func(k []byte, v []byte) error {
if len(v) > 0 && string(v)[0] == '{' {
user := &oldUser{}
err := json.Unmarshal(v, user)
if err != nil {
return err
}
users = append(users, user)
}
return nil
})
})
return users, err
}
func convertUsersToNew(old []*oldUser) ([]*users.User, error) {
list := []*users.User{}
for _, oldUser := range old {
user := &users.User{
Username: oldUser.Username,
Password: oldUser.Password,
Scope: oldUser.Scope,
Locale: oldUser.Locale,
LockPassword: oldUser.LockPassword,
ViewMode: users.ViewMode(oldUser.ViewMode),
Commands: oldUser.Commands,
Rules: []rules.Rule{},
Perm: users.Permissions{
Admin: oldUser.Admin,
Execute: oldUser.AllowCommands,
Create: oldUser.AllowNew,
Rename: oldUser.AllowEdit,
Modify: oldUser.AllowEdit,
Delete: oldUser.AllowEdit,
Share: true,
Download: true,
},
}
for _, rule := range oldUser.Rules {
user.Rules = append(user.Rules, *rule)
}
err := user.Clean("")
if err != nil {
return nil, err
}
list = append(list, user)
}
return list, nil
}
func importUsers(old *storm.DB, sto *storage.Storage) error {
oldUsers, err := readOldUsers(old)
if err != nil {
return err
}
newUsers, err := convertUsersToNew(oldUsers)
if err != nil {
return err
}
for _, user := range newUsers {
err = sto.Users.Save(user)
if err != nil {
return err
}
}
fmt.Printf("%d users successfully imported into the new DB.\n", len(newUsers))
return nil
}