Skip to content

Commit

Permalink
privilege: fix select current_role() error (pingcap#15534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored and sre-bot committed Mar 23, 2020
1 parent 8750363 commit 75bfa5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 16 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func (s *testSuite3) TestDo(c *C) {
tk.MustQuery("select @a").Check(testkit.Rows("1"))
}

func (s *testSuite3) TestSetRoleAllCorner(c *C) {
// For user with no role, `SET ROLE ALL` should active
// a empty slice, rather than nil.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user set_role_all")
se, err := session.CreateSession4Test(s.store)
c.Check(err, IsNil)
defer se.Close()
c.Assert(se.Auth(&auth.UserIdentity{Username: "set_role_all", Hostname: "localhost"}, nil, nil), IsTrue)
ctx := context.Background()
_, err = se.Execute(ctx, `set role all`)
c.Assert(err, IsNil)
_, err = se.Execute(ctx, `select current_role`)
c.Assert(err, IsNil)
}

func (s *testSuite3) TestCreateRole(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user testCreateRole;")
Expand Down
9 changes: 4 additions & 5 deletions privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,11 +1161,10 @@ func (p *MySQLPrivilege) getAllRoles(user, host string) []*auth.RoleIdentity {
key := user + "@" + host
edgeTable, ok := p.RoleGraph[key]
ret := make([]*auth.RoleIdentity, 0, len(edgeTable.roleList))
if !ok {
return nil
}
for _, r := range edgeTable.roleList {
ret = append(ret, r)
if ok {
for _, r := range edgeTable.roleList {
ret = append(ret, r)
}
}
return ret
}
Expand Down

0 comments on commit 75bfa5e

Please sign in to comment.