mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
ce68f48fb4
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
Former-commit-id: f46856bd84680104e797c78ae1c5b24a4bf4abb2 [formerly 5e687a969645645c17e69267f5410d5dd25b001d] [formerly 0ad00c12d715b805d51b7ffaa17283525e5e21f1 [formerly fbcf1ea995
]]
Former-commit-id: eaf1b6aa326396828785065f53498e02a2f95b78 [formerly 30a76c7b8bd306bb92312aa7071cc3829a2171e4]
Former-commit-id: b60dcd6ac13649e759affd60b0e8346b243ef072
114 lines
2.8 KiB
Go
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"
|
|
bolt "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
|
|
}
|