feat separate order creation and contract menus
This commit is contained in:
@@ -12,6 +12,10 @@ import (
|
||||
const platformMenusContextKey = "platform_authorized_menus"
|
||||
|
||||
func platformMenuAllowsPath(menus []platformbase.Menu, requestPath string) bool {
|
||||
return platformMenuAllowsRequest(menus, requestPath, "GET")
|
||||
}
|
||||
|
||||
func platformMenuAllowsRequest(menus []platformbase.Menu, requestPath, method string) bool {
|
||||
marker := "/platform/v1/"
|
||||
index := strings.Index(requestPath, marker)
|
||||
if index < 0 {
|
||||
@@ -20,10 +24,17 @@ func platformMenuAllowsPath(menus []platformbase.Menu, requestPath string) bool
|
||||
relative := strings.Trim(requestPath[index+len(marker):], "/")
|
||||
resource := strings.Split(relative, "/")[0]
|
||||
menuIdentity := platformRouteMenuIdentity(resource)
|
||||
if resource == "gasorder_basic" && method == "POST" {
|
||||
menuIdentity = "gasorder_create"
|
||||
}
|
||||
for _, menu := range menus {
|
||||
if menu.Identity == menuIdentity {
|
||||
return true
|
||||
}
|
||||
if method == "GET" && menu.Identity == "gasorder_create" &&
|
||||
(menuIdentity == "gasorder_contract" || resource == "user_address") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -75,7 +86,7 @@ func RequirePlatformMenuAccess() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
menus, err := platformbase.LoadPlatformMenus(claims.Role)
|
||||
if err != nil || !platformMenuAllowsPath(menus, ctx.Request.URL.Path) {
|
||||
if err != nil || !platformMenuAllowsRequest(menus, ctx.Request.URL.Path, ctx.Request.Method) {
|
||||
infra.Response.Error(ctx, errcode.ErrPermissionDenied)
|
||||
ctx.Abort()
|
||||
return
|
||||
|
||||
@@ -32,6 +32,20 @@ func TestHiddenGasorderResourcesFollowOwningSecondLevelMenu(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateOrderPermissionIsSeparateFromOrderManagement(t *testing.T) {
|
||||
createMenus := []platformbase.Menu{{Identity: "gasorder_create"}}
|
||||
if !platformMenuAllowsRequest(createMenus, "/heqi/platform/v1/gasorder_basic", "POST") {
|
||||
t.Fatal("create-order menu did not grant order creation")
|
||||
}
|
||||
if platformMenuAllowsRequest(createMenus, "/heqi/platform/v1/gasorder_basic", "GET") {
|
||||
t.Fatal("create-order menu granted order list access")
|
||||
}
|
||||
orderMenus := []platformbase.Menu{{Identity: "gasorder_basic"}}
|
||||
if platformMenuAllowsRequest(orderMenus, "/heqi/platform/v1/gasorder_basic", "POST") {
|
||||
t.Fatal("order-management menu granted order creation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocationScopeValuesAreExplicit(t *testing.T) {
|
||||
if !validLocationScope("standard") || !validLocationScope("precise") {
|
||||
t.Fatal("supported location scopes were rejected")
|
||||
|
||||
Reference in New Issue
Block a user