2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
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"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/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
|
2022-04-25 13:49:11 +00:00
|
|
|
// slice with ordered group names
|
|
|
|
groupnames []string
|
|
|
|
// map for group, group name is the key
|
|
|
|
groups map[string]Group
|
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
|
2021-08-17 16:08:32 +00:00
|
|
|
// map for API keys, keyID is the key
|
|
|
|
apiKeys map[string]APIKey
|
|
|
|
// slice with ordered API keys KeyID
|
|
|
|
apiKeysIDs []string
|
2021-11-06 13:13:20 +00:00
|
|
|
// map for shares, shareID is the key
|
|
|
|
shares map[string]Share
|
|
|
|
// slice with ordered shares shareID
|
|
|
|
sharesIDs []string
|
2022-07-11 06:17:36 +00:00
|
|
|
// map for event actions, name is the key
|
|
|
|
actions map[string]BaseEventAction
|
|
|
|
// slice with ordered actions
|
|
|
|
actionsNames []string
|
|
|
|
// map for event actions, name is the key
|
|
|
|
rules map[string]EventRule
|
|
|
|
// slice with ordered rules
|
|
|
|
rulesNames []string
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 11:25:53 +00:00
|
|
|
// MemoryProvider defines the auth provider for a memory store
|
2019-10-25 16:37:12 +00:00
|
|
|
type MemoryProvider struct {
|
|
|
|
dbHandle *memoryProviderHandle
|
|
|
|
}
|
|
|
|
|
2020-11-25 08:18:36 +00:00
|
|
|
func initializeMemoryProvider(basePath string) {
|
2020-02-02 21:20:39 +00:00
|
|
|
configFile := ""
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.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),
|
2022-04-25 13:49:11 +00:00
|
|
|
groupnames: []string{},
|
|
|
|
groups: make(map[string]Group),
|
2021-01-17 21:29:08 +00:00
|
|
|
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{},
|
2021-08-17 16:08:32 +00:00
|
|
|
apiKeys: make(map[string]APIKey),
|
|
|
|
apiKeysIDs: []string{},
|
2021-11-06 13:13:20 +00:00
|
|
|
shares: make(map[string]Share),
|
|
|
|
sharesIDs: []string{},
|
2022-07-11 06:17:36 +00:00
|
|
|
actions: make(map[string]BaseEventAction),
|
|
|
|
actionsNames: []string{},
|
|
|
|
rules: make(map[string]EventRule),
|
|
|
|
rulesNames: []string{},
|
2021-01-17 21:29:08 +00:00
|
|
|
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)
|
2021-05-06 19:35:43 +00:00
|
|
|
return user, err
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
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)
|
2021-05-06 19:35:43 +00:00
|
|
|
return user, err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
return checkUserAndPass(&user, password, ip, protocol)
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 16:16:50 +00:00
|
|
|
func (p *MemoryProvider) validateUserAndPubKey(username string, pubKey []byte, isSSHCert bool) (User, string, error) {
|
2019-10-25 16:37:12 +00:00
|
|
|
var user User
|
|
|
|
if len(pubKey) == 0 {
|
2021-03-21 18:15:47 +00:00
|
|
|
return user, "", errors.New("credentials cannot be null or empty")
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
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)
|
2021-05-06 19:35:43 +00:00
|
|
|
return user, "", err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
2022-03-31 16:16:50 +00:00
|
|
|
return checkUserAndPubKey(&user, pubKey, isSSHCert)
|
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)
|
2021-04-26 17:48:21 +00:00
|
|
|
return admin, ErrInvalidCredentials
|
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-08-17 16:08:32 +00:00
|
|
|
func (p *MemoryProvider) updateAPIKeyLastUse(keyID string) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
apiKey, err := p.apiKeyExistsInternal(keyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKey.LastUseAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.apiKeys[apiKey.KeyID] = apiKey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
func (p *MemoryProvider) setUpdatedAt(username string) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.users[user.Username] = user
|
2022-04-30 09:59:36 +00:00
|
|
|
setLastUserUpdate()
|
2021-08-19 13:51:43 +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
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
|
2019-11-13 10:36:21 +00:00
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
func (p *MemoryProvider) updateAdminLastLogin(username string) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
admin, err := p.adminExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.admins[admin.Username] = admin
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *MemoryProvider) updateTransferQuota(username string, uploadSize, downloadSize int64, reset bool) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to update transfer quota for user %#v error: %v", username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
user.UsedUploadDataTransfer = uploadSize
|
|
|
|
user.UsedDownloadDataTransfer = downloadSize
|
|
|
|
} else {
|
|
|
|
user.UsedUploadDataTransfer += uploadSize
|
|
|
|
user.UsedDownloadDataTransfer += downloadSize
|
|
|
|
}
|
|
|
|
user.LastQuotaUpdate = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
providerLog(logger.LevelDebug, "transfer quota updated for user %#v, ul increment: %v dl increment: %v is reset? %v",
|
|
|
|
username, uploadSize, downloadSize, reset)
|
|
|
|
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 {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.LastQuotaUpdate = util.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
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *MemoryProvider) getUsedQuota(username string) (int, int64, int64, 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 {
|
2022-01-30 10:42:36 +00:00
|
|
|
return 0, 0, 0, 0, errMemoryProviderClosed
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "unable to get quota for user %#v error: %v", username, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
return 0, 0, 0, 0, err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
return user.UsedQuotaFiles, user.UsedQuotaSize, user.UsedUploadDataTransfer, user.UsedDownloadDataTransfer, err
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-01-30 10:42:36 +00:00
|
|
|
user.UsedUploadDataTransfer = 0
|
|
|
|
user.UsedDownloadDataTransfer = 0
|
2020-06-20 12:30:46 +00:00
|
|
|
user.LastLogin = 0
|
2022-08-21 17:01:08 +00:00
|
|
|
user.FirstUpload = 0
|
|
|
|
user.FirstDownload = 0
|
2021-08-19 13:51:43 +00:00
|
|
|
user.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2022-04-25 13:49:11 +00:00
|
|
|
user.VirtualFolders = p.joinUserVirtualFoldersFields(user)
|
|
|
|
for idx := range user.Groups {
|
|
|
|
if err = p.addUserFromGroupMapping(user.Username, user.Groups[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-04-25 13:49:11 +00:00
|
|
|
p.removeRelationFromFolderMapping(oldFolder.Name, u.Username, "")
|
|
|
|
}
|
|
|
|
for idx := range u.Groups {
|
|
|
|
if err = p.removeUserFromGroupMapping(u.Username, u.Groups[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.VirtualFolders = p.joinUserVirtualFoldersFields(user)
|
|
|
|
for idx := range user.Groups {
|
|
|
|
if err = p.addUserFromGroupMapping(user.Username, user.Groups[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
user.LastQuotaUpdate = u.LastQuotaUpdate
|
|
|
|
user.UsedQuotaSize = u.UsedQuotaSize
|
|
|
|
user.UsedQuotaFiles = u.UsedQuotaFiles
|
2022-01-30 10:42:36 +00:00
|
|
|
user.UsedUploadDataTransfer = u.UsedUploadDataTransfer
|
|
|
|
user.UsedDownloadDataTransfer = u.UsedDownloadDataTransfer
|
2020-06-07 21:30:18 +00:00
|
|
|
user.LastLogin = u.LastLogin
|
2022-08-21 17:01:08 +00:00
|
|
|
user.FirstDownload = u.FirstDownload
|
|
|
|
user.FirstUpload = u.FirstUpload
|
2021-08-19 13:51:43 +00:00
|
|
|
user.CreatedAt = u.CreatedAt
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
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()
|
2022-04-30 09:59:36 +00:00
|
|
|
setLastUserUpdate()
|
2019-10-25 16:37:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-17 16:48:41 +00:00
|
|
|
func (p *MemoryProvider) deleteUser(user User, softDelete 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
|
|
|
|
}
|
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 {
|
2022-04-25 13:49:11 +00:00
|
|
|
p.removeRelationFromFolderMapping(oldFolder.Name, u.Username, "")
|
|
|
|
}
|
|
|
|
for idx := range u.Groups {
|
|
|
|
if err = p.removeUserFromGroupMapping(u.Username, u.Groups[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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)
|
2021-08-17 16:08:32 +00:00
|
|
|
p.deleteAPIKeysWithUser(user.Username)
|
2021-11-06 13:13:20 +00:00
|
|
|
p.deleteSharesWithUser(user.Username)
|
2019-10-25 16:37:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-02 20:20:21 +00:00
|
|
|
func (p *MemoryProvider) updateUserPassword(username, password string) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Password = password
|
|
|
|
p.dbHandle.users[username] = user
|
|
|
|
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()
|
2022-01-20 17:19:20 +00:00
|
|
|
p.addVirtualFoldersToUser(&user)
|
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-08-20 07:35:06 +00:00
|
|
|
func (p *MemoryProvider) getRecentlyUpdatedUsers(after int64) ([]User, error) {
|
2022-04-30 09:59:36 +00:00
|
|
|
if getLastUserUpdate() < after {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
users := make([]User, 0, 10)
|
|
|
|
for _, username := range p.dbHandle.usernames {
|
|
|
|
u := p.dbHandle.users[username]
|
|
|
|
if u.UpdatedAt < after {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user := u.getACopy()
|
|
|
|
p.addVirtualFoldersToUser(&user)
|
|
|
|
if len(user.Groups) > 0 {
|
|
|
|
groupMapping := make(map[string]Group)
|
|
|
|
for idx := range user.Groups {
|
|
|
|
group, err := p.groupExistsInternal(user.Groups[idx].Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupMapping[group.Name] = group
|
|
|
|
}
|
|
|
|
user.applyGroupSettings(groupMapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.SetEmptySecretsIfNil()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
2021-08-20 07:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (p *MemoryProvider) getUsersForQuotaCheck(toFetch map[string]bool) ([]User, error) {
|
|
|
|
users := make([]User, 0, 30)
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return users, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, username := range p.dbHandle.usernames {
|
2022-04-30 09:59:36 +00:00
|
|
|
if needFolders, ok := toFetch[username]; ok {
|
2022-01-20 17:19:20 +00:00
|
|
|
u := p.dbHandle.users[username]
|
|
|
|
user := u.getACopy()
|
2022-04-30 09:59:36 +00:00
|
|
|
if needFolders {
|
|
|
|
p.addVirtualFoldersToUser(&user)
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if len(user.Groups) > 0 {
|
|
|
|
groupMapping := make(map[string]Group)
|
|
|
|
for idx := range user.Groups {
|
|
|
|
group, err := p.groupExistsInternal(user.Groups[idx].Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupMapping[group.Name] = group
|
|
|
|
}
|
|
|
|
user.applyGroupSettings(groupMapping)
|
|
|
|
}
|
|
|
|
user.SetEmptySecretsIfNil()
|
2022-01-20 17:19:20 +00:00
|
|
|
user.PrepareForRendering()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, 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()
|
2022-01-20 17:19:20 +00:00
|
|
|
p.addVirtualFoldersToUser(&user)
|
2021-03-22 18:03:25 +00:00
|
|
|
user.PrepareForRendering()
|
2020-11-22 20:53:04 +00:00
|
|
|
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()
|
2022-01-20 17:19:20 +00:00
|
|
|
p.addVirtualFoldersToUser(&user)
|
2021-03-22 18:03:25 +00:00
|
|
|
user.PrepareForRendering()
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
}
|
2022-01-20 17:19:20 +00:00
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
p.addVirtualFoldersToUser(&user)
|
|
|
|
return user, nil
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
return User{}, util.NewRecordNotFoundError(fmt.Sprintf("username %q does not exist", username))
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) groupExistsInternal(name string) (Group, error) {
|
|
|
|
if val, ok := p.dbHandle.groups[name]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
return Group{}, util.NewRecordNotFoundError(fmt.Sprintf("group %q does not exist", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) actionExistsInternal(name string) (BaseEventAction, error) {
|
|
|
|
if val, ok := p.dbHandle.actions[name]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
|
|
|
return BaseEventAction{}, util.NewRecordNotFoundError(fmt.Sprintf("event action %q does not exist", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) ruleExistsInternal(name string) (EventRule, error) {
|
|
|
|
if val, ok := p.dbHandle.rules[name]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
|
|
|
return EventRule{}, util.NewRecordNotFoundError(fmt.Sprintf("event rule %q does not exist", name))
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-08-19 13:51:43 +00:00
|
|
|
admin.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
admin.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
admin.LastLogin = 0
|
2021-01-17 21:29:08 +00:00
|
|
|
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
|
2021-08-19 13:51:43 +00:00
|
|
|
admin.CreatedAt = a.CreatedAt
|
|
|
|
admin.LastLogin = a.LastLogin
|
|
|
|
admin.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-01-17 21:29:08 +00:00
|
|
|
p.dbHandle.admins[admin.Username] = admin.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) deleteAdmin(admin Admin) error {
|
2021-01-17 21:29:08 +00:00
|
|
|
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)
|
2021-08-17 16:08:32 +00:00
|
|
|
p.deleteAPIKeysWithAdmin(admin.Username)
|
2021-01-17 21:29:08 +00:00
|
|
|
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
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return Admin{}, util.NewRecordNotFoundError(fmt.Sprintf("admin %#v does not exist", username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
folder.LastQuotaUpdate = util.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
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) getGroups(limit, offset int, order string, minimal bool) ([]Group, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
groups := make([]Group, 0, limit)
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for _, name := range p.dbHandle.groupnames {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g := p.dbHandle.groups[name]
|
|
|
|
group := g.getACopy()
|
|
|
|
p.addVirtualFoldersToGroup(&group)
|
|
|
|
group.PrepareForRendering()
|
|
|
|
groups = append(groups, group)
|
|
|
|
if len(groups) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(p.dbHandle.groupnames) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := p.dbHandle.groupnames[i]
|
|
|
|
g := p.dbHandle.groups[name]
|
|
|
|
group := g.getACopy()
|
|
|
|
p.addVirtualFoldersToGroup(&group)
|
|
|
|
group.PrepareForRendering()
|
|
|
|
groups = append(groups, group)
|
|
|
|
if len(groups) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getGroupsWithNames(names []string) ([]Group, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
groups := make([]Group, 0, len(names))
|
|
|
|
for _, name := range names {
|
|
|
|
if val, ok := p.dbHandle.groups[name]; ok {
|
|
|
|
group := val.getACopy()
|
|
|
|
p.addVirtualFoldersToGroup(&group)
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getUsersInGroups(names []string) ([]string, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
var users []string
|
|
|
|
for _, name := range names {
|
|
|
|
if val, ok := p.dbHandle.groups[name]; ok {
|
|
|
|
group := val.getACopy()
|
|
|
|
users = append(users, group.Users...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) groupExists(name string) (Group, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return Group{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
group, err := p.groupExistsInternal(name)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
p.addVirtualFoldersToGroup(&group)
|
|
|
|
return group, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addGroup(group *Group) error {
|
|
|
|
if err := group.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := p.groupExistsInternal(group.Name)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("group %#v already exists", group.Name)
|
|
|
|
}
|
|
|
|
group.ID = p.getNextGroupID()
|
|
|
|
group.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
group.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
group.VirtualFolders = p.joinGroupVirtualFoldersFields(group)
|
|
|
|
p.dbHandle.groups[group.Name] = group.getACopy()
|
|
|
|
p.dbHandle.groupnames = append(p.dbHandle.groupnames, group.Name)
|
|
|
|
sort.Strings(p.dbHandle.groupnames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateGroup(group *Group) error {
|
|
|
|
if err := group.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
g, err := p.groupExistsInternal(group.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, oldFolder := range g.VirtualFolders {
|
|
|
|
p.removeRelationFromFolderMapping(oldFolder.Name, "", g.Name)
|
|
|
|
}
|
|
|
|
group.VirtualFolders = p.joinGroupVirtualFoldersFields(group)
|
|
|
|
group.CreatedAt = g.CreatedAt
|
|
|
|
group.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
group.ID = g.ID
|
|
|
|
p.dbHandle.groups[group.Name] = group.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteGroup(group Group) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
g, err := p.groupExistsInternal(group.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(g.Users) > 0 {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("the group %#v is referenced, it cannot be removed", group.Name))
|
|
|
|
}
|
|
|
|
for _, oldFolder := range g.VirtualFolders {
|
|
|
|
p.removeRelationFromFolderMapping(oldFolder.Name, "", g.Name)
|
|
|
|
}
|
|
|
|
delete(p.dbHandle.groups, group.Name)
|
|
|
|
// this could be more efficient
|
|
|
|
p.dbHandle.groupnames = make([]string, 0, len(p.dbHandle.groups))
|
|
|
|
for name := range p.dbHandle.groups {
|
|
|
|
p.dbHandle.groupnames = append(p.dbHandle.groupnames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.groupnames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpGroups() ([]Group, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
groups := make([]Group, 0, len(p.dbHandle.groups))
|
|
|
|
var err error
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return groups, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, name := range p.dbHandle.groupnames {
|
|
|
|
g := p.dbHandle.groups[name]
|
|
|
|
group := g.getACopy()
|
|
|
|
p.addVirtualFoldersToGroup(&group)
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
|
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-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) joinGroupVirtualFoldersFields(group *Group) []vfs.VirtualFolder {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range group.VirtualFolders {
|
|
|
|
folder := &group.VirtualFolders[idx]
|
|
|
|
f, err := p.addOrUpdateFolderInternal(&folder.BaseVirtualFolder, "", group.Name, 0, 0, 0)
|
|
|
|
if err == nil {
|
|
|
|
folder.BaseVirtualFolder = f
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return folders
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addVirtualFoldersToGroup(group *Group) {
|
|
|
|
if len(group.VirtualFolders) > 0 {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range group.VirtualFolders {
|
|
|
|
folder := &group.VirtualFolders[idx]
|
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder.GetACopy()
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
group.VirtualFolders = folders
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) addActionsToRule(rule *EventRule) {
|
|
|
|
var actions []EventAction
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
action := &rule.Actions[idx]
|
|
|
|
baseAction, err := p.actionExistsInternal(action.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
baseAction.Options.SetEmptySecretsIfNil()
|
|
|
|
action.BaseEventAction = baseAction
|
|
|
|
actions = append(actions, *action)
|
|
|
|
}
|
|
|
|
rule.Actions = actions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addRuleToActionMapping(ruleName, actionName string) error {
|
|
|
|
a, err := p.actionExistsInternal(actionName)
|
|
|
|
if err != nil {
|
|
|
|
return util.NewGenericError(fmt.Sprintf("action %q does not exist", actionName))
|
|
|
|
}
|
|
|
|
if !util.Contains(a.Rules, ruleName) {
|
|
|
|
a.Rules = append(a.Rules, ruleName)
|
|
|
|
p.dbHandle.actions[actionName] = a
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) removeRuleFromActionMapping(ruleName, actionName string) error {
|
|
|
|
a, err := p.actionExistsInternal(actionName)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "action %q does not exist, cannot remove from mapping", actionName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if util.Contains(a.Rules, ruleName) {
|
|
|
|
var rules []string
|
|
|
|
for _, r := range a.Rules {
|
|
|
|
if r != ruleName {
|
|
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.Rules = rules
|
|
|
|
p.dbHandle.actions[actionName] = a
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) addUserFromGroupMapping(username, groupname string) error {
|
|
|
|
g, err := p.groupExistsInternal(groupname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(g.Users, username) {
|
2022-04-25 13:49:11 +00:00
|
|
|
g.Users = append(g.Users, username)
|
|
|
|
p.dbHandle.groups[groupname] = g
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) removeUserFromGroupMapping(username, groupname string) error {
|
|
|
|
g, err := p.groupExistsInternal(groupname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(g.Users, username) {
|
2022-04-25 13:49:11 +00:00
|
|
|
var users []string
|
|
|
|
for _, u := range g.Users {
|
|
|
|
if u != username {
|
|
|
|
users = append(users, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Users = users
|
|
|
|
p.dbHandle.groups[groupname] = g
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) joinUserVirtualFoldersFields(user *User) []vfs.VirtualFolder {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folders []vfs.VirtualFolder
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
folder := &user.VirtualFolders[idx]
|
2022-04-25 13:49:11 +00:00
|
|
|
f, err := p.addOrUpdateFolderInternal(&folder.BaseVirtualFolder, user.Username, "", 0, 0, 0)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.BaseVirtualFolder = f
|
|
|
|
folders = append(folders, *folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return folders
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (p *MemoryProvider) addVirtualFoldersToUser(user *User) {
|
|
|
|
if len(user.VirtualFolders) > 0 {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
folder := &user.VirtualFolders[idx]
|
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder.GetACopy()
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) removeRelationFromFolderMapping(folderName, username, groupname string) {
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := p.folderExistsInternal(folderName)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
if username != "" {
|
|
|
|
var usernames []string
|
|
|
|
for _, user := range folder.Users {
|
|
|
|
if user != username {
|
|
|
|
usernames = append(usernames, user)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
folder.Users = usernames
|
|
|
|
}
|
|
|
|
if groupname != "" {
|
|
|
|
var groups []string
|
|
|
|
for _, group := range folder.Groups {
|
|
|
|
if group != groupname {
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folder.Groups = groups
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
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
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(p.dbHandle.vfoldersNames, folder.Name) {
|
2021-02-01 18:04:15 +00:00
|
|
|
p.dbHandle.vfoldersNames = append(p.dbHandle.vfoldersNames, folder.Name)
|
|
|
|
sort.Strings(p.dbHandle.vfoldersNames)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) addOrUpdateFolderInternal(baseFolder *vfs.BaseVirtualFolder, username, groupname string,
|
|
|
|
usedQuotaSize int64, usedQuotaFiles int, lastQuotaUpdate int64,
|
|
|
|
) (vfs.BaseVirtualFolder, error) {
|
2021-03-21 18:15:47 +00:00
|
|
|
folder, err := p.folderExistsInternal(baseFolder.Name)
|
|
|
|
if err == nil {
|
|
|
|
// exists
|
|
|
|
folder.MappedPath = baseFolder.MappedPath
|
|
|
|
folder.Description = baseFolder.Description
|
|
|
|
folder.FsConfig = baseFolder.FsConfig.GetACopy()
|
2022-05-19 17:49:51 +00:00
|
|
|
if username != "" && !util.Contains(folder.Users, username) {
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.Users = append(folder.Users, username)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if groupname != "" && !util.Contains(folder.Groups, groupname) {
|
2022-04-25 13:49:11 +00:00
|
|
|
folder.Groups = append(folder.Groups, groupname)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
p.updateFoldersMappingInternal(folder)
|
|
|
|
return folder, nil
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2021-03-21 18:15:47 +00:00
|
|
|
folder = baseFolder.GetACopy()
|
|
|
|
folder.ID = p.getNextFolderID()
|
|
|
|
folder.UsedQuotaSize = usedQuotaSize
|
|
|
|
folder.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
folder.LastQuotaUpdate = lastQuotaUpdate
|
2022-04-25 13:49:11 +00:00
|
|
|
if username != "" {
|
|
|
|
folder.Users = []string{username}
|
|
|
|
}
|
|
|
|
if groupname != "" {
|
|
|
|
folder.Groups = []string{groupname}
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
p.updateFoldersMappingInternal(folder)
|
2021-03-21 18:15:47 +00:00
|
|
|
return folder, nil
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
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-07-11 13:26:51 +00:00
|
|
|
return vfs.BaseVirtualFolder{}, util.NewRecordNotFoundError(fmt.Sprintf("folder %#v does not exist", name))
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) getFolders(limit, offset int, order string, minimal bool) ([]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-03-21 18:15:47 +00:00
|
|
|
f := p.dbHandle.vfolders[name]
|
|
|
|
folder := f.GetACopy()
|
2021-03-22 18:03:25 +00:00
|
|
|
folder.PrepareForRendering()
|
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]
|
2021-03-21 18:15:47 +00:00
|
|
|
f := p.dbHandle.vfolders[name]
|
|
|
|
folder := f.GetACopy()
|
2021-03-22 18:03:25 +00:00
|
|
|
folder.PrepareForRendering()
|
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-03-21 18:15:47 +00:00
|
|
|
folder, err := p.folderExistsInternal(name)
|
|
|
|
if err != nil {
|
|
|
|
return vfs.BaseVirtualFolder{}, err
|
|
|
|
}
|
|
|
|
return folder.GetACopy(), nil
|
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()
|
2021-03-21 18:15:47 +00:00
|
|
|
// now update the related users
|
|
|
|
for _, username := range folder.Users {
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err == nil {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
userFolder := &user.VirtualFolders[idx]
|
|
|
|
if folder.Name == userFolder.Name {
|
|
|
|
userFolder.BaseVirtualFolder = folder.GetACopy()
|
|
|
|
}
|
|
|
|
folders = append(folders, *userFolder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:59:13 +00:00
|
|
|
func (p *MemoryProvider) deleteFolder(f 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
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:59:13 +00:00
|
|
|
folder, err := p.folderExistsInternal(f.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
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
userFolder := &user.VirtualFolders[idx]
|
2021-02-01 18:04:15 +00:00
|
|
|
if folder.Name != userFolder.Name {
|
2021-03-21 18:15:47 +00:00
|
|
|
folders = append(folders, *userFolder)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
for _, groupname := range folder.Groups {
|
|
|
|
group, err := p.groupExistsInternal(groupname)
|
|
|
|
if err == nil {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range group.VirtualFolders {
|
|
|
|
groupFolder := &group.VirtualFolders[idx]
|
|
|
|
if folder.Name != groupFolder.Name {
|
|
|
|
folders = append(folders, *groupFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.VirtualFolders = folders
|
|
|
|
p.dbHandle.groups[group.Name] = group
|
|
|
|
}
|
|
|
|
}
|
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-08-17 16:08:32 +00:00
|
|
|
func (p *MemoryProvider) apiKeyExistsInternal(keyID string) (APIKey, error) {
|
|
|
|
if val, ok := p.dbHandle.apiKeys[keyID]; ok {
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
|
|
|
return APIKey{}, util.NewRecordNotFoundError(fmt.Sprintf("API key %#v does not exist", keyID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) apiKeyExists(keyID string) (APIKey, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return APIKey{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return p.apiKeyExistsInternal(keyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addAPIKey(apiKey *APIKey) error {
|
|
|
|
err := apiKey.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = p.apiKeyExistsInternal(apiKey.KeyID)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("API key %#v already exists", apiKey.KeyID)
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if apiKey.User != "" {
|
|
|
|
if _, err := p.userExistsInternal(apiKey.User); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if apiKey.Admin != "" {
|
|
|
|
if _, err := p.adminExistsInternal(apiKey.Admin); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related admin %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 13:51:43 +00:00
|
|
|
apiKey.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-08-17 16:08:32 +00:00
|
|
|
apiKey.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-08-19 13:51:43 +00:00
|
|
|
apiKey.LastUseAt = 0
|
2021-08-17 16:08:32 +00:00
|
|
|
p.dbHandle.apiKeys[apiKey.KeyID] = apiKey.getACopy()
|
|
|
|
p.dbHandle.apiKeysIDs = append(p.dbHandle.apiKeysIDs, apiKey.KeyID)
|
|
|
|
sort.Strings(p.dbHandle.apiKeysIDs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateAPIKey(apiKey *APIKey) error {
|
|
|
|
err := apiKey.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
k, err := p.apiKeyExistsInternal(apiKey.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if apiKey.User != "" {
|
|
|
|
if _, err := p.userExistsInternal(apiKey.User); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if apiKey.Admin != "" {
|
|
|
|
if _, err := p.adminExistsInternal(apiKey.Admin); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related admin %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
apiKey.ID = k.ID
|
|
|
|
apiKey.KeyID = k.KeyID
|
|
|
|
apiKey.Key = k.Key
|
|
|
|
apiKey.CreatedAt = k.CreatedAt
|
|
|
|
apiKey.LastUseAt = k.LastUseAt
|
|
|
|
apiKey.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.apiKeys[apiKey.KeyID] = apiKey.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) deleteAPIKey(apiKey APIKey) error {
|
2021-08-17 16:08:32 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
_, err := p.apiKeyExistsInternal(apiKey.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(p.dbHandle.apiKeys, apiKey.KeyID)
|
2021-11-06 13:13:20 +00:00
|
|
|
p.updateAPIKeysOrdering()
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getAPIKeys(limit int, offset int, order string) ([]APIKey, error) {
|
|
|
|
apiKeys := make([]APIKey, 0, limit)
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return apiKeys, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return apiKeys, nil
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderDESC {
|
|
|
|
for i := len(p.dbHandle.apiKeysIDs) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keyID := p.dbHandle.apiKeysIDs[i]
|
|
|
|
k := p.dbHandle.apiKeys[keyID]
|
|
|
|
apiKey := k.getACopy()
|
|
|
|
apiKey.HideConfidentialData()
|
|
|
|
apiKeys = append(apiKeys, apiKey)
|
|
|
|
if len(apiKeys) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, keyID := range p.dbHandle.apiKeysIDs {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
k := p.dbHandle.apiKeys[keyID]
|
|
|
|
apiKey := k.getACopy()
|
|
|
|
apiKey.HideConfidentialData()
|
|
|
|
apiKeys = append(apiKeys, apiKey)
|
|
|
|
if len(apiKeys) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpAPIKeys() ([]APIKey, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
apiKeys := make([]APIKey, 0, len(p.dbHandle.apiKeys))
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return apiKeys, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, k := range p.dbHandle.apiKeys {
|
|
|
|
apiKeys = append(apiKeys, k)
|
|
|
|
}
|
|
|
|
return apiKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteAPIKeysWithUser(username string) {
|
2021-11-06 13:13:20 +00:00
|
|
|
found := false
|
2021-08-17 16:08:32 +00:00
|
|
|
for k, v := range p.dbHandle.apiKeys {
|
|
|
|
if v.User == username {
|
|
|
|
delete(p.dbHandle.apiKeys, k)
|
2021-11-06 13:13:20 +00:00
|
|
|
found = true
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if found {
|
|
|
|
p.updateAPIKeysOrdering()
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteAPIKeysWithAdmin(username string) {
|
2021-11-06 13:13:20 +00:00
|
|
|
found := false
|
2021-08-17 16:08:32 +00:00
|
|
|
for k, v := range p.dbHandle.apiKeys {
|
|
|
|
if v.Admin == username {
|
|
|
|
delete(p.dbHandle.apiKeys, k)
|
2021-11-06 13:13:20 +00:00
|
|
|
found = true
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if found {
|
|
|
|
p.updateAPIKeysOrdering()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteSharesWithUser(username string) {
|
|
|
|
found := false
|
|
|
|
for k, v := range p.dbHandle.shares {
|
|
|
|
if v.Username == username {
|
|
|
|
delete(p.dbHandle.shares, k)
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
p.updateSharesOrdering()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateAPIKeysOrdering() {
|
|
|
|
// this could be more efficient
|
|
|
|
p.dbHandle.apiKeysIDs = make([]string, 0, len(p.dbHandle.apiKeys))
|
|
|
|
for keyID := range p.dbHandle.apiKeys {
|
|
|
|
p.dbHandle.apiKeysIDs = append(p.dbHandle.apiKeysIDs, keyID)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.apiKeysIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateSharesOrdering() {
|
|
|
|
// this could be more efficient
|
|
|
|
p.dbHandle.sharesIDs = make([]string, 0, len(p.dbHandle.shares))
|
|
|
|
for shareID := range p.dbHandle.shares {
|
|
|
|
p.dbHandle.sharesIDs = append(p.dbHandle.sharesIDs, shareID)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.sharesIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) shareExistsInternal(shareID, username string) (Share, error) {
|
|
|
|
if val, ok := p.dbHandle.shares[shareID]; ok {
|
|
|
|
if username != "" && val.Username != username {
|
|
|
|
return Share{}, util.NewRecordNotFoundError(fmt.Sprintf("Share %#v does not exist", shareID))
|
|
|
|
}
|
|
|
|
return val.getACopy(), nil
|
|
|
|
}
|
|
|
|
return Share{}, util.NewRecordNotFoundError(fmt.Sprintf("Share %#v does not exist", shareID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) shareExists(shareID, username string) (Share, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return Share{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return p.shareExistsInternal(shareID, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addShare(share *Share) error {
|
|
|
|
err := share.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = p.shareExistsInternal(share.ShareID, share.Username)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("share %#v already exists", share.ShareID)
|
|
|
|
}
|
|
|
|
if _, err := p.userExistsInternal(share.Username); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", share.Username))
|
|
|
|
}
|
2021-11-27 10:12:51 +00:00
|
|
|
if !share.IsRestore {
|
|
|
|
share.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
share.UpdatedAt = share.CreatedAt
|
|
|
|
share.LastUseAt = 0
|
|
|
|
share.UsedTokens = 0
|
|
|
|
}
|
|
|
|
if share.CreatedAt == 0 {
|
|
|
|
share.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
}
|
|
|
|
if share.UpdatedAt == 0 {
|
|
|
|
share.UpdatedAt = share.CreatedAt
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
p.dbHandle.shares[share.ShareID] = share.getACopy()
|
|
|
|
p.dbHandle.sharesIDs = append(p.dbHandle.sharesIDs, share.ShareID)
|
|
|
|
sort.Strings(p.dbHandle.sharesIDs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateShare(share *Share) error {
|
|
|
|
err := share.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
s, err := p.shareExistsInternal(share.ShareID, share.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := p.userExistsInternal(share.Username); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", share.Username))
|
|
|
|
}
|
|
|
|
share.ID = s.ID
|
|
|
|
share.ShareID = s.ShareID
|
2021-11-27 10:12:51 +00:00
|
|
|
if !share.IsRestore {
|
|
|
|
share.UsedTokens = s.UsedTokens
|
|
|
|
share.CreatedAt = s.CreatedAt
|
|
|
|
share.LastUseAt = s.LastUseAt
|
|
|
|
share.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
}
|
|
|
|
if share.CreatedAt == 0 {
|
|
|
|
share.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
}
|
|
|
|
if share.UpdatedAt == 0 {
|
|
|
|
share.UpdatedAt = share.CreatedAt
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
p.dbHandle.shares[share.ShareID] = share.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) deleteShare(share Share) error {
|
2021-11-06 13:13:20 +00:00
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
_, err := p.shareExistsInternal(share.ShareID, share.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(p.dbHandle.shares, share.ShareID)
|
|
|
|
p.updateSharesOrdering()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getShares(limit int, offset int, order, username string) ([]Share, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return []Share{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return []Share{}, nil
|
|
|
|
}
|
|
|
|
shares := make([]Share, 0, limit)
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderDESC {
|
|
|
|
for i := len(p.dbHandle.sharesIDs) - 1; i >= 0; i-- {
|
|
|
|
shareID := p.dbHandle.sharesIDs[i]
|
|
|
|
s := p.dbHandle.shares[shareID]
|
|
|
|
if s.Username != username {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
share := s.getACopy()
|
|
|
|
share.HideConfidentialData()
|
|
|
|
shares = append(shares, share)
|
|
|
|
if len(shares) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, shareID := range p.dbHandle.sharesIDs {
|
|
|
|
s := p.dbHandle.shares[shareID]
|
|
|
|
if s.Username != username {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
share := s.getACopy()
|
|
|
|
share.HideConfidentialData()
|
|
|
|
shares = append(shares, share)
|
|
|
|
if len(shares) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return shares, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpShares() ([]Share, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
|
|
|
|
shares := make([]Share, 0, len(p.dbHandle.shares))
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return shares, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
for _, s := range p.dbHandle.shares {
|
|
|
|
shares = append(shares, s)
|
|
|
|
}
|
|
|
|
return shares, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateShareLastUse(shareID string, numTokens int) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
share, err := p.shareExistsInternal(shareID, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
share.LastUseAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
share.UsedTokens += numTokens
|
|
|
|
p.dbHandle.shares[share.ShareID] = share
|
|
|
|
return nil
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 11:09:17 +00:00
|
|
|
func (p *MemoryProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
2021-12-25 11:08:07 +00:00
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-01-16 11:09:17 +00:00
|
|
|
func (p *MemoryProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
|
|
|
return DefenderEntry{}, ErrNotImplemented
|
2021-12-25 11:08:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 11:09:17 +00:00
|
|
|
func (p *MemoryProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
|
|
|
return DefenderEntry{}, ErrNotImplemented
|
2021-12-25 11:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateDefenderBanTime(ip string, minutes int) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteDefenderHost(ip string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addDefenderEvent(ip string, score int) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) setDefenderBanTime(ip string, banTime int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) cleanupDefender(from int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *MemoryProvider) addActiveTransfer(transfer ActiveTransfer) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateActiveTransferSizes(ulSize, dlSize, transferID int64, connectionID string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) removeActiveTransfer(transferID int64, connectionID string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) cleanupActiveTransfers(before time.Time) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getActiveTransfers(from time.Time) ([]ActiveTransfer, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
func (p *MemoryProvider) addSharedSession(session Session) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteSharedSession(key string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getSharedSession(key string) (Session, error) {
|
|
|
|
return Session{}, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) cleanupSharedSessions(sessionType SessionType, before int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) getEventActions(limit, offset int, order string, minimal bool) ([]BaseEventAction, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
actions := make([]BaseEventAction, 0, limit)
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for _, name := range p.dbHandle.actionsNames {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
a := p.dbHandle.actions[name]
|
|
|
|
action := a.getACopy()
|
|
|
|
action.PrepareForRendering()
|
|
|
|
actions = append(actions, action)
|
|
|
|
if len(actions) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(p.dbHandle.actionsNames) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := p.dbHandle.actionsNames[i]
|
|
|
|
a := p.dbHandle.actions[name]
|
|
|
|
action := a.getACopy()
|
|
|
|
action.PrepareForRendering()
|
|
|
|
actions = append(actions, action)
|
|
|
|
if len(actions) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpEventActions() ([]BaseEventAction, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
actions := make([]BaseEventAction, 0, len(p.dbHandle.actions))
|
|
|
|
for _, name := range p.dbHandle.actionsNames {
|
|
|
|
a := p.dbHandle.actions[name]
|
|
|
|
action := a.getACopy()
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
return actions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) eventActionExists(name string) (BaseEventAction, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return BaseEventAction{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
return p.actionExistsInternal(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addEventAction(action *BaseEventAction) error {
|
|
|
|
err := action.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
_, err = p.actionExistsInternal(action.Name)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("event action %q already exists", action.Name)
|
|
|
|
}
|
|
|
|
action.ID = p.getNextActionID()
|
|
|
|
action.Rules = nil
|
|
|
|
p.dbHandle.actions[action.Name] = action.getACopy()
|
|
|
|
p.dbHandle.actionsNames = append(p.dbHandle.actionsNames, action.Name)
|
|
|
|
sort.Strings(p.dbHandle.actionsNames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateEventAction(action *BaseEventAction) error {
|
|
|
|
err := action.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
oldAction, err := p.actionExistsInternal(action.Name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("event action %s does not exist", action.Name)
|
|
|
|
}
|
|
|
|
action.ID = oldAction.ID
|
|
|
|
action.Name = oldAction.Name
|
|
|
|
action.Rules = nil
|
|
|
|
if len(oldAction.Rules) > 0 {
|
|
|
|
var relatedRules []string
|
|
|
|
for _, ruleName := range oldAction.Rules {
|
|
|
|
rule, err := p.ruleExistsInternal(ruleName)
|
|
|
|
if err == nil {
|
|
|
|
relatedRules = append(relatedRules, ruleName)
|
|
|
|
rule.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.rules[ruleName] = rule
|
|
|
|
setLastRuleUpdate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action.Rules = relatedRules
|
|
|
|
}
|
|
|
|
p.dbHandle.actions[action.Name] = action.getACopy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteEventAction(action BaseEventAction) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
oldAction, err := p.actionExistsInternal(action.Name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("event action %s does not exist", action.Name)
|
|
|
|
}
|
|
|
|
if len(oldAction.Rules) > 0 {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("action %s is referenced, it cannot be removed", oldAction.Name))
|
|
|
|
}
|
|
|
|
delete(p.dbHandle.actions, action.Name)
|
|
|
|
// this could be more efficient
|
|
|
|
p.dbHandle.actionsNames = make([]string, 0, len(p.dbHandle.actions))
|
|
|
|
for name := range p.dbHandle.actions {
|
|
|
|
p.dbHandle.actionsNames = append(p.dbHandle.actionsNames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.actionsNames)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getEventRules(limit, offset int, order string) ([]EventRule, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
rules := make([]EventRule, 0, limit)
|
|
|
|
if order == OrderASC {
|
|
|
|
for _, name := range p.dbHandle.rulesNames {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r := p.dbHandle.rules[name]
|
|
|
|
rule := r.getACopy()
|
|
|
|
p.addActionsToRule(&rule)
|
|
|
|
rule.PrepareForRendering()
|
|
|
|
rules = append(rules, rule)
|
|
|
|
if len(rules) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := len(p.dbHandle.rulesNames) - 1; i >= 0; i-- {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := p.dbHandle.rulesNames[i]
|
|
|
|
r := p.dbHandle.rules[name]
|
|
|
|
rule := r.getACopy()
|
|
|
|
p.addActionsToRule(&rule)
|
|
|
|
rule.PrepareForRendering()
|
|
|
|
rules = append(rules, rule)
|
|
|
|
if len(rules) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) dumpEventRules() ([]EventRule, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
rules := make([]EventRule, 0, len(p.dbHandle.rules))
|
|
|
|
for _, name := range p.dbHandle.rulesNames {
|
|
|
|
r := p.dbHandle.rules[name]
|
|
|
|
rule := r.getACopy()
|
|
|
|
p.addActionsToRule(&rule)
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getRecentlyUpdatedRules(after int64) ([]EventRule, error) {
|
|
|
|
if getLastRuleUpdate() < after {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return nil, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
rules := make([]EventRule, 0, 10)
|
|
|
|
for _, name := range p.dbHandle.rulesNames {
|
|
|
|
r := p.dbHandle.rules[name]
|
|
|
|
if r.UpdatedAt < after {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rule := r.getACopy()
|
|
|
|
p.addActionsToRule(&rule)
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) eventRuleExists(name string) (EventRule, error) {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return EventRule{}, errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
rule, err := p.ruleExistsInternal(name)
|
|
|
|
if err != nil {
|
|
|
|
return rule, err
|
|
|
|
}
|
|
|
|
p.addActionsToRule(&rule)
|
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addEventRule(rule *EventRule) error {
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
_, err := p.ruleExistsInternal(rule.Name)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("event rule %q already exists", rule.Name)
|
|
|
|
}
|
|
|
|
rule.ID = p.getNextRuleID()
|
|
|
|
rule.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
rule.UpdatedAt = rule.CreatedAt
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
if err := p.addRuleToActionMapping(rule.Name, rule.Actions[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(rule.Actions, func(i, j int) bool {
|
|
|
|
return rule.Actions[i].Order < rule.Actions[j].Order
|
|
|
|
})
|
|
|
|
p.dbHandle.rules[rule.Name] = rule.getACopy()
|
|
|
|
p.dbHandle.rulesNames = append(p.dbHandle.rulesNames, rule.Name)
|
|
|
|
sort.Strings(p.dbHandle.rulesNames)
|
|
|
|
setLastRuleUpdate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateEventRule(rule *EventRule) error {
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
oldRule, err := p.ruleExistsInternal(rule.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldRule.Actions {
|
|
|
|
if err = p.removeRuleFromActionMapping(rule.Name, oldRule.Actions[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
if err = p.addRuleToActionMapping(rule.Name, rule.Actions[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rule.ID = oldRule.ID
|
|
|
|
rule.CreatedAt = oldRule.CreatedAt
|
|
|
|
rule.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
sort.Slice(rule.Actions, func(i, j int) bool {
|
|
|
|
return rule.Actions[i].Order < rule.Actions[j].Order
|
|
|
|
})
|
|
|
|
p.dbHandle.rules[rule.Name] = rule.getACopy()
|
|
|
|
setLastRuleUpdate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) deleteEventRule(rule EventRule, softDelete bool) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
oldRule, err := p.ruleExistsInternal(rule.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oldRule.Actions) > 0 {
|
|
|
|
for idx := range oldRule.Actions {
|
|
|
|
if err = p.removeRuleFromActionMapping(rule.Name, oldRule.Actions[idx].Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(p.dbHandle.rules, rule.Name)
|
|
|
|
p.dbHandle.rulesNames = make([]string, 0, len(p.dbHandle.rules))
|
|
|
|
for name := range p.dbHandle.rules {
|
|
|
|
p.dbHandle.rulesNames = append(p.dbHandle.rulesNames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(p.dbHandle.rulesNames)
|
|
|
|
setLastRuleUpdate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getTaskByName(name string) (Task, error) {
|
|
|
|
return Task{}, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) addTask(name string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateTask(name string, version int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) updateTaskTimestamp(name string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-08-21 17:01:08 +00:00
|
|
|
func (p *MemoryProvider) setFirstDownloadTimestamp(username string) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user.FirstDownload > 0 {
|
|
|
|
return util.NewGenericError(fmt.Sprintf("first download already set to %v",
|
|
|
|
util.GetTimeFromMsecSinceEpoch(user.FirstDownload)))
|
|
|
|
}
|
|
|
|
user.FirstDownload = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) setFirstUploadTimestamp(username string) error {
|
|
|
|
p.dbHandle.Lock()
|
|
|
|
defer p.dbHandle.Unlock()
|
|
|
|
if p.dbHandle.isClosed {
|
|
|
|
return errMemoryProviderClosed
|
|
|
|
}
|
|
|
|
user, err := p.userExistsInternal(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user.FirstUpload > 0 {
|
|
|
|
return util.NewGenericError(fmt.Sprintf("first upload already set to %v",
|
|
|
|
util.GetTimeFromMsecSinceEpoch(user.FirstUpload)))
|
|
|
|
}
|
|
|
|
user.FirstUpload = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
p.dbHandle.users[user.Username] = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *MemoryProvider) getNextGroupID() int64 {
|
|
|
|
nextID := int64(1)
|
|
|
|
for _, g := range p.dbHandle.groups {
|
|
|
|
if g.ID >= nextID {
|
|
|
|
nextID = g.ID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) getNextActionID() int64 {
|
|
|
|
nextID := int64(1)
|
|
|
|
for _, a := range p.dbHandle.actions {
|
|
|
|
if a.ID >= nextID {
|
|
|
|
nextID = a.ID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) getNextRuleID() int64 {
|
|
|
|
nextID := int64(1)
|
|
|
|
for _, r := range p.dbHandle.rules {
|
|
|
|
if r.ID >= nextID {
|
|
|
|
nextID = r.ID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextID
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
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{}
|
2021-11-06 13:13:20 +00:00
|
|
|
p.dbHandle.apiKeys = make(map[string]APIKey)
|
|
|
|
p.dbHandle.apiKeysIDs = []string{}
|
|
|
|
p.dbHandle.shares = make(map[string]Share)
|
|
|
|
p.dbHandle.sharesIDs = []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-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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")
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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")
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "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
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreFolders(dump); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.restoreGroups(dump); err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreUsers(dump); err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreAdmins(dump); err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreAPIKeys(dump); err != nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreShares(dump); err != nil {
|
2021-08-19 13:51:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if err := p.restoreEventActions(dump); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.restoreEventRules(dump); err != nil {
|
2021-11-06 13:13:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
providerLog(logger.LevelDebug, "config loaded from file: %#v", p.dbHandle.configFile)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) restoreEventActions(dump BackupData) error {
|
|
|
|
for _, action := range dump.EventActions {
|
|
|
|
a, err := p.eventActionExists(action.Name)
|
|
|
|
action := action // pin
|
|
|
|
if err == nil {
|
|
|
|
action.ID = a.ID
|
|
|
|
err = UpdateEventAction(&action, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error updating event action %q: %v", action.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = AddEventAction(&action, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error adding event action %q: %v", action.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) restoreEventRules(dump BackupData) error {
|
|
|
|
for _, rule := range dump.EventRules {
|
|
|
|
r, err := p.eventRuleExists(rule.Name)
|
|
|
|
rule := rule // pin
|
|
|
|
if err == nil {
|
|
|
|
rule.ID = r.ID
|
|
|
|
err = UpdateEventRule(&rule, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error updating event rule %q: %v", rule.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = AddEventRule(&rule, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error adding event rule %q: %v", rule.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemoryProvider) restoreShares(dump BackupData) error {
|
2021-11-06 13:13:20 +00:00
|
|
|
for _, share := range dump.Shares {
|
|
|
|
s, err := p.shareExists(share.ShareID, "")
|
|
|
|
share := share // pin
|
2021-11-27 10:12:51 +00:00
|
|
|
share.IsRestore = true
|
2021-11-06 13:13:20 +00:00
|
|
|
if err == nil {
|
|
|
|
share.ID = s.ID
|
|
|
|
err = UpdateShare(&share, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error updating share %#v: %v", share.ShareID, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = AddShare(&share, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error adding share %#v: %v", share.ShareID, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) restoreAPIKeys(dump BackupData) error {
|
2021-08-19 13:51:43 +00:00
|
|
|
for _, apiKey := range dump.APIKeys {
|
2021-11-06 13:13:20 +00:00
|
|
|
if apiKey.Key == "" {
|
2021-08-19 13:51:43 +00:00
|
|
|
return fmt.Errorf("cannot restore an empty API key: %+v", apiKey)
|
|
|
|
}
|
|
|
|
k, err := p.apiKeyExists(apiKey.KeyID)
|
|
|
|
apiKey := apiKey // pin
|
|
|
|
if err == nil {
|
|
|
|
apiKey.ID = k.ID
|
2021-10-10 11:08:05 +00:00
|
|
|
err = UpdateAPIKey(&apiKey, ActionExecutorSystem, "")
|
2021-08-19 13:51:43 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error updating API key %#v: %v", apiKey.KeyID, err)
|
2021-08-19 13:51:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-10 11:08:05 +00:00
|
|
|
err = AddAPIKey(&apiKey, ActionExecutorSystem, "")
|
2021-08-19 13:51:43 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error adding API key %#v: %v", apiKey.KeyID, err)
|
2021-08-19 13:51:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) restoreAdmins(dump BackupData) error {
|
2021-02-01 18:04:15 +00:00
|
|
|
for _, admin := range dump.Admins {
|
|
|
|
admin := admin // pin
|
2022-01-31 17:01:37 +00:00
|
|
|
admin.Username = config.convertName(admin.Username)
|
|
|
|
a, err := p.adminExists(admin.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
2021-02-01 18:04:15 +00:00
|
|
|
admin.ID = a.ID
|
2021-10-10 11:08:05 +00:00
|
|
|
err = UpdateAdmin(&admin, ActionExecutorSystem, "")
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error updating admin %#v: %v", admin.Username, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-10 11:08:05 +00:00
|
|
|
err = AddAdmin(&admin, ActionExecutorSystem, "")
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error adding admin %#v: %v", admin.Username, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) restoreGroups(dump BackupData) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
for _, group := range dump.Groups {
|
|
|
|
group := group // pin
|
|
|
|
group.Name = config.convertName(group.Name)
|
|
|
|
g, err := p.groupExists(group.Name)
|
|
|
|
if err == nil {
|
|
|
|
group.ID = g.ID
|
|
|
|
err = UpdateGroup(&group, g.Users, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error updating group %#v: %v", group.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
group.Users = nil
|
|
|
|
err = AddGroup(&group, ActionExecutorSystem, "")
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "error adding group %#v: %v", group.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *MemoryProvider) restoreFolders(dump BackupData) error {
|
2021-02-01 18:04:15 +00:00
|
|
|
for _, folder := range dump.Folders {
|
2021-01-05 08:50:22 +00:00
|
|
|
folder := folder // pin
|
2022-01-31 17:01:37 +00:00
|
|
|
folder.Name = config.convertName(folder.Name)
|
2021-02-01 18:04:15 +00:00
|
|
|
f, err := p.getFolderByName(folder.Name)
|
|
|
|
if err == nil {
|
|
|
|
folder.ID = f.ID
|
2022-04-25 13:49:11 +00:00
|
|
|
err = UpdateFolder(&folder, f.Users, f.Groups, ActionExecutorSystem, "")
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error updating folder %#v: %v", folder.Name, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
folder.Users = nil
|
2022-07-24 06:10:23 +00:00
|
|
|
err = AddFolder(&folder, ActionExecutorSystem, "")
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error adding folder %#v: %v", folder.Name, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
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
|
2022-01-31 17:01:37 +00:00
|
|
|
user.Username = config.convertName(user.Username)
|
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-10-10 11:08:05 +00:00
|
|
|
err = UpdateUser(&user, ActionExecutorSystem, "")
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error updating user %#v: %v", user.Username, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-10 11:08:05 +00:00
|
|
|
err = AddUser(&user, ActionExecutorSystem, "")
|
2020-02-02 21:20:39 +00:00
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error adding user %#v: %v", user.Username, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
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")
|
|
|
|
}
|
2021-11-15 17:40:31 +00:00
|
|
|
|
|
|
|
func (p *MemoryProvider) resetDatabase() error {
|
|
|
|
return errors.New("memory provider does not store data, reset not possible")
|
|
|
|
}
|