Skip to content

Commit

Permalink
feat: optimize get permission name (#548)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Aug 17, 2021
1 parent 40b9c06 commit b1d88d6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
19 changes: 4 additions & 15 deletions manager/permission/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,13 @@ func InitRole(e *casbin.Enforcer, g *gin.Engine) error {
}

func GetAPIGroupName(path string) (string, error) {
apiGroupRegexp := regexp.MustCompile(`^/api/v[0-9]+/(?P<apiGroup>[\-_a-zA-Z]+)`)
apiGroupRegexp := regexp.MustCompile(`^/api/v[0-9]+/([-_a-zA-Z]*)[/.*]*`)
matchs := apiGroupRegexp.FindStringSubmatch(path)
if matchs == nil {
if len(matchs) != 2 {
return "", errors.New("faild to find api group")
}
apiGroupName := ""
regexGroupNames := apiGroupRegexp.SubexpNames()
for i, name := range regexGroupNames {
if i != 0 && name == "apiGroup" {
apiGroupName = matchs[i]
}
}

if apiGroupName != "" {
return apiGroupName, nil
}
return "", errors.New("faild to find api group")

return matchs[1], nil
}

func RoleName(object, action string) string {
Expand Down Expand Up @@ -128,8 +117,8 @@ func GetAPIGroupNames(g *gin.Engine) []string {
}

}
return APIGroups

return APIGroups
}

func SystemRoles(g *gin.Engine) []string {
Expand Down
67 changes: 46 additions & 21 deletions manager/permission/rbac/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,64 @@ package rbac

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetApiGroupName(t *testing.T) {
tests := []struct {
path string
exceptedGroupName string
hasError bool
name string
path string
expect func(t *testing.T, data string, err error)
}{
{
path: "/api/v1/users",
exceptedGroupName: "users",
hasError: false,
name: `path is /api/v1/users`,
path: "/api/v1/users",
expect: func(t *testing.T, data string, err error) {
assert := assert.New(t)
assert.Equal(data, "users")
},
},
{
name: `path is /api/v1/users/`,
path: "/api/v1/users/",
expect: func(t *testing.T, data string, err error) {
assert := assert.New(t)
assert.Equal(data, "users")
},
},
{
name: `path is /api/v1/users/name`,
path: "/api/v1/users/name",
expect: func(t *testing.T, data string, err error) {
assert := assert.New(t)
assert.Equal(data, "users")
},
},
{
name: `path is /api/user`,
path: "/api/user",
expect: func(t *testing.T, data string, err error) {
assert := assert.New(t)
assert.EqualError(err, "faild to find api group")
},
},
{
path: "/api/user",
exceptedGroupName: "",
hasError: true,
name: "path is empty",
path: "",
expect: func(t *testing.T, data string, err error) {
assert := assert.New(t)
assert.EqualError(err, "faild to find api group")
},
},
}

for _, tt := range tests {
groupName, err := GetAPIGroupName(tt.path)
if tt.hasError {
if err == nil {
t.Errorf("GetApiGroupName(%s) should return error", tt.path)
}
}

if groupName != tt.exceptedGroupName {
t.Errorf("GetApiGroupName(%v) = %v, want %v", tt.path, groupName, tt.exceptedGroupName)
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
name, err := GetAPIGroupName(tc.path)
tc.expect(t, name, err)
})
}

}

func TestRoleName(t *testing.T) {
Expand Down

0 comments on commit b1d88d6

Please sign in to comment.