diff --git a/internal/dataprovider/sqlcommon.go b/internal/dataprovider/sqlcommon.go index c0ccd369..a873c1ba 100644 --- a/internal/dataprovider/sqlcommon.go +++ b/internal/dataprovider/sqlcommon.go @@ -987,6 +987,12 @@ func sqlCommonAddUser(user *User, dbHandle *sql.DB) error { defer cancel() return sqlCommonExecuteTx(ctx, dbHandle, func(tx *sql.Tx) error { + if config.IsShared == 1 { + _, err := tx.ExecContext(ctx, getRemoveSoftDeletedUserQuery(), user.Username) + if err != nil { + return err + } + } q := getAddUserQuery() _, err := tx.ExecContext(ctx, q, user.Username, user.Password, string(publicKeys), user.HomeDir, user.UID, user.GID, user.MaxSessions, user.QuotaSize, user.QuotaFiles, string(permissions), user.UploadBandwidth, diff --git a/internal/dataprovider/sqlqueries.go b/internal/dataprovider/sqlqueries.go index 870b7ec3..b2230897 100644 --- a/internal/dataprovider/sqlqueries.go +++ b/internal/dataprovider/sqlqueries.go @@ -527,6 +527,10 @@ func getDeleteUserQuery(softDelete bool) string { return fmt.Sprintf(`DELETE FROM %s WHERE id = %s`, sqlTableUsers, sqlPlaceholders[0]) } +func getRemoveSoftDeletedUserQuery() string { + return fmt.Sprintf(`DELETE FROM %s WHERE username = %s AND deleted_at > 0`, sqlTableUsers, sqlPlaceholders[0]) +} + func getFolderByNameQuery() string { return fmt.Sprintf(`SELECT %s FROM %s WHERE name = %s`, selectFolderFields, sqlTableFolders, sqlPlaceholders[0]) }