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"
|
|
|
|
"time"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2019-08-12 16:31:31 +00:00
|
|
|
)
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
const (
|
2021-12-25 11:08:07 +00:00
|
|
|
boltDatabaseVersion = 15
|
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")
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
dbVersionBucket = []byte("db_version")
|
|
|
|
dbVersionKey = []byte("version")
|
2021-11-06 13:13:20 +00:00
|
|
|
boltBuckets = [][]byte{usersBucket, foldersBucket, adminsBucket, apiKeysBucket,
|
|
|
|
sharesBucket, 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
|
|
|
var user User
|
2021-01-05 08:50:22 +00:00
|
|
|
if password == "" {
|
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
|
|
|
}
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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
|
|
|
|
bucket, err := 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 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)
|
|
|
|
} 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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := 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)
|
|
|
|
if err == nil {
|
|
|
|
providerLog(logger.LevelDebug, "last login updated for user %#v", username)
|
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelWarn, "error updating last login for user %#v: %v", username, err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
bucket, err := getAdminsBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := 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 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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := 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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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())
|
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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteAdmin(admin *Admin) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := getAdminsBucket(tx)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket.Get([]byte(admin.Username)) == 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
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
if err := deleteRelatedAPIKey(tx, admin.Username, APIKeyScopeAdmin); err != nil {
|
|
|
|
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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := 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
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user, err = joinUserAndFolders(u, folderBucket)
|
|
|
|
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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := getFoldersBucket(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
|
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 {
|
|
|
|
err = addUserToFolderMapping(&user.VirtualFolders[idx].BaseVirtualFolder, user, folderBucket)
|
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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := getFoldersBucket(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
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range oldUser.VirtualFolders {
|
|
|
|
err = removeUserFromFolderMapping(&oldUser.VirtualFolders[idx], &oldUser, folderBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
err = addUserToFolderMapping(&user.VirtualFolders[idx].BaseVirtualFolder, user, folderBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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
|
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
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(user.Username), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) deleteUser(user *User) error {
|
2019-08-12 16:31:31 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
exists := bucket.Get([]byte(user.Username))
|
|
|
|
if exists == nil {
|
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("user %#v does not exist", user.Username))
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
if len(user.VirtualFolders) > 0 {
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
err = removeUserFromFolderMapping(&user.VirtualFolders[idx], user, folderBucket)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
|
|
|
|
if err := deleteRelatedAPIKey(tx, user.Username, APIKeyScopeUser); err != nil {
|
|
|
|
return err
|
2019-08-12 16:31:31 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if err := deleteRelatedShares(tx, user.Username); err != nil {
|
|
|
|
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 {
|
|
|
|
bucket, err := 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", 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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := getUsersBucket(tx)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := 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() {
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err := joinUserAndFolders(v, folderBucket)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
err = addCredentialsToUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:35:06 +00:00
|
|
|
// bolt provider cannot be shared, so we always return no recently updated users
|
|
|
|
func (p *BoltProvider) getRecentlyUpdatedUsers(after int64) ([]User, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func (p *BoltProvider) getUsersForQuotaCheck(toFetch map[string]bool) ([]User, error) {
|
|
|
|
users := make([]User, 0, 30)
|
|
|
|
|
|
|
|
err := p.dbHandle.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := getUsersBucket(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
foldersBucket, err := getFoldersBucket(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
|
|
|
|
}
|
|
|
|
needFolders, ok := toFetch[user.Username]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if needFolders && len(user.VirtualFolders) > 0 {
|
|
|
|
var folders []vfs.VirtualFolder
|
|
|
|
for idx := range user.VirtualFolders {
|
|
|
|
folder := &user.VirtualFolders[idx]
|
|
|
|
baseFolder, err := folderExistsInternal(folder.Name, foldersBucket)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.BaseVirtualFolder = baseFolder
|
|
|
|
folders = append(folders, *folder)
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
}
|
|
|
|
|
|
|
|
user.SetEmptySecretsIfNil()
|
|
|
|
user.PrepareForRendering()
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
bucket, err := getUsersBucket(tx)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
folderBucket, err := 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
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err := joinUserAndFolders(v, folderBucket)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err == nil {
|
2021-03-22 18:03:25 +00:00
|
|
|
user.PrepareForRendering()
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err := joinUserAndFolders(v, folderBucket)
|
2019-08-12 16:31:31 +00:00
|
|
|
if err == nil {
|
2021-03-22 18:03:25 +00:00
|
|
|
user.PrepareForRendering()
|
2020-11-22 20:53:04 +00:00
|
|
|
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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func (p *BoltProvider) getFolders(limit, offset int, order string) ([]vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, limit)
|
|
|
|
var err error
|
|
|
|
if limit <= 0 {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
err = p.dbHandle.View(func(tx *bolt.Tx) error {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
folder, err = folderExistsInternal(name, bucket)
|
|
|
|
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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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
|
2021-03-21 18:15:47 +00:00
|
|
|
return 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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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
|
|
|
|
buf, err := json.Marshal(folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(folder.Name), buf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (p *BoltProvider) deleteFolder(folder *vfs.BaseVirtualFolder) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := getFoldersBucket(tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
usersBucket, err := getUsersBucket(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(folder.Name)); f == nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewRecordNotFoundError(fmt.Sprintf("folder %v does not exist", folder.Name))
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = json.Unmarshal(f, &folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 {
|
2021-02-01 18:04:15 +00:00
|
|
|
if folder.Name != userFolder.Name {
|
2020-06-07 21:30:18 +00:00
|
|
|
folders = append(folders, userFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.VirtualFolders = folders
|
|
|
|
buf, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = usersBucket.Put([]byte(user.Username), buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-11 20:27:36 +00:00
|
|
|
bucket, err := 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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BoltProvider) deleteShare(share *Share) error {
|
|
|
|
return p.dbHandle.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
providerLog(logger.LevelDebug, "bolt database is up to date, current version: %v", version)
|
2020-10-05 17:42:33 +00:00
|
|
|
return ErrNoInitRequired
|
2022-01-09 11:25:53 +00:00
|
|
|
case version < 15:
|
2021-02-22 07:37:50 +00:00
|
|
|
err = fmt.Errorf("database version %v is too old, please see the upgrading docs", version)
|
|
|
|
providerLog(logger.LevelError, "%v", err)
|
|
|
|
logger.ErrorToConsole("%v", err)
|
|
|
|
return err
|
2022-01-30 10:42:36 +00:00
|
|
|
case version == 15:
|
|
|
|
return updateBoltDatabaseVersion(p.dbHandle, 16)
|
2020-11-26 21:08:33 +00:00
|
|
|
default:
|
2021-02-22 07:37:50 +00:00
|
|
|
if version > boltDatabaseVersion {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "database version %v is newer than the supported one: %v", version,
|
2020-11-26 21:08:33 +00:00
|
|
|
boltDatabaseVersion)
|
2021-02-22 07:37:50 +00:00
|
|
|
logger.WarnToConsole("database version %v is newer than the supported one: %v", version,
|
2020-11-26 21:08:33 +00:00
|
|
|
boltDatabaseVersion)
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("database version not handled: %v", 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 {
|
2022-01-30 10:42:36 +00:00
|
|
|
case 16:
|
|
|
|
return updateBoltDatabaseVersion(p.dbHandle, 15)
|
2021-08-17 16:08:32 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("database version not handled: %v", dbVersion.Version)
|
|
|
|
}
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func joinUserAndFolders(u []byte, foldersBucket *bolt.Bucket) (User, error) {
|
|
|
|
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]
|
2021-02-01 18:04:15 +00:00
|
|
|
baseFolder, err := 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func folderExistsInternal(name string, bucket *bolt.Bucket) (vfs.BaseVirtualFolder, error) {
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
f := bucket.Get([]byte(name))
|
|
|
|
if f == nil {
|
2021-07-11 13:26:51 +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
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func 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
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func addUserToFolderMapping(baseFolder *vfs.BaseVirtualFolder, user *User, bucket *bolt.Bucket) error {
|
|
|
|
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
|
|
|
|
baseFolder.Users = []string{user.Username}
|
|
|
|
return addFolderInternal(*baseFolder, bucket)
|
|
|
|
}
|
|
|
|
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
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsStringInSlice(user.Username, baseFolder.Users) {
|
2020-06-07 21:30:18 +00:00
|
|
|
baseFolder.Users = append(baseFolder.Users, user.Username)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func removeUserFromFolderMapping(folder *vfs.VirtualFolder, user *User, 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 {
|
2020-06-07 21:30:18 +00:00
|
|
|
// the folder does not exists so there is no associated user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var baseFolder vfs.BaseVirtualFolder
|
|
|
|
err := json.Unmarshal(f, &baseFolder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(user.Username, baseFolder.Users) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var newUserMapping []string
|
|
|
|
for _, u := range baseFolder.Users {
|
|
|
|
if u != user.Username {
|
|
|
|
newUserMapping = append(newUserMapping, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseFolder.Users = newUserMapping
|
|
|
|
buf, err := json.Marshal(baseFolder)
|
|
|
|
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
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func (p *BoltProvider) adminExistsInternal(tx *bolt.Tx, username string) error {
|
|
|
|
bucket, err := getAdminsBucket(tx)
|
|
|
|
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 {
|
|
|
|
bucket, err := getUsersBucket(tx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRelatedShares(tx *bolt.Tx, username string) error {
|
|
|
|
bucket, err := getSharesBucket(tx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
func deleteRelatedAPIKey(tx *bolt.Tx, username string, scope APIKeyScope) error {
|
|
|
|
bucket, err := getAPIKeysBucket(tx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func getSharesBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
func getAPIKeysBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-11 20:27:36 +00:00
|
|
|
func 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func 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
|
|
|
}
|
|
|
|
|
2021-06-11 20:27:36 +00:00
|
|
|
func 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 {
|
2021-08-17 16:08:32 +00:00
|
|
|
err = fmt.Errorf("unable to find folders buckets, 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 {
|
|
|
|
return fmt.Errorf("unable to find database version bucket")
|
|
|
|
}
|
|
|
|
v := bucket.Get(dbVersionKey)
|
|
|
|
if v == nil {
|
2020-02-08 13:44:25 +00:00
|
|
|
dbVersion = schemaVersion{
|
2022-01-09 11:25:53 +00:00
|
|
|
Version: 15,
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(v, &dbVersion)
|
|
|
|
})
|
|
|
|
return dbVersion, err
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +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 {
|
|
|
|
return fmt.Errorf("unable to find database version bucket")
|
|
|
|
}
|
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-01-30 10:42:36 +00:00
|
|
|
}
|