visamasteractions: register /:id/actions as its own rbac prefix
The server panicked at startup: the actions group declared require-staff:cards for /visa-master-payment-orders, which cards/visamaster already registers with require:cards, and rbac allows one policy per prefix. The group is now /visa-master-payment-orders/:id/actions, the same way /callback has its own prefix; the routes are unchanged. manager.New's handler registration moves into registerRoutes, and TestRegisterRoutes runs it without a database, so a conflicting policy fails the tests instead of the server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,11 +27,13 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
}
|
||||
|
||||
// Register only wires read endpoints: actions are recorded by the
|
||||
// repositories that change orders, never through the API.
|
||||
// repositories that change orders, never through the API. The group is
|
||||
// its own /:id/actions prefix because rbac allows one policy per prefix
|
||||
// and cards/visamaster already declares require:cards for baseURL.
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.Cards))
|
||||
group.GET("/:id/actions", h.list)
|
||||
group.GET("/:id/actions/:actionId", h.get)
|
||||
group := rbac.Group(router, baseURL+"/:id/actions", rbac.RequireStaff(perm.Cards))
|
||||
group.GET("", h.list)
|
||||
group.GET("/:actionId", h.get)
|
||||
}
|
||||
|
||||
// list godoc
|
||||
|
||||
@@ -74,6 +74,19 @@ func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin.
|
||||
}
|
||||
adminGroup.Use(rbac.Guard(rbac.NewService(client)))
|
||||
|
||||
registerRoutes(router, adminGroup, cfg, client, logger)
|
||||
|
||||
// Deny by default: every admin route must declare its policy via rbac.Group.
|
||||
if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
// registerRoutes has every bounded context register its handlers. rbac
|
||||
// panics here if two groups declare different policies for one prefix.
|
||||
func registerRoutes(router *gin.Engine, adminGroup *gin.RouterGroup, cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) {
|
||||
// Each bounded context registers its own modules' handlers.
|
||||
// Add new modules inside the relevant context's router.go rather than here.
|
||||
identity.Register(adminGroup, router, client, logger)
|
||||
@@ -94,13 +107,6 @@ func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin.
|
||||
logger,
|
||||
)
|
||||
backup.NewHandler(logger, backupSvc).Register(adminGroup)
|
||||
|
||||
// Deny by default: every admin route must declare its policy via rbac.Group.
|
||||
if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
// paymentGatewayClient picks the real GBÜS client outside debug mode, and
|
||||
|
||||
29
internal/handlers/manager/manager_test.go
Normal file
29
internal/handlers/manager/manager_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handlermanager
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"online-back/internal/config"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// TestRegisterRoutes registers every handler the way New does (no database:
|
||||
// handlers only touch it per request), so a startup panic such as two
|
||||
// groups declaring different policies for one prefix fails here instead
|
||||
// of on the server.
|
||||
func TestRegisterRoutes(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
debug := true
|
||||
cfg := &config.Config{IsDebug: &debug}
|
||||
|
||||
router := gin.New()
|
||||
adminGroup := router.Group(adminAPIPrefix)
|
||||
registerRoutes(router, adminGroup, cfg, nil, logging.GetLogger())
|
||||
|
||||
if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user