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/>.
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
//go:build !nobolt
|
2020-05-23 09:58:05 +00:00
|
|
|
// +build !nobolt
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2021-02-28 11:10:40 +00:00
|
|
|
"crypto/x509"
|
2019-08-12 16:31:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2022-07-11 06:17:36 +00:00
|
|
|
"sort"
|
2019-08-12 16:31:31 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
|
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/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2019-08-12 16:31:31 +00:00
|
|
|
)
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
const (
|
2022-09-25 17:48:55 +00:00
|
|
|
boltDatabaseVersion = 23
|
2019-11-13 10:36:21 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
var (
|
2021-02-16 18:11:36 +00:00
|
|
|
usersBucket = []byte("users")
|
2022-04-25 13:49:11 +00:00
|
|
|
groupsBucket = []byte("groups")
|
2021-01-17 21:29:08 +00:00
|
|
|
foldersBucket = []byte("folders")
|
|
|
|
adminsBucket = []byte("admins")
|
2021-08-17 16:08:32 +00:00
|
|
|
apiKeysBucket = []byte("api_keys")
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesBucket = []byte("shares")
|
2022-07-11 06:17:36 +00:00
|
|
|
actionsBucket = []byte("events_actions")
|
|
|
|
rulesBucket = []byte("events_rules")
|
2021-01-17 21:29:08 +00:00
|
|
|
dbVersionBucket = []byte("db_version")
|
|
|
|
dbVersionKey = []byte("version")
|
2022-04-25 13:49:11 +00:00
|
|
|
boltBuckets = [][]byte{usersBucket, groupsBucket, foldersBucket, adminsBucket, apiKeysBucket,
|
2022-07-11 06:17:36 +00:00
|
|
|
sharesBucket, actionsBucket, rulesBucket, dbVersionBucket}
|
2019-08-12 16:31:31 +00:00
|
|
|
)
|
|
|
|
|
2022-01-09 11:25:53 +00:00
|
|
|
// BoltProvider defines the auth provider for bolt key/value store
|
2019-08-12 16:31:31 +00:00
|
|
|
type BoltProvider struct {
|
|
|
|
dbHandle *bolt.DB
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
func init() {
|
2020-06-19 15:08:51 +00:00
|
|
|
version.AddFeature("+bolt")
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
func initializeBoltProvider(basePath string) error {
|
|
|
|
var err error
|
2021-03-23 18:14:15 +00:00
|
|
|
|
2019-08-12 16:31:31 +00:00
|
|
|
dbPath := config.Name
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsFileInputValid(dbPath) {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("invalid database path: %#v", dbPath)
|
2020-03-02 23:34:06 +00:00
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
if !filepath.IsAbs(dbPath) {
|
|
|
|
dbPath = filepath.Join(basePath, dbPath)
|
|
|
|
}
|
|
|
|
dbHandle, err := bolt.Open(dbPath, 0600, &bolt.Options{
|
|
|
|
NoGrowSync: false,
|
|
|
|
FreelistType: bolt.FreelistArrayType,
|
|
|
|
Timeout: 5 * time.Second})
|
|
|
|
if err == nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelDebug, "bolt key store handle created")
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
for _, bucket := range boltBuckets {
|
|
|
|
if err := dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, e := tx.CreateBucketIfNotExists(bucket)
|
|
|
|
return e
|
|
|
|
}); err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error creating bucket %#v: %v", string(bucket), err)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
provider = &BoltProvider{dbHandle: dbHandle}
|
2019-08-12 16:31:31 +00:00
|
|
|
} else {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "error creating bolt key/value store handler: %v", err)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) checkAvailability() error {
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err := getBoltDatabaseVersion(p.dbHandle)
|
2019-09-13 16:45:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func (p *BoltProvider) 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 *BoltProvider) validateUserAndPass(username, password, ip, protocol string) (User, error) {
|
2019-08-12 16:31:31 +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-08-12 16:31:31 +00:00
|
|
|
}
|
2021-02-16 18:11:36 +00:00
|
|
|
return checkUserAndPass(&user, password, ip, protocol)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) 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
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
err = admin.checkUserAndPass(password, ip)
|
|
|
|
return admin, err
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:16:50 +00:00
|
|
|
func (p *BoltProvider) validateUserAndPubKey(username string, pubKey []byte, isSSHCert bool) (User, string, error) {
|
2019-08-12 16:31:31 +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-08-12 16:31:31 +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-08-12 16:31:31 +00:00
|
|
|
}
|
2022-03-31 16:16:50 +00:00
|
|
|
return checkUserAndPubKey(&user, pubKey, isSSHCert)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
func (p *BoltProvider) updateAPIKeyLastUse(keyID string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(keyID)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("key %#v does not exist, unable to update last use", keyID))
|
|
|
|
}
|
|
|
|
var apiKey APIKey
|
|
|
|
err = json.Unmarshal(u, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKey.LastUseAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(keyID), buf)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error updating last use for key %#v: %v", keyID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelDebug, "last use updated for key %#v", keyID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
func (p *BoltProvider) setUpdatedAt(username string) {
|
|
|
|
p.dbHandle.Update(func(tx *bolt.Tx) error { //nolint:errcheck
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2021-08-19 13:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to update updated at", username))
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(username), buf)
|
|
|
|
if err == nil {
|
|
|
|
providerLog(logger.LevelDebug, "updated at set for user %#v", username)
|
2022-04-30 09:59:36 +00:00
|
|
|
setLastUserUpdate()
|
2021-08-19 13:51:43 +00:00
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelWarn, "error setting updated_at for user %#v: %v", username, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) updateLastLogin(username string) error {
|
2019-11-13 10:36:21 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-11-13 10:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to update last login", username))
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
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
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
err = bucket.Put([]byte(username), buf)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
2020-04-30 12:23:55 +00:00
|
|
|
providerLog(logger.LevelWarn, "error updating last login for user %#v: %v", username, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelDebug, "last login updated for user %#v", username)
|
2020-04-30 12:23:55 +00:00
|
|
|
}
|
|
|
|
return err
|
2019-11-13 10:36:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
func (p *BoltProvider) updateAdminLastLogin(username string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-08-19 13:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var a []byte
|
|
|
|
if a = bucket.Get([]byte(username)); a == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("admin %#v does not exist, unable to update last login", username))
|
|
|
|
}
|
|
|
|
var admin Admin
|
|
|
|
err = json.Unmarshal(a, &admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(username), buf)
|
|
|
|
if err == nil {
|
|
|
|
providerLog(logger.LevelDebug, "last login updated for admin %#v", username)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelWarn, "error updating last login for admin %#v: %v", username, err)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *BoltProvider) updateTransferQuota(username string, uploadSize, downloadSize int64, reset bool) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2022-01-30 10:42:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to update transfer quota",
|
|
|
|
username))
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !reset {
|
|
|
|
user.UsedUploadDataTransfer += uploadSize
|
|
|
|
user.UsedDownloadDataTransfer += downloadSize
|
|
|
|
} else {
|
|
|
|
user.UsedUploadDataTransfer = uploadSize
|
|
|
|
user.UsedDownloadDataTransfer = downloadSize
|
|
|
|
}
|
|
|
|
user.LastQuotaUpdate = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(username), buf)
|
|
|
|
providerLog(logger.LevelDebug, "transfer quota updated for user %#v, ul increment: %v dl increment: %v is reset? %v",
|
|
|
|
username, uploadSize, downloadSize, reset)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
|
2019-08-12 16:31:31 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to update quota", username))
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
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())
|
2019-08-12 16:31:31 +00:00
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
err = bucket.Put([]byte(username), buf)
|
|
|
|
providerLog(logger.LevelDebug, "quota updated for user %#v, files increment: %v size increment: %v is reset? %v",
|
|
|
|
username, filesAdd, sizeAdd, reset)
|
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *BoltProvider) getUsedQuota(username string) (int, int64, int64, int64, error) {
|
2019-08-12 16:31:31 +00:00
|
|
|
user, err := p.userExists(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-08-12 16:31:31 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
return user.UsedQuotaFiles, user.UsedQuotaSize, user.UsedUploadDataTransfer, user.UsedDownloadDataTransfer, err
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) adminExists(username string) (Admin, error) {
|
|
|
|
var admin Admin
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a := bucket.Get([]byte(username))
|
|
|
|
if a == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("admin %v does not exist", username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
return json.Unmarshal(a, &admin)
|
|
|
|
})
|
|
|
|
|
|
|
|
return admin, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addAdmin(admin *Admin) error {
|
|
|
|
err := admin.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
if a := bucket.Get([]byte(admin.Username)); a != nil {
|
|
|
|
return fmt.Errorf("admin %v already exists", admin.Username)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.ID = int64(id)
|
2021-08-19 13:51:43 +00:00
|
|
|
admin.LastLogin = 0
|
|
|
|
admin.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
admin.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2022-09-13 16:04:27 +00:00
|
|
|
for idx := range admin.Groups {
|
|
|
|
err = p.addAdminToGroupMapping(admin.Username, admin.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
buf, err := json.Marshal(admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(admin.Username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateAdmin(admin *Admin) error {
|
|
|
|
err := admin.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
var a []byte
|
|
|
|
if a = bucket.Get([]byte(admin.Username)); a == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("admin %v does not exist", admin.Username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
var oldAdmin Admin
|
|
|
|
err = json.Unmarshal(a, &oldAdmin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:04:27 +00:00
|
|
|
for idx := range oldAdmin.Groups {
|
|
|
|
err = p.removeAdminFromGroupMapping(oldAdmin.Username, oldAdmin.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range admin.Groups {
|
|
|
|
err = p.addAdminToGroupMapping(admin.Username, admin.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
admin.ID = oldAdmin.ID
|
2021-08-19 13:51:43 +00:00
|
|
|
admin.CreatedAt = oldAdmin.CreatedAt
|
|
|
|
admin.LastLogin = oldAdmin.LastLogin
|
|
|
|
admin.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-01-17 21:29:08 +00:00
|
|
|
buf, err := json.Marshal(admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(admin.Username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) deleteAdmin(admin Admin) error {
|
2021-01-17 21:29:08 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:04:27 +00:00
|
|
|
var a []byte
|
|
|
|
if a = bucket.Get([]byte(admin.Username)); a == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("admin %v does not exist", admin.Username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
var oldAdmin Admin
|
|
|
|
err = json.Unmarshal(a, &oldAdmin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oldAdmin.Groups) > 0 {
|
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldAdmin.Groups {
|
|
|
|
err = p.removeAdminFromGroupMapping(oldAdmin.Username, oldAdmin.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
if err := p.deleteRelatedAPIKey(tx, admin.Username, APIKeyScopeAdmin); err != nil {
|
2021-08-17 16:08:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
return bucket.Delete([]byte(admin.Username))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getAdmins(limit int, offset int, order string) ([]Admin, error) {
|
|
|
|
admins := make([]Admin, 0, limit)
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var admin Admin
|
|
|
|
err = json.Unmarshal(v, &admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
admins = append(admins, admin)
|
|
|
|
if len(admins) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var admin Admin
|
|
|
|
err = json.Unmarshal(v, &admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
admins = append(admins, admin)
|
|
|
|
if len(admins) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return admins, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpAdmins() ([]Admin, error) {
|
|
|
|
admins := make([]Admin, 0, 30)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var admin Admin
|
|
|
|
err = json.Unmarshal(v, &admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admins = append(admins, admin)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return admins, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) userExists(username string) (User, error) {
|
2019-08-12 16:31:31 +00:00
|
|
|
var user User
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u := bucket.Get([]byte(username))
|
|
|
|
if u == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist", username))
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
user, err = p.joinUserAndFolders(u, foldersBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
})
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) addUser(user *User) error {
|
2021-01-25 20:31:33 +00:00
|
|
|
err := ValidateUser(user)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
if u := bucket.Get([]byte(user.Username)); u != nil {
|
2019-09-05 21:42:00 +00:00
|
|
|
return fmt.Errorf("username %v already exists", user.Username)
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.ID = int64(id)
|
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.FirstDownload = 0
|
|
|
|
user.FirstUpload = 0
|
2021-08-19 13:51:43 +00:00
|
|
|
user.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range user.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.addRelationToFolderMapping(&user.VirtualFolders[idx].BaseVirtualFolder, user, nil, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range user.Groups {
|
|
|
|
err = p.addUserToGroupMapping(user.Username, user.Groups[idx].Name, groupBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
return bucket.Put([]byte(user.Username), buf)
|
2019-08-12 16:31:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) updateUser(user *User) error {
|
2021-01-25 20:31:33 +00:00
|
|
|
err := ValidateUser(user)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(user.Username)); u == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist", user.Username))
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
var oldUser User
|
|
|
|
err = json.Unmarshal(u, &oldUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if err = p.updateUserRelations(tx, user, oldUser); err != nil {
|
|
|
|
return err
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2020-10-22 08:42:40 +00:00
|
|
|
user.ID = oldUser.ID
|
2020-06-07 21:30:18 +00:00
|
|
|
user.LastQuotaUpdate = oldUser.LastQuotaUpdate
|
|
|
|
user.UsedQuotaSize = oldUser.UsedQuotaSize
|
|
|
|
user.UsedQuotaFiles = oldUser.UsedQuotaFiles
|
2022-01-30 10:42:36 +00:00
|
|
|
user.UsedUploadDataTransfer = oldUser.UsedUploadDataTransfer
|
|
|
|
user.UsedDownloadDataTransfer = oldUser.UsedDownloadDataTransfer
|
2020-06-07 21:30:18 +00:00
|
|
|
user.LastLogin = oldUser.LastLogin
|
2022-08-21 17:01:08 +00:00
|
|
|
user.FirstDownload = oldUser.FirstDownload
|
|
|
|
user.FirstUpload = oldUser.FirstUpload
|
2021-08-19 13:51:43 +00:00
|
|
|
user.CreatedAt = oldUser.CreatedAt
|
|
|
|
user.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2019-08-12 16:31:31 +00:00
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
|
|
|
|
err = bucket.Put([]byte(user.Username), buf)
|
|
|
|
if err == nil {
|
|
|
|
setLastUserUpdate()
|
|
|
|
}
|
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-17 16:48:41 +00:00
|
|
|
func (p *BoltProvider) deleteUser(user User, softDelete bool) error {
|
2019-08-12 16:31:31 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(user.Username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %q does not exist", user.Username))
|
|
|
|
}
|
|
|
|
var oldUser User
|
|
|
|
err = json.Unmarshal(u, &oldUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
if len(oldUser.VirtualFolders) > 0 {
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
for idx := range oldUser.VirtualFolders {
|
|
|
|
err = p.removeRelationFromFolderMapping(oldUser.VirtualFolders[idx], oldUser.Username, "", foldersBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
if len(oldUser.Groups) > 0 {
|
2022-04-25 13:49:11 +00:00
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
for idx := range oldUser.Groups {
|
|
|
|
err = p.removeUserFromGroupMapping(oldUser.Username, oldUser.Groups[idx].Name, groupBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := p.deleteRelatedAPIKey(tx, user.Username, APIKeyScopeUser); err != nil {
|
2021-08-17 16:08:32 +00:00
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if err := p.deleteRelatedShares(tx, user.Username); err != nil {
|
2021-11-06 13:13:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
return bucket.Delete([]byte(user.Username))
|
2019-08-12 16:31:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-02 20:20:21 +00:00
|
|
|
func (p *BoltProvider) updateUserPassword(username, password string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2022-04-02 20:20:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist", username))
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Password = password
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) dumpUsers() ([]User, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
users := make([]User, 0, 100)
|
2020-04-30 12:23:55 +00:00
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
2022-04-30 09:59:36 +00:00
|
|
|
user, err := p.joinUserAndFolders(v, foldersBucket)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:35:06 +00:00
|
|
|
func (p *BoltProvider) getRecentlyUpdatedUsers(after int64) ([]User, error) {
|
2022-04-30 09:59:36 +00:00
|
|
|
if getLastUserUpdate() < after {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
users := make([]User, 0, 10)
|
2022-01-20 17:19:20 +00:00
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2022-01-20 17:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupsBucket, err := p.getGroupsBucket(tx)
|
2022-01-20 17:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var user User
|
|
|
|
err := json.Unmarshal(v, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
if user.UpdatedAt < after {
|
2022-01-20 17:19:20 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
if len(user.VirtualFolders) > 0 {
|
2022-01-20 17:19:20 +00:00
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
folder := &user.VirtualFolders[idx]
|
2022-04-25 13:49:11 +00:00
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name, foldersBucket)
|
2022-01-20 17:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
}
|
2022-04-30 09:59:36 +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, groupsBucket)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupMapping[group.Name] = group
|
|
|
|
}
|
|
|
|
user.applyGroupSettings(groupMapping)
|
|
|
|
}
|
2022-01-20 17:19:20 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getUsersForQuotaCheck(toFetch map[string]bool) ([]User, error) {
|
|
|
|
users := make([]User, 0, 10)
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getUsersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupsBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var user User
|
|
|
|
err := json.Unmarshal(v, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if needFolders, ok := toFetch[user.Username]; ok {
|
|
|
|
if needFolders && len(user.VirtualFolders) > 0 {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
folder := &user.VirtualFolders[idx]
|
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name, foldersBucket)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
}
|
|
|
|
if len(user.Groups) > 0 {
|
|
|
|
groupMapping := make(map[string]Group)
|
|
|
|
for idx := range user.Groups {
|
|
|
|
group, err := p.groupExistsInternal(user.Groups[idx].Name, groupsBucket)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupMapping[group.Name] = group
|
|
|
|
}
|
|
|
|
user.applyGroupSettings(groupMapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.SetEmptySecretsIfNil()
|
|
|
|
user.PrepareForRendering()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 17:19:20 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) getUsers(limit int, offset int, order string) ([]User, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
users := make([]User, 0, limit)
|
2019-08-12 16:31:31 +00:00
|
|
|
var err error
|
2019-10-25 16:37:12 +00:00
|
|
|
if limit <= 0 {
|
|
|
|
return users, err
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
err = p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
2020-06-07 21:30:18 +00:00
|
|
|
if order == OrderASC {
|
2019-08-12 16:31:31 +00:00
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
user, err := p.joinUserAndFolders(v, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
user.PrepareForRendering()
|
|
|
|
users = append(users, user)
|
2019-08-12 16:31:31 +00:00
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
user, err := p.joinUserAndFolders(v, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
user.PrepareForRendering()
|
|
|
|
users = append(users, user)
|
2019-08-12 16:31:31 +00:00
|
|
|
if len(users) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) dumpFolders() ([]vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, 50)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(v, &folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
folders = append(folders, folder)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) 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
|
|
|
|
if limit <= 0 {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
err = p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(v, &folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(v, &folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 err
|
|
|
|
})
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *BoltProvider) getFolderByName(name string) (vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
folder, err = p.folderExistsInternal(name, bucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) addFolder(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
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
if f := bucket.Get([]byte(folder.Name)); f != nil {
|
|
|
|
return fmt.Errorf("folder %v already exists", folder.Name)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder.Users = nil
|
2022-09-13 16:04:27 +00:00
|
|
|
folder.Groups = nil
|
2022-04-25 13:49:11 +00:00
|
|
|
return p.addFolderInternal(*folder, bucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *BoltProvider) updateFolder(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
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var f []byte
|
|
|
|
|
|
|
|
if f = bucket.Get([]byte(folder.Name)); f == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("folder %v does not exist", folder.Name))
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
|
|
|
var oldFolder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(f, &oldFolder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
folder.ID = oldFolder.ID
|
|
|
|
folder.LastQuotaUpdate = oldFolder.LastQuotaUpdate
|
|
|
|
folder.UsedQuotaFiles = oldFolder.UsedQuotaFiles
|
|
|
|
folder.UsedQuotaSize = oldFolder.UsedQuotaSize
|
|
|
|
folder.Users = oldFolder.Users
|
2022-09-13 16:04:27 +00:00
|
|
|
folder.Groups = oldFolder.Groups
|
2021-02-01 18:04:15 +00:00
|
|
|
buf, err := json.Marshal(folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(folder.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:25:00 +00:00
|
|
|
func (p *BoltProvider) deleteFolderMappings(folder vfs.BaseVirtualFolder, usersBucket, groupsBucket *bolt.Bucket) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
for _, username := range folder.Users {
|
|
|
|
var u []byte
|
|
|
|
if u = usersBucket.Get([]byte(username)); u == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err := json.Unmarshal(u, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for _, userFolder := range user.VirtualFolders {
|
|
|
|
if folder.Name != userFolder.Name {
|
|
|
|
folders = append(folders, userFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = usersBucket.Put([]byte(user.Username), buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, groupname := range folder.Groups {
|
|
|
|
var u []byte
|
|
|
|
if u = groupsBucket.Get([]byte(groupname)); u == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(u, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for _, groupFolder := range group.VirtualFolders {
|
|
|
|
if folder.Name != groupFolder.Name {
|
|
|
|
folders = append(folders, groupFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.VirtualFolders = folders
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = groupsBucket.Put([]byte(group.Name), buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:59:13 +00:00
|
|
|
func (p *BoltProvider) deleteFolder(baseFolder vfs.BaseVirtualFolder) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
usersBucket, err := p.getUsersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
groupsBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
var f []byte
|
2022-08-16 15:59:13 +00:00
|
|
|
if f = bucket.Get([]byte(baseFolder.Name)); f == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("folder %v does not exist", baseFolder.Name))
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(f, &folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-19 20:25:00 +00:00
|
|
|
if err = p.deleteFolderMappings(folder, usersBucket, groupsBucket); err != nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
return err
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
return bucket.Delete([]byte(folder.Name))
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *BoltProvider) updateFolderQuota(name string, filesAdd int, sizeAdd int64, reset bool) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var f []byte
|
2021-02-01 18:04:15 +00:00
|
|
|
if f = bucket.Get([]byte(name)); f == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("folder %#v does not exist, unable to update quota", name))
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(f, &folder)
|
|
|
|
if err != nil {
|
|
|
|
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())
|
2020-06-07 21:30:18 +00:00
|
|
|
buf, err := json.Marshal(folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
return bucket.Put([]byte(folder.Name), buf)
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *BoltProvider) getUsedFolderQuota(name string) (int, int64, error) {
|
|
|
|
folder, err := p.getFolderByName(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 *BoltProvider) getGroups(limit, offset int, order string, minimal bool) ([]Group, error) {
|
|
|
|
groups := make([]Group, 0, limit)
|
|
|
|
var err error
|
|
|
|
if limit <= 0 {
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
err = p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var group Group
|
2022-04-30 09:59:36 +00:00
|
|
|
group, err = p.joinGroupAndFolders(v, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
group.PrepareForRendering()
|
|
|
|
groups = append(groups, group)
|
|
|
|
if len(groups) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var group Group
|
2022-04-30 09:59:36 +00:00
|
|
|
group, err = p.joinGroupAndFolders(v, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
group.PrepareForRendering()
|
|
|
|
groups = append(groups, group)
|
|
|
|
if len(groups) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getGroupsWithNames(names []string) ([]Group, error) {
|
|
|
|
var groups []Group
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
g := bucket.Get([]byte(name))
|
|
|
|
if g == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
group, err := p.joinGroupAndFolders(g, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getUsersInGroups(names []string) ([]string, error) {
|
|
|
|
var usernames []string
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
g := bucket.Get([]byte(name))
|
|
|
|
if g == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
usernames = append(usernames, group.Users...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return usernames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) groupExists(name string) (Group, error) {
|
|
|
|
var group Group
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g := bucket.Get([]byte(name))
|
|
|
|
if g == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %#v does not exist", name))
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
group, err = p.joinGroupAndFolders(g, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addGroup(group *Group) error {
|
|
|
|
if err := group.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u := bucket.Get([]byte(group.Name)); u != nil {
|
|
|
|
return fmt.Errorf("group %v already exists", group.Name)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
group.ID = int64(id)
|
|
|
|
group.CreatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
group.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2022-09-13 16:04:27 +00:00
|
|
|
group.Users = nil
|
|
|
|
group.Admins = nil
|
2022-04-25 13:49:11 +00:00
|
|
|
for idx := range group.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.addRelationToFolderMapping(&group.VirtualFolders[idx].BaseVirtualFolder, nil, group, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateGroup(group *Group) error {
|
|
|
|
if err := group.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var g []byte
|
|
|
|
if g = bucket.Get([]byte(group.Name)); g == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %#v does not exist", group.Name))
|
|
|
|
}
|
|
|
|
var oldGroup Group
|
|
|
|
err = json.Unmarshal(g, &oldGroup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldGroup.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.removeRelationFromFolderMapping(oldGroup.VirtualFolders[idx], "", oldGroup.Name, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range group.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.addRelationToFolderMapping(&group.VirtualFolders[idx].BaseVirtualFolder, nil, group, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.ID = oldGroup.ID
|
|
|
|
group.CreatedAt = oldGroup.CreatedAt
|
2022-07-11 06:17:36 +00:00
|
|
|
group.Users = oldGroup.Users
|
2022-09-13 16:04:27 +00:00
|
|
|
group.Admins = oldGroup.Admins
|
2022-04-25 13:49:11 +00:00
|
|
|
group.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteGroup(group Group) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var g []byte
|
|
|
|
if g = bucket.Get([]byte(group.Name)); g == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %#v does not exist", group.Name))
|
|
|
|
}
|
|
|
|
var oldGroup Group
|
|
|
|
err = json.Unmarshal(g, &oldGroup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oldGroup.Users) > 0 {
|
2022-09-13 16:04:27 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("the group %#v is referenced, it cannot be removed", oldGroup.Name))
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
if len(oldGroup.VirtualFolders) > 0 {
|
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldGroup.VirtualFolders {
|
|
|
|
err = p.removeRelationFromFolderMapping(oldGroup.VirtualFolders[idx], "", oldGroup.Name, foldersBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
if len(oldGroup.Admins) > 0 {
|
|
|
|
adminsBucket, err := p.getAdminsBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
for idx := range oldGroup.Admins {
|
|
|
|
err = p.removeGroupFromAdminMapping(oldGroup.Name, oldGroup.Admins[idx], adminsBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Delete([]byte(group.Name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpGroups() ([]Group, error) {
|
|
|
|
groups := make([]Group, 0, 50)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
2022-04-30 09:59:36 +00:00
|
|
|
group, err := p.joinGroupAndFolders(v, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
func (p *BoltProvider) apiKeyExists(keyID string) (APIKey, error) {
|
|
|
|
var apiKey APIKey
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k := bucket.Get([]byte(keyID))
|
|
|
|
if k == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("API key %v does not exist", keyID))
|
|
|
|
}
|
|
|
|
return json.Unmarshal(k, &apiKey)
|
|
|
|
})
|
|
|
|
return apiKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addAPIKey(apiKey *APIKey) error {
|
|
|
|
err := apiKey.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a := bucket.Get([]byte(apiKey.KeyID)); a != nil {
|
|
|
|
return fmt.Errorf("API key %v already exists", apiKey.KeyID)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKey.ID = int64(id)
|
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-11-06 13:13:20 +00:00
|
|
|
if apiKey.User != "" {
|
|
|
|
if err := p.userExistsInternal(tx, apiKey.User); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if apiKey.Admin != "" {
|
|
|
|
if err := p.adminExistsInternal(tx, 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
|
|
|
buf, err := json.Marshal(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(apiKey.KeyID), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateAPIKey(apiKey *APIKey) error {
|
|
|
|
err := apiKey.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var a []byte
|
|
|
|
|
|
|
|
if a = bucket.Get([]byte(apiKey.KeyID)); a == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("API key %v does not exist", apiKey.KeyID))
|
|
|
|
}
|
|
|
|
var oldAPIKey APIKey
|
|
|
|
err = json.Unmarshal(a, &oldAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKey.ID = oldAPIKey.ID
|
|
|
|
apiKey.KeyID = oldAPIKey.KeyID
|
|
|
|
apiKey.Key = oldAPIKey.Key
|
|
|
|
apiKey.CreatedAt = oldAPIKey.CreatedAt
|
|
|
|
apiKey.LastUseAt = oldAPIKey.LastUseAt
|
|
|
|
apiKey.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
2021-11-06 13:13:20 +00:00
|
|
|
if apiKey.User != "" {
|
|
|
|
if err := p.userExistsInternal(tx, apiKey.User); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", apiKey.User))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if apiKey.Admin != "" {
|
|
|
|
if err := p.adminExistsInternal(tx, 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
|
|
|
buf, err := json.Marshal(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(apiKey.KeyID), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) deleteAPIKey(apiKey APIKey) error {
|
2021-08-17 16:08:32 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket.Get([]byte(apiKey.KeyID)) == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("API key %v does not exist", apiKey.KeyID))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Delete([]byte(apiKey.KeyID))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getAPIKeys(limit int, offset int, order string) ([]APIKey, error) {
|
|
|
|
apiKeys := make([]APIKey, 0, limit)
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var apiKey APIKey
|
|
|
|
err = json.Unmarshal(v, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKey.HideConfidentialData()
|
|
|
|
apiKeys = append(apiKeys, apiKey)
|
|
|
|
if len(apiKeys) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var apiKey APIKey
|
|
|
|
err = json.Unmarshal(v, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKey.HideConfidentialData()
|
|
|
|
apiKeys = append(apiKeys, apiKey)
|
|
|
|
if len(apiKeys) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return apiKeys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpAPIKeys() ([]APIKey, error) {
|
|
|
|
apiKeys := make([]APIKey, 0, 30)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var apiKey APIKey
|
|
|
|
err = json.Unmarshal(v, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiKeys = append(apiKeys, apiKey)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return apiKeys, err
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func (p *BoltProvider) shareExists(shareID, username string) (Share, error) {
|
|
|
|
var share Share
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := bucket.Get([]byte(shareID))
|
|
|
|
if s == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", shareID))
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(s, &share); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if username != "" && share.Username != username {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", shareID))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addShare(share *Share) error {
|
|
|
|
err := share.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a := bucket.Get([]byte(share.ShareID)); a != nil {
|
|
|
|
return fmt.Errorf("share %v already exists", share.ShareID)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
share.ID = int64(id)
|
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
|
|
|
if err := p.userExistsInternal(tx, share.Username); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", share.Username))
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(share.ShareID), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateShare(share *Share) error {
|
|
|
|
if err := share.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-06 14:18:16 +00:00
|
|
|
var s []byte
|
2021-11-06 13:13:20 +00:00
|
|
|
|
2021-11-06 14:18:16 +00:00
|
|
|
if s = bucket.Get([]byte(share.ShareID)); s == nil {
|
2021-11-06 13:13:20 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", share.ShareID))
|
|
|
|
}
|
|
|
|
var oldObject Share
|
2021-11-06 14:18:16 +00:00
|
|
|
if err = json.Unmarshal(s, &oldObject); err != nil {
|
2021-11-06 13:13:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-06 14:18:16 +00:00
|
|
|
if oldObject.Username != share.Username {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", share.ShareID))
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
share.ID = oldObject.ID
|
|
|
|
share.ShareID = oldObject.ShareID
|
2021-11-27 10:12:51 +00:00
|
|
|
if !share.IsRestore {
|
|
|
|
share.UsedTokens = oldObject.UsedTokens
|
|
|
|
share.CreatedAt = oldObject.CreatedAt
|
|
|
|
share.LastUseAt = oldObject.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
|
|
|
if err := p.userExistsInternal(tx, share.Username); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("related user %#v does not exists", share.Username))
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(share.ShareID), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) deleteShare(share Share) error {
|
2021-11-06 13:13:20 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-06 14:18:16 +00:00
|
|
|
var s []byte
|
|
|
|
|
|
|
|
if s = bucket.Get([]byte(share.ShareID)); s == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", share.ShareID))
|
|
|
|
}
|
|
|
|
var oldObject Share
|
|
|
|
if err = json.Unmarshal(s, &oldObject); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if oldObject.Username != share.Username {
|
2021-11-06 13:13:20 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("Share %v does not exist", share.ShareID))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Delete([]byte(share.ShareID))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getShares(limit int, offset int, order, username string) ([]Share, error) {
|
|
|
|
shares := make([]Share, 0, limit)
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
itNum := 0
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var share Share
|
|
|
|
if err := json.Unmarshal(v, &share); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if share.Username != username {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
share.HideConfidentialData()
|
|
|
|
shares = append(shares, share)
|
|
|
|
if len(shares) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
var share Share
|
|
|
|
err = json.Unmarshal(v, &share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if share.Username != username {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
share.HideConfidentialData()
|
|
|
|
shares = append(shares, share)
|
|
|
|
if len(shares) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return shares, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpShares() ([]Share, error) {
|
|
|
|
shares := make([]Share, 0, 30)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var share Share
|
|
|
|
err = json.Unmarshal(v, &share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shares = append(shares, share)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return shares, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateShareLastUse(shareID string, numTokens int) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(shareID)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("share %#v does not exist, unable to update last use", shareID))
|
|
|
|
}
|
|
|
|
var share Share
|
|
|
|
err = json.Unmarshal(u, &share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
share.LastUseAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
share.UsedTokens += numTokens
|
|
|
|
buf, err := json.Marshal(share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(shareID), buf)
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error updating last use for share %#v: %v", shareID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelDebug, "last use updated for share %#v", shareID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-16 11:09:17 +00:00
|
|
|
func (p *BoltProvider) 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 *BoltProvider) 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 *BoltProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
|
|
|
return DefenderEntry{}, ErrNotImplemented
|
2021-12-25 11:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateDefenderBanTime(ip string, minutes int) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteDefenderHost(ip string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addDefenderEvent(ip string, score int) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) setDefenderBanTime(ip string, banTime int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) cleanupDefender(from int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func (p *BoltProvider) addActiveTransfer(transfer ActiveTransfer) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateActiveTransferSizes(ulSize, dlSize, transferID int64, connectionID string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) removeActiveTransfer(transferID int64, connectionID string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) cleanupActiveTransfers(before time.Time) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getActiveTransfers(from time.Time) ([]ActiveTransfer, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
func (p *BoltProvider) addSharedSession(session Session) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteSharedSession(key string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getSharedSession(key string) (Session, error) {
|
|
|
|
return Session{}, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) cleanupSharedSessions(sessionType SessionType, before int64) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *BoltProvider) getEventActions(limit, offset int, order string, minimal bool) ([]BaseEventAction, error) {
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
actions := make([]BaseEventAction, 0, limit)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var action BaseEventAction
|
|
|
|
err = json.Unmarshal(v, &action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
action.PrepareForRendering()
|
|
|
|
actions = append(actions, action)
|
|
|
|
if len(actions) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var action BaseEventAction
|
|
|
|
err = json.Unmarshal(v, &action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
action.PrepareForRendering()
|
|
|
|
actions = append(actions, action)
|
|
|
|
if len(actions) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return actions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpEventActions() ([]BaseEventAction, error) {
|
|
|
|
actions := make([]BaseEventAction, 0, 50)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var action BaseEventAction
|
|
|
|
err = json.Unmarshal(v, &action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return actions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) eventActionExists(name string) (BaseEventAction, error) {
|
|
|
|
var action BaseEventAction
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k := bucket.Get([]byte(name))
|
|
|
|
if k == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("action %q does not exist", name))
|
|
|
|
}
|
|
|
|
return json.Unmarshal(k, &action)
|
|
|
|
})
|
|
|
|
return action, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addEventAction(action *BaseEventAction) error {
|
|
|
|
err := action.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a := bucket.Get([]byte(action.Name)); a != nil {
|
|
|
|
return fmt.Errorf("event action %s already exists", action.Name)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
action.ID = int64(id)
|
|
|
|
action.Rules = nil
|
|
|
|
buf, err := json.Marshal(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(action.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateEventAction(action *BaseEventAction) error {
|
|
|
|
err := action.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var a []byte
|
|
|
|
|
|
|
|
if a = bucket.Get([]byte(action.Name)); a == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("event action %s does not exist", action.Name))
|
|
|
|
}
|
|
|
|
var oldAction BaseEventAction
|
|
|
|
err = json.Unmarshal(a, &oldAction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
action.ID = oldAction.ID
|
|
|
|
action.Name = oldAction.Name
|
|
|
|
action.Rules = nil
|
|
|
|
if len(oldAction.Rules) > 0 {
|
|
|
|
rulesBucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var relatedRules []string
|
|
|
|
for _, ruleName := range oldAction.Rules {
|
|
|
|
r := rulesBucket.Get([]byte(ruleName))
|
|
|
|
if r != nil {
|
|
|
|
relatedRules = append(relatedRules, ruleName)
|
|
|
|
var rule EventRule
|
|
|
|
err := json.Unmarshal(r, &rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rule.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = rulesBucket.Put([]byte(rule.Name), buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
setLastRuleUpdate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action.Rules = relatedRules
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(action.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteEventAction(action BaseEventAction) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var a []byte
|
|
|
|
|
|
|
|
if a = bucket.Get([]byte(action.Name)); a == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("action %s does not exist", action.Name))
|
|
|
|
}
|
|
|
|
var oldAction BaseEventAction
|
|
|
|
err = json.Unmarshal(a, &oldAction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oldAction.Rules) > 0 {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("action %s is referenced, it cannot be removed", oldAction.Name))
|
|
|
|
}
|
|
|
|
return bucket.Delete([]byte(action.Name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getEventRules(limit, offset int, order string) ([]EventRule, error) {
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rules := make([]EventRule, 0, limit)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
itNum := 0
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
if order == OrderASC {
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var rule EventRule
|
|
|
|
rule, err = p.joinRuleAndActions(v, actionsBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rule.PrepareForRendering()
|
|
|
|
rules = append(rules, rule)
|
|
|
|
if len(rules) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := cursor.Last(); k != nil; k, v = cursor.Prev() {
|
|
|
|
itNum++
|
|
|
|
if itNum <= offset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var rule EventRule
|
|
|
|
rule, err = p.joinRuleAndActions(v, actionsBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rule.PrepareForRendering()
|
|
|
|
rules = append(rules, rule)
|
|
|
|
if len(rules) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) dumpEventRules() ([]EventRule, error) {
|
|
|
|
rules := make([]EventRule, 0, 50)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
rule, err := p.joinRuleAndActions(v, actionsBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getRecentlyUpdatedRules(after int64) ([]EventRule, error) {
|
|
|
|
if getLastRuleUpdate() < after {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rules := make([]EventRule, 0, 10)
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var rule EventRule
|
|
|
|
err := json.Unmarshal(v, &rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule.UpdatedAt < after {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var actions []EventAction
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
action := &rule.Actions[idx]
|
|
|
|
var baseAction BaseEventAction
|
|
|
|
k := actionsBucket.Get([]byte(action.Name))
|
|
|
|
if k == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(k, &baseAction)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
baseAction.Options.SetEmptySecretsIfNil()
|
|
|
|
action.BaseEventAction = baseAction
|
|
|
|
actions = append(actions, *action)
|
|
|
|
}
|
|
|
|
rule.Actions = actions
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) eventRuleExists(name string) (EventRule, error) {
|
|
|
|
var rule EventRule
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r := bucket.Get([]byte(name))
|
|
|
|
if r == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("event rule %q does not exist", name))
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rule, err = p.joinRuleAndActions(r, actionsBucket)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return rule, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addEventRule(rule *EventRule) error {
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r := bucket.Get([]byte(rule.Name)); r != nil {
|
|
|
|
return fmt.Errorf("event rule %q already exists", rule.Name)
|
|
|
|
}
|
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rule.ID = int64(id)
|
|
|
|
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, actionsBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(rule.Actions, func(i, j int) bool {
|
|
|
|
return rule.Actions[i].Order < rule.Actions[j].Order
|
|
|
|
})
|
|
|
|
buf, err := json.Marshal(rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte(rule.Name), buf)
|
|
|
|
if err == nil {
|
|
|
|
setLastRuleUpdate()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateEventRule(rule *EventRule) error {
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var r []byte
|
|
|
|
if r = bucket.Get([]byte(rule.Name)); r == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("event rule %q does not exist", rule.Name))
|
|
|
|
}
|
|
|
|
var oldRule EventRule
|
|
|
|
if err = json.Unmarshal(r, &oldRule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldRule.Actions {
|
|
|
|
if err = p.removeRuleFromActionMapping(rule.Name, oldRule.Actions[idx].Name, actionsBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
if err = p.addRuleToActionMapping(rule.Name, rule.Actions[idx].Name, actionsBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rule.ID = oldRule.ID
|
|
|
|
rule.CreatedAt = oldRule.CreatedAt
|
|
|
|
rule.UpdatedAt = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
buf, err := json.Marshal(rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sort.Slice(rule.Actions, func(i, j int) bool {
|
|
|
|
return rule.Actions[i].Order < rule.Actions[j].Order
|
|
|
|
})
|
|
|
|
err = bucket.Put([]byte(rule.Name), buf)
|
|
|
|
if err == nil {
|
|
|
|
setLastRuleUpdate()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteEventRule(rule EventRule, softDelete bool) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getRulesBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var r []byte
|
|
|
|
if r = bucket.Get([]byte(rule.Name)); r == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("event rule %q does not exist", rule.Name))
|
|
|
|
}
|
|
|
|
var oldRule EventRule
|
|
|
|
if err = json.Unmarshal(r, &oldRule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oldRule.Actions) > 0 {
|
|
|
|
actionsBucket, err := p.getActionsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldRule.Actions {
|
|
|
|
if err = p.removeRuleFromActionMapping(rule.Name, oldRule.Actions[idx].Name, actionsBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bucket.Delete([]byte(rule.Name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:48:55 +00:00
|
|
|
func (*BoltProvider) getTaskByName(name string) (Task, error) {
|
2022-07-11 06:17:36 +00:00
|
|
|
return Task{}, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:48:55 +00:00
|
|
|
func (*BoltProvider) addTask(name string) error {
|
2022-07-11 06:17:36 +00:00
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:48:55 +00:00
|
|
|
func (*BoltProvider) updateTask(name string, version int64) error {
|
2022-07-11 06:17:36 +00:00
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:48:55 +00:00
|
|
|
func (*BoltProvider) updateTaskTimestamp(name string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoltProvider) addNode() error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoltProvider) getNodeByName(name string) (Node, error) {
|
|
|
|
return Node{}, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoltProvider) getNodes() ([]Node, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoltProvider) updateNodeTimestamp() error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoltProvider) cleanupNodes() error {
|
2022-07-11 06:17:36 +00:00
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2022-08-21 17:01:08 +00:00
|
|
|
func (p *BoltProvider) setFirstDownloadTimestamp(username string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getUsersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to set download timestamp",
|
|
|
|
username))
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
err = json.Unmarshal(u, &user)
|
|
|
|
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())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) setFirstUploadTimestamp(username string) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := p.getUsersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var u []byte
|
|
|
|
if u = bucket.Get([]byte(username)); u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist, unable to set upload timestamp",
|
|
|
|
username))
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
if err = json.Unmarshal(u, &user); 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())
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) close() error {
|
2019-09-28 20:48:52 +00:00
|
|
|
return p.dbHandle.Close()
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) reloadConfig() error {
|
2020-02-02 21:20:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
// initializeDatabase does nothing, no initilization is needed for bolt provider
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) initializeDatabase() error {
|
2020-08-30 11:50:43 +00:00
|
|
|
return ErrNoInitRequired
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) migrateDatabase() error {
|
2020-02-08 13:44:25 +00:00
|
|
|
dbVersion, err := getBoltDatabaseVersion(p.dbHandle)
|
2019-11-13 10:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 07:37:50 +00:00
|
|
|
switch version := dbVersion.Version; {
|
|
|
|
case version == boltDatabaseVersion:
|
2022-10-28 12:28:37 +00:00
|
|
|
providerLog(logger.LevelDebug, "bolt database is up to date, current version: %d", version)
|
2020-10-05 17:42:33 +00:00
|
|
|
return ErrNoInitRequired
|
2022-10-28 12:28:37 +00:00
|
|
|
case version < 23:
|
|
|
|
err = fmt.Errorf("database schema version %d is too old, please see the upgrading docs", version)
|
2021-02-22 07:37:50 +00:00
|
|
|
providerLog(logger.LevelError, "%v", err)
|
|
|
|
logger.ErrorToConsole("%v", err)
|
|
|
|
return err
|
2020-11-26 21:08:33 +00:00
|
|
|
default:
|
2021-02-22 07:37:50 +00:00
|
|
|
if version > boltDatabaseVersion {
|
2022-10-28 12:28:37 +00:00
|
|
|
providerLog(logger.LevelError, "database schema version %d is newer than the supported one: %d", version,
|
2020-11-26 21:08:33 +00:00
|
|
|
boltDatabaseVersion)
|
2022-10-28 12:28:37 +00:00
|
|
|
logger.WarnToConsole("database schema version %d is newer than the supported one: %d", version,
|
2020-11-26 21:08:33 +00:00
|
|
|
boltDatabaseVersion)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-28 12:28:37 +00:00
|
|
|
return fmt.Errorf("database schema version not handled: %d", version)
|
2020-11-26 21:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) revertDatabase(targetVersion int) error {
|
2020-11-26 21:08:33 +00:00
|
|
|
dbVersion, err := getBoltDatabaseVersion(p.dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 07:37:50 +00:00
|
|
|
if dbVersion.Version == targetVersion {
|
|
|
|
return errors.New("current version match target version, nothing to do")
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
switch dbVersion.Version {
|
|
|
|
default:
|
2022-09-13 16:04:27 +00:00
|
|
|
return fmt.Errorf("database schema version not handled: %v", dbVersion.Version)
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:40:31 +00:00
|
|
|
func (p *BoltProvider) resetDatabase() error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
for _, bucketName := range boltBuckets {
|
|
|
|
err := tx.DeleteBucket(bucketName)
|
|
|
|
if err != nil && !errors.Is(err, bolt.ErrBucketNotFound) {
|
|
|
|
return fmt.Errorf("unable to remove bucket %v: %w", bucketName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *BoltProvider) joinRuleAndActions(r []byte, actionsBucket *bolt.Bucket) (EventRule, error) {
|
|
|
|
var rule EventRule
|
|
|
|
err := json.Unmarshal(r, &rule)
|
|
|
|
if err != nil {
|
|
|
|
return rule, err
|
|
|
|
}
|
|
|
|
var actions []EventAction
|
|
|
|
for idx := range rule.Actions {
|
|
|
|
action := &rule.Actions[idx]
|
|
|
|
var baseAction BaseEventAction
|
|
|
|
k := actionsBucket.Get([]byte(action.Name))
|
|
|
|
if k == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(k, &baseAction)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
baseAction.Options.SetEmptySecretsIfNil()
|
|
|
|
action.BaseEventAction = baseAction
|
|
|
|
actions = append(actions, *action)
|
|
|
|
}
|
|
|
|
rule.Actions = actions
|
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) joinGroupAndFolders(g []byte, foldersBucket *bolt.Bucket) (Group, error) {
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
if len(group.VirtualFolders) > 0 {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range group.VirtualFolders {
|
|
|
|
folder := &group.VirtualFolders[idx]
|
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name, foldersBucket)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
group.VirtualFolders = folders
|
|
|
|
}
|
|
|
|
group.SetEmptySecretsIfNil()
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) joinUserAndFolders(u []byte, foldersBucket *bolt.Bucket) (User, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var user User
|
|
|
|
err := json.Unmarshal(u, &user)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
if len(user.VirtualFolders) > 0 {
|
|
|
|
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
|
|
|
baseFolder, err := p.folderExistsInternal(folder.Name, foldersBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.BaseVirtualFolder = baseFolder
|
|
|
|
folders = append(folders, *folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
2020-06-07 21:30:18 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) groupExistsInternal(name string, bucket *bolt.Bucket) (Group, error) {
|
|
|
|
var group Group
|
|
|
|
g := bucket.Get([]byte(name))
|
|
|
|
if g == nil {
|
|
|
|
err := util.NewRecordNotFoundError(fmt.Sprintf("group %#v does not exist", name))
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) folderExistsInternal(name string, bucket *bolt.Bucket) (vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
f := bucket.Get([]byte(name))
|
|
|
|
if f == nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
err := util.NewRecordNotFoundError(fmt.Sprintf("folder %#v does not exist", name))
|
2020-06-07 21:30:18 +00:00
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(f, &folder)
|
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) addFolderInternal(folder vfs.BaseVirtualFolder, bucket *bolt.Bucket) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
id, err := bucket.NextSequence()
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return err
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
folder.ID = int64(id)
|
|
|
|
buf, err := json.Marshal(folder)
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return err
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return bucket.Put([]byte(folder.Name), buf)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
func (p *BoltProvider) addRuleToActionMapping(ruleName, actionName string, bucket *bolt.Bucket) error {
|
|
|
|
a := bucket.Get([]byte(actionName))
|
|
|
|
if a == nil {
|
|
|
|
return util.NewGenericError(fmt.Sprintf("action %q does not exist", actionName))
|
|
|
|
}
|
|
|
|
var action BaseEventAction
|
|
|
|
err := json.Unmarshal(a, &action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !util.Contains(action.Rules, ruleName) {
|
|
|
|
action.Rules = append(action.Rules, ruleName)
|
|
|
|
buf, err := json.Marshal(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(action.Name), buf)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) removeRuleFromActionMapping(ruleName, actionName string, bucket *bolt.Bucket) error {
|
|
|
|
a := bucket.Get([]byte(actionName))
|
|
|
|
if a == nil {
|
|
|
|
providerLog(logger.LevelWarn, "action %q does not exist, cannot remove from mapping", actionName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var action BaseEventAction
|
|
|
|
err := json.Unmarshal(a, &action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if util.Contains(action.Rules, ruleName) {
|
|
|
|
var rules []string
|
|
|
|
for _, r := range action.Rules {
|
|
|
|
if r != ruleName {
|
|
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action.Rules = util.RemoveDuplicates(rules, false)
|
|
|
|
buf, err := json.Marshal(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(action.Name), buf)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) addUserToGroupMapping(username, groupname string, bucket *bolt.Bucket) error {
|
|
|
|
g := bucket.Get([]byte(groupname))
|
|
|
|
if g == nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %q does not exist", groupname))
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(group.Users, username) {
|
2022-04-25 13:49:11 +00:00
|
|
|
group.Users = append(group.Users, username)
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) removeUserFromGroupMapping(username, groupname string, bucket *bolt.Bucket) error {
|
|
|
|
g := bucket.Get([]byte(groupname))
|
|
|
|
if g == nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %q does not exist", groupname))
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
var users []string
|
|
|
|
for _, u := range group.Users {
|
|
|
|
if u != username {
|
|
|
|
users = append(users, u)
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
}
|
|
|
|
group.Users = util.RemoveDuplicates(users, false)
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) addAdminToGroupMapping(username, groupname string, bucket *bolt.Bucket) error {
|
|
|
|
g := bucket.Get([]byte(groupname))
|
|
|
|
if g == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %q does not exist", groupname))
|
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !util.Contains(group.Admins, username) {
|
|
|
|
group.Admins = append(group.Admins, username)
|
2022-04-25 13:49:11 +00:00
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:04:27 +00:00
|
|
|
func (p *BoltProvider) removeAdminFromGroupMapping(username, groupname string, bucket *bolt.Bucket) error {
|
|
|
|
g := bucket.Get([]byte(groupname))
|
|
|
|
if g == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("group %q does not exist", groupname))
|
|
|
|
}
|
|
|
|
var group Group
|
|
|
|
err := json.Unmarshal(g, &group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var admins []string
|
|
|
|
for _, a := range group.Admins {
|
|
|
|
if a != username {
|
|
|
|
admins = append(admins, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.Admins = util.RemoveDuplicates(admins, false)
|
|
|
|
buf, err := json.Marshal(group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group.Name), buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) removeGroupFromAdminMapping(groupName, adminName string, bucket *bolt.Bucket) error {
|
|
|
|
var a []byte
|
|
|
|
if a = bucket.Get([]byte(adminName)); a == nil {
|
|
|
|
// the admin does not exist so there is no associated group
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var admin Admin
|
|
|
|
err := json.Unmarshal(a, &admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var newGroups []AdminGroupMapping
|
|
|
|
for _, g := range admin.Groups {
|
|
|
|
if g.Name != groupName {
|
|
|
|
newGroups = append(newGroups, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
admin.Groups = newGroups
|
|
|
|
buf, err := json.Marshal(admin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(adminName), buf)
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) addRelationToFolderMapping(baseFolder *vfs.BaseVirtualFolder, user *User, group *Group, bucket *bolt.Bucket) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
f := bucket.Get([]byte(baseFolder.Name))
|
|
|
|
if f == nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
// folder does not exists, try to create
|
2021-03-21 18:15:47 +00:00
|
|
|
baseFolder.LastQuotaUpdate = 0
|
|
|
|
baseFolder.UsedQuotaFiles = 0
|
|
|
|
baseFolder.UsedQuotaSize = 0
|
2022-04-25 13:49:11 +00:00
|
|
|
if user != nil {
|
|
|
|
baseFolder.Users = []string{user.Username}
|
|
|
|
}
|
|
|
|
if group != nil {
|
|
|
|
baseFolder.Groups = []string{group.Name}
|
|
|
|
}
|
|
|
|
return p.addFolderInternal(*baseFolder, bucket)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
var oldFolder vfs.BaseVirtualFolder
|
|
|
|
err := json.Unmarshal(f, &oldFolder)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
baseFolder.ID = oldFolder.ID
|
|
|
|
baseFolder.LastQuotaUpdate = oldFolder.LastQuotaUpdate
|
|
|
|
baseFolder.UsedQuotaFiles = oldFolder.UsedQuotaFiles
|
|
|
|
baseFolder.UsedQuotaSize = oldFolder.UsedQuotaSize
|
|
|
|
baseFolder.Users = oldFolder.Users
|
2022-04-25 13:49:11 +00:00
|
|
|
baseFolder.Groups = oldFolder.Groups
|
2022-05-19 17:49:51 +00:00
|
|
|
if user != nil && !util.Contains(baseFolder.Users, user.Username) {
|
2020-06-07 21:30:18 +00:00
|
|
|
baseFolder.Users = append(baseFolder.Users, user.Username)
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if group != nil && !util.Contains(baseFolder.Groups, group.Name) {
|
2022-04-25 13:49:11 +00:00
|
|
|
baseFolder.Groups = append(baseFolder.Groups, group.Name)
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
buf, err := json.Marshal(baseFolder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(baseFolder.Name), buf)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) removeRelationFromFolderMapping(folder vfs.VirtualFolder, username, groupname string,
|
|
|
|
bucket *bolt.Bucket,
|
|
|
|
) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
var f []byte
|
2021-02-01 18:04:15 +00:00
|
|
|
if f = bucket.Get([]byte(folder.Name)); f == nil {
|
2022-04-25 13:49:11 +00:00
|
|
|
// the folder does not exist so there is no associated user/group
|
2020-06-07 21:30:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var baseFolder vfs.BaseVirtualFolder
|
|
|
|
err := json.Unmarshal(f, &baseFolder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
found := false
|
2022-09-13 16:04:27 +00:00
|
|
|
if username != "" {
|
2022-04-25 13:49:11 +00:00
|
|
|
found = true
|
2020-06-07 21:30:18 +00:00
|
|
|
var newUserMapping []string
|
|
|
|
for _, u := range baseFolder.Users {
|
2022-04-25 13:49:11 +00:00
|
|
|
if u != username {
|
2020-06-07 21:30:18 +00:00
|
|
|
newUserMapping = append(newUserMapping, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseFolder.Users = newUserMapping
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
2022-09-13 16:04:27 +00:00
|
|
|
if groupname != "" {
|
2022-04-25 13:49:11 +00:00
|
|
|
found = true
|
|
|
|
var newGroupMapping []string
|
|
|
|
for _, g := range baseFolder.Groups {
|
|
|
|
if g != groupname {
|
|
|
|
newGroupMapping = append(newGroupMapping, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseFolder.Groups = newGroupMapping
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(baseFolder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(folder.Name), buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) updateUserRelations(tx *bolt.Tx, user *User, oldUser User) error {
|
2022-04-30 09:59:36 +00:00
|
|
|
foldersBucket, err := p.getFoldersBucket(tx)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupBucket, err := p.getGroupsBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range oldUser.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.removeRelationFromFolderMapping(oldUser.VirtualFolders[idx], oldUser.Username, "", foldersBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
for idx := range oldUser.Groups {
|
|
|
|
err = p.removeUserFromGroupMapping(user.Username, oldUser.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range user.VirtualFolders {
|
2022-04-30 09:59:36 +00:00
|
|
|
err = p.addRelationToFolderMapping(&user.VirtualFolders[idx].BaseVirtualFolder, user, nil, foldersBucket)
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx := range user.Groups {
|
|
|
|
err = p.addUserToGroupMapping(user.Username, user.Groups[idx].Name, groupBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func (p *BoltProvider) adminExistsInternal(tx *bolt.Tx, username string) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getAdminsBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a := bucket.Get([]byte(username))
|
|
|
|
if a == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("admin %v does not exist", username))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) userExistsInternal(tx *bolt.Tx, username string) error {
|
2022-04-25 13:49:11 +00:00
|
|
|
bucket, err := p.getUsersBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u := bucket.Get([]byte(username))
|
|
|
|
if u == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("username %#v does not exist", username))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) deleteRelatedShares(tx *bolt.Tx, username string) error {
|
|
|
|
bucket, err := p.getSharesBucket(tx)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var toRemove []string
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var share Share
|
|
|
|
err = json.Unmarshal(v, &share)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if share.Username == username {
|
|
|
|
toRemove = append(toRemove, share.ShareID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range toRemove {
|
|
|
|
if err := bucket.Delete([]byte(k)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) deleteRelatedAPIKey(tx *bolt.Tx, username string, scope APIKeyScope) error {
|
|
|
|
bucket, err := p.getAPIKeysBucket(tx)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var toRemove []string
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var apiKey APIKey
|
|
|
|
err = json.Unmarshal(v, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if scope == APIKeyScopeUser {
|
|
|
|
if apiKey.User == username {
|
|
|
|
toRemove = append(toRemove, apiKey.KeyID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if apiKey.Admin == username {
|
|
|
|
toRemove = append(toRemove, apiKey.KeyID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range toRemove {
|
|
|
|
if err := bucket.Delete([]byte(k)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) getSharesBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
2021-11-06 13:13:20 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
bucket := tx.Bucket(sharesBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
err = errors.New("unable to find shares bucket, bolt database structure not correcly defined")
|
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) getAPIKeysBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
2021-08-17 16:08:32 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
bucket := tx.Bucket(apiKeysBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
err = errors.New("unable to find api keys bucket, bolt database structure not correcly defined")
|
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) getAdminsBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
2021-01-17 21:29:08 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
bucket := tx.Bucket(adminsBucket)
|
|
|
|
if bucket == nil {
|
2021-08-17 16:08:32 +00:00
|
|
|
err = errors.New("unable to find admins bucket, bolt database structure not correcly defined")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) getUsersBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
2020-02-08 13:44:25 +00:00
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(usersBucket)
|
2021-01-17 21:29:08 +00:00
|
|
|
if bucket == nil {
|
2021-08-17 16:08:32 +00:00
|
|
|
err = errors.New("unable to find users bucket, bolt database structure not correcly defined")
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
return bucket, err
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func (p *BoltProvider) getGroupsBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(groupsBucket)
|
|
|
|
if bucket == nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
err = fmt.Errorf("unable to find groups bucket, bolt database structure not correcly defined")
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getFoldersBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(foldersBucket)
|
|
|
|
if bucket == nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
err = fmt.Errorf("unable to find folders bucket, bolt database structure not correcly defined")
|
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getActionsBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(actionsBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
err = fmt.Errorf("unable to find event actions bucket, bolt database structure not correcly defined")
|
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) getRulesBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
|
|
|
var err error
|
|
|
|
bucket := tx.Bucket(rulesBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
err = fmt.Errorf("unable to find event rules bucket, bolt database structure not correcly defined")
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
return bucket, err
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) {
|
|
|
|
var dbVersion schemaVersion
|
2019-11-13 10:36:21 +00:00
|
|
|
err := dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(dbVersionBucket)
|
|
|
|
if bucket == nil {
|
2022-09-13 16:04:27 +00:00
|
|
|
return fmt.Errorf("unable to find database schema version bucket")
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
v := bucket.Get(dbVersionKey)
|
|
|
|
if v == nil {
|
2020-02-08 13:44:25 +00:00
|
|
|
dbVersion = schemaVersion{
|
2022-10-28 12:28:37 +00:00
|
|
|
Version: 23,
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(v, &dbVersion)
|
|
|
|
})
|
|
|
|
return dbVersion, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 12:28:37 +00:00
|
|
|
/*func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error {
|
2019-11-13 10:36:21 +00:00
|
|
|
err := dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(dbVersionBucket)
|
|
|
|
if bucket == nil {
|
2022-09-13 16:04:27 +00:00
|
|
|
return fmt.Errorf("unable to find database schema version bucket")
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
newDbVersion := schemaVersion{
|
2019-11-13 10:36:21 +00:00
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(newDbVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put(dbVersionKey, buf)
|
|
|
|
})
|
|
|
|
return err
|
2022-10-28 12:28:37 +00:00
|
|
|
}*/
|