mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
f1a89f5ec4
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
Former-commit-id: a44ebfc7a5687b5f51f0ff791335f66ab9f2e8e0 [formerly 9a044fadd8f2ebbb7dbb773273799c26a797f513] [formerly 7f374d016eaf756cfce215dd16a2274bbabe1915 [formerly f55f205ced
]]
Former-commit-id: 31015ddc5f4fc28c895743f6fe9dcf5488bb4b01 [formerly e439027304a1e49667fafde011e07d043ef0d2ee]
Former-commit-id: 0394c60358673b56991364260b1cbe41fa457593
115 lines
2.8 KiB
Go
115 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{
|
|
ID: uint(oldUser.ID),
|
|
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
|
|
}
|