|
| 1 | +// Copyright 2025 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package planbuilder |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/go-mysql-server/sql" |
| 19 | + "github.com/dolthub/go-mysql-server/sql/mysql_db" |
| 20 | +) |
| 21 | + |
| 22 | +// mockDefiner temporarily impersonates the view definer during binding. It clones the current authorization state |
| 23 | +// (when available), adds the requested global privileges (e.g. CREATE VIEW), and updates both the session privilege |
| 24 | +// cache and the cached AuthorizationQueryState. Callers must defer the returned restore function. |
| 25 | +func (b *Builder) mockDefiner(privileges ...sql.PrivilegeType) func() { |
| 26 | + if b == nil || b.ctx == nil || b.ctx.Session == nil { |
| 27 | + return func() {} |
| 28 | + } |
| 29 | + |
| 30 | + var privilegeSet mysql_db.PrivilegeSet |
| 31 | + if state, ok := b.authQueryState.(defaultAuthorizationQueryState); ok && state.enabled { |
| 32 | + privilegeSet = state.privSet.Copy() |
| 33 | + } else { |
| 34 | + privilegeSet = mysql_db.NewPrivilegeSet() |
| 35 | + } |
| 36 | + privilegeSet.AddGlobalStatic(privileges...) |
| 37 | + |
| 38 | + initialAuthQueryState := b.authQueryState |
| 39 | + if state, ok := b.authQueryState.(defaultAuthorizationQueryState); ok { |
| 40 | + state.privSet = privilegeSet |
| 41 | + b.authQueryState = state |
| 42 | + } |
| 43 | + |
| 44 | + initialPrivilegeSet, initialCounter := b.ctx.Session.GetPrivilegeSet() |
| 45 | + b.ctx.SetPrivilegeSet(privilegeSet, initialCounter) |
| 46 | + |
| 47 | + return func() { |
| 48 | + b.authQueryState = initialAuthQueryState |
| 49 | + b.ctx.SetPrivilegeSet(initialPrivilegeSet, initialCounter) |
| 50 | + } |
| 51 | +} |
0 commit comments