Skip to content

Commit

Permalink
Merge pull request #8639 from dolthub/daylon/fix-for
Browse files Browse the repository at this point in the history
Fix for updated issue 8623
  • Loading branch information
Hydrocharged authored Dec 5, 2024
2 parents 272813a + 85d2545 commit 77c3bf6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions go/libraries/doltcore/branch_control/branch_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
goerrors "errors"
"fmt"
"os"
"strings"
"sync/atomic"

flatbuffers "github.com/dolthub/flatbuffers/v23/go"
Expand Down Expand Up @@ -243,7 +244,7 @@ func CheckAccess(ctx context.Context, flags Permissions) error {

user := branchAwareSession.GetUser()
host := branchAwareSession.GetHost()
database := branchAwareSession.GetCurrentDatabase()
database := getDatabaseNameOnly(branchAwareSession.GetCurrentDatabase())
branch, err := branchAwareSession.GetBranch()
if err != nil {
return err
Expand Down Expand Up @@ -277,7 +278,7 @@ func CanCreateBranch(ctx context.Context, branchName string) error {

user := branchAwareSession.GetUser()
host := branchAwareSession.GetHost()
database := branchAwareSession.GetCurrentDatabase()
database := getDatabaseNameOnly(branchAwareSession.GetCurrentDatabase())
if controller.Namespace.CanCreate(database, branchName, user, host) {
return nil
}
Expand All @@ -304,7 +305,7 @@ func CanDeleteBranch(ctx context.Context, branchName string) error {

user := branchAwareSession.GetUser()
host := branchAwareSession.GetHost()
database := branchAwareSession.GetCurrentDatabase()
database := getDatabaseNameOnly(branchAwareSession.GetCurrentDatabase())
// Get the permissions for the branch, user, and host combination
_, perms := controller.Access.Match(database, branchName, user, host)
// If the user has the write or admin flags, then we allow access
Expand All @@ -329,7 +330,7 @@ func AddAdminForContext(ctx context.Context, branchName string) error {

user := branchAwareSession.GetUser()
host := branchAwareSession.GetHost()
database := branchAwareSession.GetCurrentDatabase()
database := getDatabaseNameOnly(branchAwareSession.GetCurrentDatabase())
// Check if we already have admin permissions for the given branch, as there's no need to do another insertion if so
controller.Access.RWMutex.RLock()
_, modPerms := controller.Access.Match(database, branchName, user, host)
Expand Down Expand Up @@ -381,3 +382,10 @@ func HasDatabasePrivileges(ctx Context, database string) bool {
sql.PrivilegeType_Insert, sql.PrivilegeType_Update, sql.PrivilegeType_Delete, sql.PrivilegeType_Execute, sql.PrivilegeType_GrantOption)
return hasSuper || isGlobalAdmin || isDatabaseAdmin
}

// getDatabaseNameOnly gets the database name only, which is useful for when the database name includes a revision.
// This is a direct reimplementation of the logic in dsess.SplitRevisionDbName, however we cannot use that function due
// to import cycles.
func getDatabaseNameOnly(dbName string) string {
return strings.SplitN(dbName, "/", 2)[0]
}
5 changes: 5 additions & 0 deletions integration-tests/bats/branch-control.bats
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ SQL
@test "branch-control: Issue #8623" {
# https://github.com/dolthub/dolt/issues/8623
dolt sql <<SQL
DELETE FROM dolt_branch_namespace_control WHERE true;
DELETE FROM dolt_branch_control WHERE true;
DROP USER IF EXISTS 'tadmin'@'%';
DROP USER IF EXISTS 'ttask'@'%';
DROP DATABASE IF EXISTS t;
CREATE DATABASE t;
CREATE USER 'ttask'@'%' IDENTIFIED BY 't';
GRANT ALL ON t.* TO 'ttask'@'%';
Expand Down

0 comments on commit 77c3bf6

Please sign in to comment.