Skip to content

Commit

Permalink
*: Support "show grants for current_user();" (pingcap#5697)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli authored and winkyao committed Mar 27, 2018
1 parent 959c319 commit 0dd2ee5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ func (s *testSuite) TestShow2(c *C) {

tk.MustExec(`grant all on *.* to 'root'@'%'`)
tk.MustQuery("show grants").Check(testkit.Rows(`GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'`))

tk.MustQuery("show grants for current_user()").Check(testkit.Rows(`GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'`))
tk.MustQuery("show grants for current_user").Check(testkit.Rows(`GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'`))
}

func (s *testSuite) TestCollation(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -4759,6 +4759,10 @@ Username:
{
$$ = &auth.UserIdentity{Username: $1.(string), Hostname: strings.TrimPrefix($2, "@")}
}
| "CURRENT_USER" OptionalBraces
{
$$ = &auth.UserIdentity{CurrentUser: true}
}

UsernameList:
Username
Expand Down
5 changes: 3 additions & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{`SHOW FULL TABLES WHERE Table_Type != 'VIEW'`, true},
{`SHOW GRANTS`, true},
{`SHOW GRANTS FOR 'test'@'localhost'`, true},
{`SHOW GRANTS FOR current_user()`, true},
{`SHOW GRANTS FOR current_user`, true},
{`SHOW COLUMNS FROM City;`, true},
{`SHOW COLUMNS FROM tv189.1_t_1_x;`, true},
{`SHOW FIELDS FROM City;`, true},
Expand Down Expand Up @@ -2028,8 +2030,7 @@ func (s *testParserSuite) TestView(c *C) {
{"create or replace algorithm = merge definer = 'root' sql security invoker view v(a,b) as select * from t", true},
{"create or replace algorithm = merge definer = 'root' sql security invoker view v(a,b) as select * from t with local check option", true},
{"create or replace algorithm = merge definer = 'root' sql security invoker view v(a,b) as select * from t with cascaded check option", true},
// fixme: should be true
{"create or replace algorithm = merge definer = current_user view v as select * from t", false},
{"create or replace algorithm = merge definer = current_user view v as select * from t", true},
}
s.RunTest(c, table)

Expand Down
13 changes: 11 additions & 2 deletions plan/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type preprocessor struct {
ctx sessionctx.Context
err error
inPrepare bool
// When visiting create/drop table statement.
// inCreateOrDropTable is true when visiting create/drop table statement.
inCreateOrDropTable bool
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func checkColumn(colDef *ast.ColumnDef) error {
return nil
}

// isNowSymFunc checks whether default value is a NOW() builtin function.
// isDefaultValNowSymFunc checks whether defaul value is a NOW() builtin function.
func isDefaultValNowSymFunc(expr ast.ExprNode) bool {
if funcCall, ok := expr.(*ast.FuncCallExpr); ok {
// Default value NOW() is transformed to CURRENT_TIMESTAMP() in parser.
Expand All @@ -450,6 +450,7 @@ func isInvalidDefaultValue(colDef *ast.ColumnDef) bool {
return false
}

// isIncorrectName checks if the identifier is incorrect.
// See https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
func isIncorrectName(name string) bool {
if len(name) == 0 {
Expand Down Expand Up @@ -495,6 +496,14 @@ func (p *preprocessor) resolveShowStmt(node *ast.ShowStmt) {
} else if node.Table != nil && node.Table.Schema.L == "" {
node.Table.Schema = model.NewCIStr(node.DBName)
}
if node.User != nil && node.User.CurrentUser {
// Fill the Username and Hostname with the current user.
currentUser := p.ctx.GetSessionVars().User
if currentUser != nil {
node.User.Username = currentUser.Username
node.User.Hostname = currentUser.Hostname
}
}
}

func (p *preprocessor) resolveAlterTableStmt(node *ast.AlterTableStmt) {
Expand Down
5 changes: 3 additions & 2 deletions util/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (

// UserIdentity represents username and hostname.
type UserIdentity struct {
Username string
Hostname string
Username string
Hostname string
CurrentUser bool
}

// String converts UserIdentity to the format user@host.
Expand Down

0 comments on commit 0dd2ee5

Please sign in to comment.