2019-10-25 16:37:12 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2021-02-28 11:10:40 +00:00
|
|
|
"crypto/x509"
|
2019-10-25 16:37:12 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-02-02 21:20:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-25 16:37:12 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-07 21:30:18 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-10-25 16:37:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errMemoryProviderClosed = errors.New("memory provider is closed")
|
|
|
|
)
|
|
|
|
|
|
|
|
type memoryProviderHandle struct {
|
2020-07-24 21:39:38 +00:00
|
|
|
// configuration file to use for loading users
|
|
|
|
configFile string
|
|
|
|
sync.Mutex
|
2019-10-25 16:37:12 +00:00
|
|
|
isClosed bool
|
|
|
|
// slice with ordered usernames
|
|
|
|
usernames []string
|
|
|
|
// map for users, username is the key
|
|
|
|
users map[string]User
|
2021-02-01 18:04:15 +00:00
|
|
|
// map for virtual folders, folder name is the key
|
2020-06-07 21:30:18 +00:00
|
|
|
vfolders map[string]vfs.BaseVirtualFolder
|
2021-02-01 18:04:15 +00:00
|
|
|
// slice with ordered folder names
|
|
|
|
vfoldersNames []string
|
2021-01-17 21:29:08 +00:00
|
|
|
// map for admins, username is the key
|
|
|
|
admins map[string]Admin
|
|
|
|
// slice with ordered admins
|
|
|
|
adminsUsernames []string
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MemoryProvider auth provider for a memory store
|
|
|
|
type MemoryProvider struct {
|
|
|
|
dbHandle *memoryProviderHandle
|
|
|
|
}
|
|
|
|
|
2020-11-25 08:18:36 +00:00
|
|
|
func initializeMemoryProvider(basePath string) {
|
2020-04-01 21:25:23 +00:00
|
|
|
logSender = fmt.Sprintf("dataprovider_%v", MemoryDataProviderName)
|
2020-02-02 21:20:39 +00:00
|
|
|
configFile := ""
|
2020-03-03 08:09:58 +00:00
|
|
|
if utils.IsFileInputValid(config.Name) {
|
2020-02-02 21:20:39 +00:00
|
|
|
configFile = config.Name
|
|
|
|
if !filepath.IsAbs(configFile) {
|
|
|
|
configFile = filepath.Join(basePath, configFile)
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
provider = &MemoryProvider{
|
2019-10-25 16:37:12 +00:00
|
|
|
dbHandle: &memoryProviderHandle{
|
2021-01-17 21:29:08 +00:00
|
|
|
isClosed: false,
|
|
|
|
usernames: []string{},
|
|
|
|
users: make(map[string]User),
|
|
|
|
vfolders: make(map[string]vfs.BaseVirtualFolder),
|
2021-02-01 18:04:15 +00:00
|
|
|
vfoldersNames: []string{},
|
2021-01-17 21:29:08 +00:00
|
|
|
admins: make(map[string]Admin),
|
|
|
|
adminsUsernames: []string{},
|
|
|
|
configFile: configFile,
|
2019-10-25 16:37:12 +00:00
|
|
|
},
|
|
|
|
}
|
2020-11-25 08:15:23 +00:00
|
|
|
if err := provider.reloadConfig(); err != nil {
|
2020-11-25 08:18:36 +00:00
|
|
|
logger.Error(logSender, "", "unable to load initial data: %v", err)
|
2020-11-25 08:15:23 +00:00
|
|
|
logger.ErrorToConsole("unable to load initial data: %v", err)
|
|
|
|
}
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) checkAvailability() error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) close() error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
p.dbHandle.isClosed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func (p *MemoryProvider) validateUserAndTLSCert(username, protocol string, tlsCert *x509.Certificate) (User, error) {
|
|
|
|
var user User
|
|
|
|
if tlsCert == nil {
|
|
|
|
return user, errors.New("TLS certificate cannot be null or empty")
|
|
|
|
}
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error authenticating user %#v: %v", username, err)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return checkUserAndTLSCertificate(&user, protocol, tlsCert)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) validateUserAndPass(username, password, ip, protocol string) (User, error) {
|
2019-10-25 16:37:12 +00:00
|
|
|
var user User
|
2021-01-05 08:50:22 +00:00
|
|
|
if password == "" {
|
2019-10-25 16:37:12 +00:00
|
|
|
return user, errors.New("Credentials cannot be null or empty")
|
|
|
|
}
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
if err != nil {
|
2021-01-04 16:52:14 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user %#v: %v", username, err)
|
2019-10-25 16:37:12 +00:00
|
|
|
return user, err
|
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
return checkUserAndPass(&user, password, ip, protocol)
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) validateUserAndPubKey(username string, pubKey []byte) (User, string, error) {
|
2019-10-25 16:37:12 +00:00
|
|
|
var user User
|
|
|
|
if len(pubKey) == 0 {
|
|
|
|
return user, "", errors.New("Credentials cannot be null or empty")
|
|
|
|
}
|
|
|
|
user, err := p.userExists(username)
|
|
|
|
if err != nil {
|
2021-01-04 16:52:14 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user %#v: %v", username, err)
|
2019-10-25 16:37:12 +00:00
|
|
|
return user, "", err
|
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
return checkUserAndPubKey(&user, pubKey)
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) validateAdminAndPass(username, password, ip string) (Admin, error) {
|
|
|
|
admin, err := p.adminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error authenticating admin %#v: %v", username, err)
|
|
|
|
return admin, err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
err = admin.checkUserAndPass(password, ip)
|
|
|
|
return admin, err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) updateLastLogin(username string) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-11-13 10:36:21 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.LastLogin = utils.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
providerLog(logger.LevelWarn, "unable to update quota for user %#v error: %v", username, err)
|
2019-10-25 16:37:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
user.UsedQuotaSize = sizeAdd
|
|
|
|
user.UsedQuotaFiles = filesAdd
|
|
|
|
} else {
|
|
|
|
user.UsedQuotaSize += sizeAdd
|
|
|
|
user.UsedQuotaFiles += filesAdd
|
|
|
|
}
|
|
|
|
user.LastQuotaUpdate = utils.GetTimeAsMsSinceEpoch(time.Now())
|
2020-06-07 21:30:18 +00:00
|
|
|
providerLog(logger.LevelDebug, "quota updated for user %#v, files increment: %v size increment: %v is reset? %v",
|
|
|
|
username, filesAdd, sizeAdd, reset)
|
2019-10-25 16:37:12 +00:00
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) getUsedQuota(username string) (int, int64, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return 0, 0, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
providerLog(logger.LevelWarn, "unable to get quota for user %#v error: %v", username, err)
|
2019-10-25 16:37:12 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return user.UsedQuotaFiles, user.UsedQuotaSize, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) addUser(user *User) error {
|
2021-02-01 18:04:15 +00:00
|
|
|
// we can query virtual folder while validating a user
|
|
|
|
// so we have to check without holding the lock
|
|
|
|
err := ValidateUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
2019-10-25 16:37:12 +00:00
|
|
|
_, err = p.userExistsInternal(user.Username)
|
|
|
|
if err == nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
return fmt.Errorf("username %#v already exists", user.Username)
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
user.ID = p.getNextID()
|
2020-06-20 12:30:46 +00:00
|
|
|
user.LastQuotaUpdate = 0
|
|
|
|
user.UsedQuotaSize = 0
|
|
|
|
user.UsedQuotaFiles = 0
|
|
|
|
user.LastLogin = 0
|
2020-06-07 21:30:18 +00:00
|
|
|
user.VirtualFolders = p.joinVirtualFoldersFields(user)
|
2021-01-05 08:50:22 +00:00
|
|
|
p.dbHandle.users[user.Username] = user.getACopy()
|
2019-10-25 16:37:12 +00:00
|
|
|
p.dbHandle.usernames = append(p.dbHandle.usernames, user.Username)
|
|
|
|
sort.Strings(p.dbHandle.usernames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) updateUser(user *User) error {
|
2021-02-01 18:04:15 +00:00
|
|
|
// we can query virtual folder while validating a user
|
|
|
|
// so we have to check without holding the lock
|
|
|
|
err := ValidateUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
u, err := p.userExistsInternal(user.Username)
|
2019-10-25 16:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, oldFolder := range u.VirtualFolders {
|
2021-02-01 18:04:15 +00:00
|
|
|
p.removeUserFromFolderMapping(oldFolder.Name, u.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
user.VirtualFolders = p.joinVirtualFoldersFields(user)
|
|
|
|
user.LastQuotaUpdate = u.LastQuotaUpdate
|
|
|
|
user.UsedQuotaSize = u.UsedQuotaSize
|
|
|
|
user.UsedQuotaFiles = u.UsedQuotaFiles
|
|
|
|
user.LastLogin = u.LastLogin
|
2021-01-17 21:29:08 +00:00
|
|
|
user.ID = u.ID
|
2021-01-05 08:50:22 +00:00
|
|
|
// pre-login and external auth hook will use the passed *user so save a copy
|
|
|
|
p.dbHandle.users[user.Username] = user.getACopy()
|
2019-10-25 16:37:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) deleteUser(user *User) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
u, err := p.userExistsInternal(user.Username)
|
2019-10-25 16:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, oldFolder := range u.VirtualFolders {
|
2021-02-01 18:04:15 +00:00
|
|
|
p.removeUserFromFolderMapping(oldFolder.Name, u.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2019-10-25 16:37:12 +00:00
|
|
|
delete(p.dbHandle.users, user.Username)
|
|
|
|
// this could be more efficient
|
2021-01-05 08:50:22 +00:00
|
|
|
p.dbHandle.usernames = make([]string, 0, len(p.dbHandle.users))
|
2019-10-25 16:37:12 +00:00
|
|
|
for username := range p.dbHandle.users {
|
|
|
|
p.dbHandle.usernames = append(p.dbHandle.usernames, username)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.usernames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) dumpUsers() ([]User, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
users := make([]User, 0, len(p.dbHandle.usernames))
|
|
|
|
var err error
|
2019-12-27 22:12:44 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return users, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, username := range p.dbHandle.usernames {
|
2020-11-30 20:46:34 +00:00
|
|
|
u := p.dbHandle.users[username]
|
|
|
|
user := u.getACopy()
|
2020-01-31 18:04:00 +00:00
|
|
|
err = addCredentialsToUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) dumpFolders() ([]vfs.BaseVirtualFolder, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2021-02-01 18:04:15 +00:00
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, len(p.dbHandle.vfoldersNames))
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return folders, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, f := range p.dbHandle.vfolders {
|
|
|
|
folders = append(folders, f)
|
|
|
|
}
|
|
|
|
return folders, nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) getUsers(limit int, offset int, order string) ([]User, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
users := make([]User, 0, limit)
|
2019-10-25 16:37:12 +00:00
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return users, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
itNum := 0
|
2020-06-07 21:30:18 +00:00
|
|
|
if order == OrderASC {
|
2019-10-25 16:37:12 +00:00
|
|
|
for _, username := range p.dbHandle.usernames {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
u := p.dbHandle.users[username]
|
|
|
|
user := u.getACopy()
|
2020-11-22 20:53:04 +00:00
|
|
|
user.HideConfidentialData()
|
|
|
|
users = append(users, user)
|
2019-10-25 16:37:12 +00:00
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(p.dbHandle.usernames) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
username := p.dbHandle.usernames[i]
|
2020-11-30 20:46:34 +00:00
|
|
|
u := p.dbHandle.users[username]
|
|
|
|
user := u.getACopy()
|
2020-11-22 20:53:04 +00:00
|
|
|
user.HideConfidentialData()
|
|
|
|
users = append(users, user)
|
2019-10-25 16:37:12 +00:00
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) userExists(username string) (User, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2019-10-25 16:37:12 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return User{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return p.userExistsInternal(username)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) userExistsInternal(username string) (User, error) {
|
2019-10-25 16:37:12 +00:00
|
|
|
if val, ok := p.dbHandle.users[username]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
return User{}, &RecordNotFoundError{err: fmt.Sprintf("username %#v does not exist", username)}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) addAdmin(admin *Admin) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
err := admin.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = p.adminExistsInternal(admin.Username)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("admin %#v already exists", admin.Username)
|
|
|
|
}
|
|
|
|
admin.ID = p.getNextAdminID()
|
|
|
|
p.dbHandle.admins[admin.Username] = admin.getACopy()
|
|
|
|
p.dbHandle.adminsUsernames = append(p.dbHandle.adminsUsernames, admin.Username)
|
|
|
|
sort.Strings(p.dbHandle.adminsUsernames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateAdmin(admin *Admin) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
err := admin.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a, err := p.adminExistsInternal(admin.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.ID = a.ID
|
|
|
|
p.dbHandle.admins[admin.Username] = admin.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteAdmin(admin *Admin) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
_, err := p.adminExistsInternal(admin.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(p.dbHandle.admins, admin.Username)
|
|
|
|
// this could be more efficient
|
|
|
|
p.dbHandle.adminsUsernames = make([]string, 0, len(p.dbHandle.admins))
|
|
|
|
for username := range p.dbHandle.admins {
|
|
|
|
p.dbHandle.adminsUsernames = append(p.dbHandle.adminsUsernames, username)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.adminsUsernames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) adminExists(username string) (Admin, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return Admin{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return p.adminExistsInternal(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) adminExistsInternal(username string) (Admin, error) {
|
|
|
|
if val, ok := p.dbHandle.admins[username]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
|
|
|
return Admin{}, &RecordNotFoundError{err: fmt.Sprintf("admin %#v does not exist", username)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpAdmins() ([]Admin, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
admins := make([]Admin, 0, len(p.dbHandle.admins))
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return admins, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, admin := range p.dbHandle.admins {
|
|
|
|
admins = append(admins, admin)
|
|
|
|
}
|
|
|
|
return admins, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getAdmins(limit int, offset int, order string) ([]Admin, error) {
|
|
|
|
admins := make([]Admin, 0, limit)
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return admins, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return admins, nil
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for _, username := range p.dbHandle.adminsUsernames {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
a := p.dbHandle.admins[username]
|
|
|
|
admin := a.getACopy()
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
admins = append(admins, admin)
|
|
|
|
if len(admins) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(p.dbHandle.adminsUsernames) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
username := p.dbHandle.adminsUsernames[i]
|
|
|
|
a := p.dbHandle.admins[username]
|
|
|
|
admin := a.getACopy()
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
admins = append(admins, admin)
|
|
|
|
if len(admins) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return admins, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) updateFolderQuota(name string, filesAdd int, sizeAdd int64, reset bool) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := p.folderExistsInternal(name)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelWarn, "unable to update quota for folder %#v error: %v", name, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
folder.UsedQuotaSize = sizeAdd
|
|
|
|
folder.UsedQuotaFiles = filesAdd
|
|
|
|
} else {
|
|
|
|
folder.UsedQuotaSize += sizeAdd
|
|
|
|
folder.UsedQuotaFiles += filesAdd
|
|
|
|
}
|
|
|
|
folder.LastQuotaUpdate = utils.GetTimeAsMsSinceEpoch(time.Now())
|
2021-02-01 18:04:15 +00:00
|
|
|
p.dbHandle.vfolders[name] = folder
|
2020-06-07 21:30:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) getUsedFolderQuota(name string) (int, int64, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return 0, 0, errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := p.folderExistsInternal(name)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelWarn, "unable to get quota for folder %#v error: %v", name, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return folder.UsedQuotaFiles, folder.UsedQuotaSize, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) joinVirtualFoldersFields(user *User) []vfs.VirtualFolder {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for _, folder := range user.VirtualFolders {
|
2021-02-01 18:04:15 +00:00
|
|
|
f, err := p.addOrGetFolderInternal(folder.Name, folder.MappedPath, user.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
folder.UsedQuotaFiles = f.UsedQuotaFiles
|
|
|
|
folder.UsedQuotaSize = f.UsedQuotaSize
|
|
|
|
folder.LastQuotaUpdate = f.LastQuotaUpdate
|
|
|
|
folder.ID = f.ID
|
2021-02-01 18:04:15 +00:00
|
|
|
folder.MappedPath = f.MappedPath
|
2020-06-07 21:30:18 +00:00
|
|
|
folders = append(folders, folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return folders
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) removeUserFromFolderMapping(folderName, username string) {
|
|
|
|
folder, err := p.folderExistsInternal(folderName)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
var usernames []string
|
|
|
|
for _, user := range folder.Users {
|
|
|
|
if user != username {
|
|
|
|
usernames = append(usernames, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folder.Users = usernames
|
2021-02-01 18:04:15 +00:00
|
|
|
p.dbHandle.vfolders[folder.Name] = folder
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) updateFoldersMappingInternal(folder vfs.BaseVirtualFolder) {
|
2021-02-01 18:04:15 +00:00
|
|
|
p.dbHandle.vfolders[folder.Name] = folder
|
|
|
|
if !utils.IsStringInSlice(folder.Name, p.dbHandle.vfoldersNames) {
|
|
|
|
p.dbHandle.vfoldersNames = append(p.dbHandle.vfoldersNames, folder.Name)
|
|
|
|
sort.Strings(p.dbHandle.vfoldersNames)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) addOrGetFolderInternal(folderName, folderMappedPath, username string) (vfs.BaseVirtualFolder, error) {
|
|
|
|
folder, err := p.folderExistsInternal(folderName)
|
2020-06-07 21:30:18 +00:00
|
|
|
if _, ok := err.(*RecordNotFoundError); ok {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
ID: p.getNextFolderID(),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
|
|
|
MappedPath: folderMappedPath,
|
|
|
|
UsedQuotaSize: 0,
|
|
|
|
UsedQuotaFiles: 0,
|
|
|
|
LastQuotaUpdate: 0,
|
2020-06-07 21:30:18 +00:00
|
|
|
Users: []string{username},
|
|
|
|
}
|
|
|
|
p.updateFoldersMappingInternal(folder)
|
|
|
|
return folder, nil
|
|
|
|
}
|
|
|
|
if err == nil && !utils.IsStringInSlice(username, folder.Users) {
|
|
|
|
folder.Users = append(folder.Users, username)
|
|
|
|
p.updateFoldersMappingInternal(folder)
|
|
|
|
}
|
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) folderExistsInternal(name string) (vfs.BaseVirtualFolder, error) {
|
|
|
|
if val, ok := p.dbHandle.vfolders[name]; ok {
|
2020-06-07 21:30:18 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
return vfs.BaseVirtualFolder{}, &RecordNotFoundError{err: fmt.Sprintf("folder %#v does not exist", name)}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) getFolders(limit, offset int, order string) ([]vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, limit)
|
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return folders, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
2021-02-01 18:04:15 +00:00
|
|
|
for _, name := range p.dbHandle.vfoldersNames {
|
2020-06-07 21:30:18 +00:00
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder := p.dbHandle.vfolders[name]
|
2020-06-07 21:30:18 +00:00
|
|
|
folders = append(folders, folder)
|
|
|
|
if len(folders) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-01 18:04:15 +00:00
|
|
|
for i := len(p.dbHandle.vfoldersNames) - 1; i >= 0; i-- {
|
2020-06-07 21:30:18 +00:00
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
name := p.dbHandle.vfoldersNames[i]
|
|
|
|
folder := p.dbHandle.vfolders[name]
|
2020-06-07 21:30:18 +00:00
|
|
|
folders = append(folders, folder)
|
|
|
|
if len(folders) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *MemoryProvider) getFolderByName(name string) (vfs.BaseVirtualFolder, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return vfs.BaseVirtualFolder{}, errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
return p.folderExistsInternal(name)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) addFolder(folder *vfs.BaseVirtualFolder) error {
|
2021-02-04 18:09:43 +00:00
|
|
|
err := ValidateFolder(folder)
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
_, err = p.folderExistsInternal(folder.Name)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("folder %#v already exists", folder.Name)
|
|
|
|
}
|
|
|
|
folder.ID = p.getNextFolderID()
|
|
|
|
folder.Users = nil
|
|
|
|
p.dbHandle.vfolders[folder.Name] = folder.GetACopy()
|
|
|
|
p.dbHandle.vfoldersNames = append(p.dbHandle.vfoldersNames, folder.Name)
|
|
|
|
sort.Strings(p.dbHandle.vfoldersNames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateFolder(folder *vfs.BaseVirtualFolder) error {
|
2021-02-04 18:09:43 +00:00
|
|
|
err := ValidateFolder(folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
f, err := p.folderExistsInternal(folder.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
folder.ID = f.ID
|
|
|
|
folder.LastQuotaUpdate = f.LastQuotaUpdate
|
|
|
|
folder.UsedQuotaFiles = f.UsedQuotaFiles
|
|
|
|
folder.UsedQuotaSize = f.UsedQuotaSize
|
|
|
|
folder.Users = f.Users
|
|
|
|
p.dbHandle.vfolders[folder.Name] = folder.GetACopy()
|
2020-06-07 21:30:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) deleteFolder(folder *vfs.BaseVirtualFolder) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-06-07 21:30:18 +00:00
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err := p.folderExistsInternal(folder.Name)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, username := range folder.Users {
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err == nil {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for _, userFolder := range user.VirtualFolders {
|
2021-02-01 18:04:15 +00:00
|
|
|
if folder.Name != userFolder.Name {
|
2020-06-07 21:30:18 +00:00
|
|
|
folders = append(folders, userFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
delete(p.dbHandle.vfolders, folder.Name)
|
|
|
|
p.dbHandle.vfoldersNames = []string{}
|
|
|
|
for name := range p.dbHandle.vfolders {
|
|
|
|
p.dbHandle.vfoldersNames = append(p.dbHandle.vfoldersNames, name)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
sort.Strings(p.dbHandle.vfoldersNames)
|
2020-06-07 21:30:18 +00:00
|
|
|
return nil
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) getNextID() int64 {
|
2019-10-25 16:37:12 +00:00
|
|
|
nextID := int64(1)
|
2021-01-17 21:29:08 +00:00
|
|
|
for _, v := range p.dbHandle.users {
|
|
|
|
if v.ID >= nextID {
|
|
|
|
nextID = v.ID + 1
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
2020-02-02 21:20:39 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) getNextFolderID() int64 {
|
2020-06-07 21:30:18 +00:00
|
|
|
nextID := int64(1)
|
|
|
|
for _, v := range p.dbHandle.vfolders {
|
|
|
|
if v.ID >= nextID {
|
|
|
|
nextID = v.ID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) getNextAdminID() int64 {
|
|
|
|
nextID := int64(1)
|
|
|
|
for _, a := range p.dbHandle.admins {
|
|
|
|
if a.ID >= nextID {
|
|
|
|
nextID = a.ID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) clear() {
|
2020-07-24 21:39:38 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
2020-02-02 21:20:39 +00:00
|
|
|
p.dbHandle.usernames = []string{}
|
|
|
|
p.dbHandle.users = make(map[string]User)
|
2021-02-01 18:04:15 +00:00
|
|
|
p.dbHandle.vfoldersNames = []string{}
|
2020-06-07 21:30:18 +00:00
|
|
|
p.dbHandle.vfolders = make(map[string]vfs.BaseVirtualFolder)
|
2021-01-17 21:29:08 +00:00
|
|
|
p.dbHandle.admins = make(map[string]Admin)
|
|
|
|
p.dbHandle.adminsUsernames = []string{}
|
2020-02-02 21:20:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) reloadConfig() error {
|
2020-11-25 08:18:36 +00:00
|
|
|
if p.dbHandle.configFile == "" {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelDebug, "no dump configuration file defined")
|
2020-02-02 21:20:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelDebug, "loading dump from file: %#v", p.dbHandle.configFile)
|
2020-02-02 21:20:39 +00:00
|
|
|
fi, err := os.Stat(p.dbHandle.configFile)
|
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelWarn, "error loading dump: %v", err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() == 0 {
|
2021-02-01 18:04:15 +00:00
|
|
|
err = errors.New("dump configuration file is invalid, its size must be > 0")
|
|
|
|
providerLog(logger.LevelWarn, "error loading dump: %v", err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() > 10485760 {
|
2021-02-01 18:04:15 +00:00
|
|
|
err = errors.New("dump configuration file is invalid, its size must be <= 10485760 bytes")
|
|
|
|
providerLog(logger.LevelWarn, "error loading dump: %v", err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
content, err := os.ReadFile(p.dbHandle.configFile)
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelWarn, "error loading dump: %v", err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
dump, err := ParseDumpData(content)
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelWarn, "error loading dump: %v", err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
p.clear()
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
if err := p.restoreFolders(&dump); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.restoreUsers(&dump); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.restoreAdmins(&dump); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
providerLog(logger.LevelDebug, "config loaded from file: %#v", p.dbHandle.configFile)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) restoreAdmins(dump *BackupData) error {
|
|
|
|
for _, admin := range dump.Admins {
|
|
|
|
a, err := p.adminExists(admin.Username)
|
|
|
|
admin := admin // pin
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
admin.ID = a.ID
|
|
|
|
err = p.updateAdmin(&admin)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error updating admin %#v: %v", admin.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = p.addAdmin(&admin)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error adding admin %#v: %v", admin.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) restoreFolders(dump *BackupData) error {
|
|
|
|
for _, folder := range dump.Folders {
|
2021-01-05 08:50:22 +00:00
|
|
|
folder := folder // pin
|
2021-02-01 18:04:15 +00:00
|
|
|
f, err := p.getFolderByName(folder.Name)
|
|
|
|
if err == nil {
|
|
|
|
folder.ID = f.ID
|
|
|
|
err = p.updateFolder(&folder)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error updating folder %#v: %v", folder.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
folder.Users = nil
|
|
|
|
err = p.addFolder(&folder)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error adding folder %#v: %v", folder.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) restoreUsers(dump *BackupData) error {
|
2020-02-02 21:20:39 +00:00
|
|
|
for _, user := range dump.Users {
|
2021-01-05 08:50:22 +00:00
|
|
|
user := user // pin
|
2021-02-01 18:04:15 +00:00
|
|
|
u, err := p.userExists(user.Username)
|
2020-02-02 21:20:39 +00:00
|
|
|
if err == nil {
|
|
|
|
user.ID = u.ID
|
2021-01-05 08:50:22 +00:00
|
|
|
err = p.updateUser(&user)
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error updating user %#v: %v", user.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-05 08:50:22 +00:00
|
|
|
err = p.addUser(&user)
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error adding user %#v: %v", user.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
|
|
|
|
// initializeDatabase does nothing, no initilization is needed for memory provider
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) initializeDatabase() error {
|
2020-08-30 11:50:43 +00:00
|
|
|
return ErrNoInitRequired
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) migrateDatabase() error {
|
2020-10-05 17:42:33 +00:00
|
|
|
return ErrNoInitRequired
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
2020-11-26 21:08:33 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *MemoryProvider) revertDatabase(targetVersion int) error {
|
2020-11-26 21:08:33 +00:00
|
|
|
return errors.New("memory provider does not store data, revert not possible")
|
|
|
|
}
|