2019-07-20 10:26:52 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2019-09-14 14:18:31 +00:00
|
|
|
"context"
|
2019-07-20 10:26:52 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-06-07 21:30:18 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-02-23 10:30:26 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
const (
|
2020-07-08 16:54:44 +00:00
|
|
|
sqlDatabaseVersion = 4
|
|
|
|
initialDBVersionSQL = "INSERT INTO {{schema_version}} (version) VALUES (1);"
|
|
|
|
defaultSQLQueryTimeout = 10 * time.Second
|
|
|
|
longSQLQueryTimeout = 60 * time.Second
|
2020-02-08 13:44:25 +00:00
|
|
|
)
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
var errSQLFoldersAssosaction = errors.New("unable to associate virtual folders to user")
|
|
|
|
|
|
|
|
type sqlQuerier interface {
|
2020-07-08 16:54:44 +00:00
|
|
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUserByUsername(username string, dbHandle sqlQuerier) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getUserByUsernameQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, username)
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err = getUserFromDbRow(row, nil)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return getUserWithVirtualFolders(user, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:36:12 +00:00
|
|
|
func sqlCommonValidateUserAndPass(username, password, ip, protocol string, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
if len(password) == 0 {
|
|
|
|
return user, errors.New("Credentials cannot be null or empty")
|
|
|
|
}
|
2019-08-11 12:53:37 +00:00
|
|
|
user, err := getUserByUsername(username, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user: %v, error: %v", username, err)
|
2019-08-12 16:31:31 +00:00
|
|
|
return user, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-08-19 17:36:12 +00:00
|
|
|
return checkUserAndPass(user, password, ip, protocol)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
func sqlCommonValidateUserAndPubKey(username string, pubKey []byte, dbHandle *sql.DB) (User, string, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
|
|
|
if len(pubKey) == 0 {
|
2019-09-05 19:35:53 +00:00
|
|
|
return user, "", errors.New("Credentials cannot be null or empty")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-11 12:53:37 +00:00
|
|
|
user, err := getUserByUsername(username, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error authenticating user: %v, error: %v", username, err)
|
2019-09-05 19:35:53 +00:00
|
|
|
return user, "", err
|
2019-07-20 19:17:53 +00:00
|
|
|
}
|
2019-08-12 16:31:31 +00:00
|
|
|
return checkUserAndPubKey(user, pubKey)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 16:45:36 +00:00
|
|
|
func sqlCommonCheckAvailability(dbHandle *sql.DB) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
2019-09-14 14:18:31 +00:00
|
|
|
defer cancel()
|
|
|
|
return dbHandle.PingContext(ctx)
|
2019-09-13 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonGetUserByID(ID int64, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getUserByIDQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, ID)
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err = getUserFromDbRow(row, nil)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return getUserWithVirtualFolders(user, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonUpdateQuota(username string, filesAdd int, sizeAdd int64, reset bool, dbHandle *sql.DB) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-28 11:24:46 +00:00
|
|
|
q := getUpdateQuotaQuery(reset)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, sizeAdd, filesAdd, utils.GetTimeAsMsSinceEpoch(time.Now()), username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2019-11-13 10:36:21 +00:00
|
|
|
providerLog(logger.LevelDebug, "quota updated for user %#v, files increment: %v size increment: %v is reset? %v",
|
2019-07-28 11:24:46 +00:00
|
|
|
username, filesAdd, sizeAdd, reset)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2019-11-13 10:36:21 +00:00
|
|
|
providerLog(logger.LevelWarn, "error updating quota for user %#v: %v", username, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonGetUsedQuota(username string, dbHandle *sql.DB) (int, int64, error) {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getQuotaQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
var usedFiles int
|
|
|
|
var usedSize int64
|
2020-07-08 16:54:44 +00:00
|
|
|
err = stmt.QueryRowContext(ctx, username).Scan(&usedSize, &usedFiles)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error getting quota for user: %v, error: %v", username, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return usedFiles, usedSize, err
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func sqlCommonUpdateLastLogin(username string, dbHandle *sql.DB) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getUpdateLastLoginQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, utils.GetTimeAsMsSinceEpoch(time.Now()), username)
|
2020-06-07 21:30:18 +00:00
|
|
|
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-08-11 12:53:37 +00:00
|
|
|
func sqlCommonCheckUserExists(username string, dbHandle *sql.DB) (User, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user User
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getUserByUsernameQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, username)
|
2020-06-07 21:30:18 +00:00
|
|
|
user, err = getUserFromDbRow(row, nil)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return getUserWithVirtualFolders(user, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonAddUser(user User, dbHandle *sql.DB) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
tx, err := dbHandle.BeginTx(ctx, nil)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getAddUserQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := tx.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
permissions, err := user.GetPermissionsAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
publicKeys, err := user.GetPublicKeysAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-08-01 20:42:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
filters, err := user.GetFiltersAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-12-30 17:37:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
fsConfig, err := user.GetFsConfigAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2020-01-19 06:41:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, user.Username, user.Password, string(publicKeys), user.HomeDir, user.UID, user.GID, user.MaxSessions, user.QuotaSize,
|
2020-06-07 21:30:18 +00:00
|
|
|
user.QuotaFiles, string(permissions), user.UploadBandwidth, user.DownloadBandwidth, user.Status, user.ExpirationDate, string(filters),
|
|
|
|
string(fsConfig))
|
2020-02-23 10:30:26 +00:00
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2020-02-23 10:30:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
err = generateVirtualFoldersMapping(ctx, user, tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonUpdateUser(user User, dbHandle *sql.DB) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
err := validateUser(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
tx, err := dbHandle.BeginTx(ctx, nil)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getUpdateUserQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := tx.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
permissions, err := user.GetPermissionsAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-01 20:42:46 +00:00
|
|
|
publicKeys, err := user.GetPublicKeysAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-08-01 20:42:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
filters, err := user.GetFiltersAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2019-12-30 17:37:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
fsConfig, err := user.GetFsConfigAsJSON()
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2020-01-19 06:41:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, user.Password, string(publicKeys), user.HomeDir, user.UID, user.GID, user.MaxSessions, user.QuotaSize,
|
2020-06-07 21:30:18 +00:00
|
|
|
user.QuotaFiles, string(permissions), user.UploadBandwidth, user.DownloadBandwidth, user.Status, user.ExpirationDate,
|
|
|
|
string(filters), string(fsConfig), user.ID)
|
2020-02-23 10:30:26 +00:00
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sqlCommonRollbackTransaction(tx)
|
2020-02-23 10:30:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
err = generateVirtualFoldersMapping(ctx, user, tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func sqlCommonDeleteUser(user User, dbHandle *sql.DB) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getDeleteUserQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, user.ID)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func sqlCommonDumpUsers(dbHandle sqlQuerier) ([]User, error) {
|
|
|
|
users := make([]User, 0, 100)
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-12-27 22:12:44 +00:00
|
|
|
q := getDumpUsersQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return users, err
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
u, err := getUserFromDbRow(nil, rows)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
err = addCredentialsToUser(&u)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
users = append(users, u)
|
|
|
|
}
|
|
|
|
return getUsersWithVirtualFolders(users, dbHandle)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func sqlCommonGetUsers(limit int, offset int, order string, username string, dbHandle sqlQuerier) ([]User, error) {
|
|
|
|
users := make([]User, 0, limit)
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2019-07-20 10:26:52 +00:00
|
|
|
q := getUsersQuery(order, username)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
var rows *sql.Rows
|
|
|
|
if len(username) > 0 {
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err = stmt.QueryContext(ctx, username, limit, offset) //nolint:rowserrcheck // rows.Err() is checked
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err = stmt.QueryContext(ctx, limit, offset) //nolint:rowserrcheck // rows.Err() is checked
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
u, err := getUserFromDbRow(nil, rows)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return users, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
users = append(users, HideUserSensitiveData(&u))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
return getUsersWithVirtualFolders(users, dbHandle)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
func updateUserPermissionsFromDb(user *User, permissions string) error {
|
|
|
|
var err error
|
|
|
|
perms := make(map[string][]string)
|
|
|
|
err = json.Unmarshal([]byte(permissions), &perms)
|
|
|
|
if err == nil {
|
|
|
|
user.Permissions = perms
|
|
|
|
} else {
|
|
|
|
// compatibility layer: until version 0.9.4 permissions were a string list
|
|
|
|
var list []string
|
|
|
|
err = json.Unmarshal([]byte(permissions), &list)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
perms["/"] = list
|
|
|
|
user.Permissions = perms
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func getUserFromDbRow(row *sql.Row, rows *sql.Rows) (User, error) {
|
|
|
|
var user User
|
|
|
|
var permissions sql.NullString
|
|
|
|
var password sql.NullString
|
|
|
|
var publicKey sql.NullString
|
2019-12-30 17:37:50 +00:00
|
|
|
var filters sql.NullString
|
2020-01-19 06:41:05 +00:00
|
|
|
var fsConfig sql.NullString
|
2019-07-20 10:26:52 +00:00
|
|
|
var err error
|
|
|
|
if row != nil {
|
|
|
|
err = row.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions,
|
2019-07-28 17:29:32 +00:00
|
|
|
&user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate,
|
2020-06-07 21:30:18 +00:00
|
|
|
&user.UploadBandwidth, &user.DownloadBandwidth, &user.ExpirationDate, &user.LastLogin, &user.Status, &filters, &fsConfig)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
|
|
|
err = rows.Scan(&user.ID, &user.Username, &password, &publicKey, &user.HomeDir, &user.UID, &user.GID, &user.MaxSessions,
|
2019-07-28 17:29:32 +00:00
|
|
|
&user.QuotaSize, &user.QuotaFiles, &permissions, &user.UsedQuotaSize, &user.UsedQuotaFiles, &user.LastQuotaUpdate,
|
2020-06-07 21:30:18 +00:00
|
|
|
&user.UploadBandwidth, &user.DownloadBandwidth, &user.ExpirationDate, &user.LastLogin, &user.Status, &filters, &fsConfig)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-08-12 16:31:31 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return user, &RecordNotFoundError{err: err.Error()}
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
if password.Valid {
|
|
|
|
user.Password = password.String
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
// we can have a empty string or an invalid json in null string
|
|
|
|
// so we do a relaxed test if the field is optional, for example we
|
|
|
|
// populate public keys only if unmarshal does not return an error
|
2019-07-20 10:26:52 +00:00
|
|
|
if publicKey.Valid {
|
2019-08-01 20:42:46 +00:00
|
|
|
var list []string
|
|
|
|
err = json.Unmarshal([]byte(publicKey.String), &list)
|
|
|
|
if err == nil {
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = list
|
2019-08-01 20:42:46 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if permissions.Valid {
|
2020-02-23 10:30:26 +00:00
|
|
|
err = updateUserPermissionsFromDb(&user, permissions.String)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if filters.Valid {
|
|
|
|
var userFilters UserFilters
|
|
|
|
err = json.Unmarshal([]byte(filters.String), &userFilters)
|
|
|
|
if err == nil {
|
|
|
|
user.Filters = userFilters
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
if fsConfig.Valid {
|
|
|
|
var fs Filesystem
|
|
|
|
err = json.Unmarshal([]byte(fsConfig.String), &fs)
|
|
|
|
if err == nil {
|
|
|
|
user.FsConfig = fs
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonCheckFolderExists(ctx context.Context, name string, dbHandle sqlQuerier) (vfs.BaseVirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
q := getFolderByPathQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, name)
|
2020-06-07 21:30:18 +00:00
|
|
|
err = row.Scan(&folder.ID, &folder.MappedPath, &folder.UsedQuotaSize, &folder.UsedQuotaFiles, &folder.LastQuotaUpdate)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return folder, &RecordNotFoundError{err: err.Error()}
|
|
|
|
}
|
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonAddOrGetFolder(ctx context.Context, name string, usedQuotaSize int64, usedQuotaFiles int, lastQuotaUpdate int64, dbHandle sqlQuerier) (vfs.BaseVirtualFolder, error) {
|
|
|
|
folder, err := sqlCommonCheckFolderExists(ctx, name, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if _, ok := err.(*RecordNotFoundError); ok {
|
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: name,
|
|
|
|
UsedQuotaSize: usedQuotaSize,
|
|
|
|
UsedQuotaFiles: usedQuotaFiles,
|
|
|
|
LastQuotaUpdate: lastQuotaUpdate,
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
err = sqlCommonAddFolder(f, dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return folder, err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
return sqlCommonCheckFolderExists(ctx, name, dbHandle)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
return folder, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonAddFolder(folder vfs.BaseVirtualFolder, dbHandle sqlQuerier) error {
|
|
|
|
err := validateFolder(&folder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getAddFolderQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, folder.MappedPath, folder.UsedQuotaSize, folder.UsedQuotaFiles, folder.LastQuotaUpdate)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonDeleteFolder(folder vfs.BaseVirtualFolder, dbHandle sqlQuerier) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getDeleteFolderQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, folder.ID)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonDumpFolders(dbHandle sqlQuerier) ([]vfs.BaseVirtualFolder, error) {
|
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, 50)
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getDumpFoldersQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = rows.Scan(&folder.ID, &folder.MappedPath, &folder.UsedQuotaSize, &folder.UsedQuotaFiles, &folder.LastQuotaUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
folders = append(folders, folder)
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
return getVirtualFoldersWithUsers(folders, dbHandle)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonGetFolders(limit, offset int, order, folderPath string, dbHandle sqlQuerier) ([]vfs.BaseVirtualFolder, error) {
|
|
|
|
folders := make([]vfs.BaseVirtualFolder, 0, limit)
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getFoldersQuery(order, folderPath)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
var rows *sql.Rows
|
|
|
|
if len(folderPath) > 0 {
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err = stmt.QueryContext(ctx, folderPath, limit, offset) //nolint:rowserrcheck // rows.Err() is checked
|
2020-06-07 21:30:18 +00:00
|
|
|
} else {
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err = stmt.QueryContext(ctx, limit, offset) //nolint:rowserrcheck // rows.Err() is checked
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err = rows.Scan(&folder.ID, &folder.MappedPath, &folder.UsedQuotaSize, &folder.UsedQuotaFiles, &folder.LastQuotaUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
folders = append(folders, folder)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
return getVirtualFoldersWithUsers(folders, dbHandle)
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonClearFolderMapping(ctx context.Context, user User, dbHandle sqlQuerier) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getClearFolderMappingQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, user.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonAddFolderMapping(ctx context.Context, user User, folder vfs.VirtualFolder, dbHandle sqlQuerier) error {
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getAddFolderMappingQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, folder.VirtualPath, folder.QuotaSize, folder.QuotaFiles, folder.ID, user.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func generateVirtualFoldersMapping(ctx context.Context, user User, dbHandle sqlQuerier) error {
|
|
|
|
err := sqlCommonClearFolderMapping(ctx, user, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, vfolder := range user.VirtualFolders {
|
2020-07-08 16:54:44 +00:00
|
|
|
f, err := sqlCommonAddOrGetFolder(ctx, vfolder.MappedPath, 0, 0, 0, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vfolder.BaseVirtualFolder = f
|
2020-07-08 16:54:44 +00:00
|
|
|
err = sqlCommonAddFolderMapping(ctx, user, vfolder, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserWithVirtualFolders(user User, dbHandle sqlQuerier) (User, error) {
|
|
|
|
users, err := getUsersWithVirtualFolders([]User{user}, dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
if len(users) == 0 {
|
|
|
|
return user, errSQLFoldersAssosaction
|
|
|
|
}
|
|
|
|
return users[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUsersWithVirtualFolders(users []User, dbHandle sqlQuerier) ([]User, error) {
|
|
|
|
var err error
|
|
|
|
usersVirtualFolders := make(map[int64][]vfs.VirtualFolder)
|
|
|
|
if len(users) == 0 {
|
|
|
|
return users, err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getRelatedFoldersForUsersQuery(users)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var folder vfs.VirtualFolder
|
|
|
|
var userID int64
|
|
|
|
err = rows.Scan(&folder.ID, &folder.MappedPath, &folder.UsedQuotaSize, &folder.UsedQuotaFiles,
|
|
|
|
&folder.LastQuotaUpdate, &folder.VirtualPath, &folder.QuotaSize, &folder.QuotaFiles, &userID)
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
usersVirtualFolders[userID] = append(usersVirtualFolders[userID], folder)
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
if len(usersVirtualFolders) == 0 {
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
for idx := range users {
|
|
|
|
ref := &users[idx]
|
|
|
|
ref.VirtualFolders = usersVirtualFolders[ref.ID]
|
|
|
|
}
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVirtualFoldersWithUsers(folders []vfs.BaseVirtualFolder, dbHandle sqlQuerier) ([]vfs.BaseVirtualFolder, error) {
|
|
|
|
var err error
|
|
|
|
vFoldersUsers := make(map[int64][]string)
|
|
|
|
if len(folders) == 0 {
|
|
|
|
return folders, err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getRelatedUsersForFoldersQuery(folders)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var username string
|
|
|
|
var folderID int64
|
|
|
|
err = rows.Scan(&folderID, &username)
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
vFoldersUsers[folderID] = append(vFoldersUsers[folderID], username)
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
if len(vFoldersUsers) == 0 {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
for idx := range folders {
|
|
|
|
ref := &folders[idx]
|
|
|
|
ref.Users = vFoldersUsers[ref.ID]
|
|
|
|
}
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonUpdateFolderQuota(mappedPath string, filesAdd int, sizeAdd int64, reset bool, dbHandle *sql.DB) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getUpdateFolderQuotaQuery(reset)
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, sizeAdd, filesAdd, utils.GetTimeAsMsSinceEpoch(time.Now()), mappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
providerLog(logger.LevelDebug, "quota updated for folder %#v, files increment: %v size increment: %v is reset? %v",
|
|
|
|
mappedPath, filesAdd, sizeAdd, reset)
|
|
|
|
} else {
|
|
|
|
providerLog(logger.LevelWarn, "error updating quota for folder %#v: %v", mappedPath, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonGetFolderUsedQuota(mappedPath string, dbHandle *sql.DB) (int, int64, error) {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getQuotaFolderQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
var usedFiles int
|
|
|
|
var usedSize int64
|
2020-07-08 16:54:44 +00:00
|
|
|
err = stmt.QueryRowContext(ctx, mappedPath).Scan(&usedSize, &usedFiles)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error getting quota for folder: %v, error: %v", mappedPath, err)
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return usedFiles, usedSize, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-04-30 12:23:55 +00:00
|
|
|
func sqlCommonRollbackTransaction(tx *sql.Tx) {
|
|
|
|
err := tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error rolling back transaction: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 11:50:43 +00:00
|
|
|
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB, showInitWarn bool) (schemaVersion, error) {
|
2020-02-08 13:44:25 +00:00
|
|
|
var result schemaVersion
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-02-08 13:44:25 +00:00
|
|
|
q := getDatabaseVersionQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-02-08 13:44:25 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
2020-08-30 11:50:43 +00:00
|
|
|
if showInitWarn && strings.Contains(err.Error(), sqlTableSchemaVersion) {
|
2020-07-09 18:01:37 +00:00
|
|
|
logger.WarnToConsole("database query error, did you forgot to run the \"initprovider\" command?")
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
row := stmt.QueryRowContext(ctx)
|
2020-02-08 13:44:25 +00:00
|
|
|
err = row.Scan(&result.Version)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonUpdateDatabaseVersion(ctx context.Context, dbHandle sqlQuerier, version int) error {
|
2020-02-08 13:44:25 +00:00
|
|
|
q := getUpdateDBVersionQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-02-08 13:44:25 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, version)
|
2020-02-08 13:44:25 +00:00
|
|
|
return err
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func sqlCommonExecSQLAndUpdateDBVersion(dbHandle *sql.DB, sql []string, newVersion int) error {
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
tx, err := dbHandle.BeginTx(ctx, nil)
|
2020-02-23 10:30:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, q := range sql {
|
|
|
|
if len(strings.TrimSpace(q)) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = tx.ExecContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
err = sqlCommonUpdateDatabaseVersion(ctx, tx, newVersion)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonGetCompatVirtualFolders(dbHandle *sql.DB) ([]userCompactVFolders, error) {
|
|
|
|
users := []userCompactVFolders{}
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
|
|
|
defer cancel()
|
2020-06-07 21:30:18 +00:00
|
|
|
q := getCompatVirtualFoldersQuery()
|
2020-07-08 16:54:44 +00:00
|
|
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
defer stmt.Close()
|
2020-07-08 16:54:44 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var user userCompactVFolders
|
|
|
|
var virtualFolders sql.NullString
|
|
|
|
err = rows.Scan(&user.ID, &user.Username, &virtualFolders)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if virtualFolders.Valid {
|
|
|
|
var list []virtualFoldersCompact
|
|
|
|
err = json.Unmarshal([]byte(virtualFolders.String), &list)
|
|
|
|
if err == nil && len(list) > 0 {
|
|
|
|
user.VirtualFolders = list
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users, rows.Err()
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:54:44 +00:00
|
|
|
func sqlCommonRestoreCompatVirtualFolders(ctx context.Context, users []userCompactVFolders, dbHandle sqlQuerier) ([]string, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
foldersToScan := []string{}
|
|
|
|
for _, user := range users {
|
|
|
|
for _, vfolder := range user.VirtualFolders {
|
|
|
|
providerLog(logger.LevelInfo, "restoring virtual folder: %+v for user %#v", vfolder, user.Username)
|
|
|
|
// -1 means included in user quota, 0 means unlimited
|
|
|
|
quotaSize := int64(-1)
|
|
|
|
quotaFiles := -1
|
|
|
|
if vfolder.ExcludeFromQuota {
|
|
|
|
quotaFiles = 0
|
|
|
|
quotaSize = 0
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
b, err := sqlCommonAddOrGetFolder(ctx, vfolder.MappedPath, 0, 0, 0, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error restoring virtual folder for user %#v: %v", user.Username, err)
|
|
|
|
return foldersToScan, err
|
|
|
|
}
|
|
|
|
u := User{
|
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
}
|
|
|
|
f := vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: b,
|
|
|
|
VirtualPath: vfolder.VirtualPath,
|
|
|
|
QuotaSize: quotaSize,
|
|
|
|
QuotaFiles: quotaFiles,
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
err = sqlCommonAddFolderMapping(ctx, u, f, dbHandle)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelWarn, "error adding virtual folder mapping for user %#v: %v", user.Username, err)
|
|
|
|
return foldersToScan, err
|
|
|
|
}
|
|
|
|
if !utils.IsStringInSlice(vfolder.MappedPath, foldersToScan) {
|
|
|
|
foldersToScan = append(foldersToScan, vfolder.MappedPath)
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelInfo, "virtual folder: %+v for user %#v successfully restored", vfolder, user.Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return foldersToScan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlCommonUpdateDatabaseFrom3To4(sqlV4 string, dbHandle *sql.DB) error {
|
2020-10-05 17:42:33 +00:00
|
|
|
logger.InfoToConsole("updating database version: 3 -> 4")
|
2020-06-07 21:30:18 +00:00
|
|
|
providerLog(logger.LevelInfo, "updating database version: 3 -> 4")
|
|
|
|
users, err := sqlCommonGetCompatVirtualFolders(dbHandle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sql := strings.ReplaceAll(sqlV4, "{{users}}", sqlTableUsers)
|
|
|
|
sql = strings.ReplaceAll(sql, "{{folders}}", sqlTableFolders)
|
|
|
|
sql = strings.ReplaceAll(sql, "{{folders_mapping}}", sqlTableFoldersMapping)
|
2020-07-08 16:54:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
tx, err := dbHandle.BeginTx(ctx, nil)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, q := range strings.Split(sql, ";") {
|
|
|
|
if len(strings.TrimSpace(q)) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
_, err = tx.ExecContext(ctx, q)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
foldersToScan, err := sqlCommonRestoreCompatVirtualFolders(ctx, users, tx)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 16:54:44 +00:00
|
|
|
err = sqlCommonUpdateDatabaseVersion(ctx, tx, 4)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sqlCommonRollbackTransaction(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err == nil {
|
|
|
|
go updateVFoldersQuotaAfterRestore(foldersToScan)
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
return err
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|