Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Implement POST /principal

天舟34469a706e9a
diff --git a/backend/server/principal.go b/backend/server/principal.go
index b696a6f121..3b6e2c43e4 100644
--- a/backend/server/principal.go
+++ b/backend/server/principal.go
@@ -3,7 +3,6 @@ package server
import (
"context"
"fmt"
- "log"
"net/http"
"strconv"
@@ -14,6 +13,34 @@ import (
)
func (s *Server) registerPrincipalRoutes(g *echo.Group) {
+ g.POST("/principal", func(c echo.Context) error {
+ principalCreate := &api.PrincipalCreate{}
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, principalCreate); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create principal request.").SetInternal(err)
+ }
+
+ principalCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ principalCreate.Status = api.Invited
+ principalCreate.Type = api.EndUser
+ principalCreate.PasswordHash = ""
+
+ principal, err := s.PrincipalService.CreatePrincipal(context.Background(), principalCreate)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
+ return echo.NewHTTPError(http.StatusConflict, bytebase.ErrorMessage(err))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create principal.").SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ c.Response().WriteHeader(http.StatusOK)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create principal response").SetInternal(err)
+ }
+
+ return nil
+ })
+
g.GET("/principal", func(c echo.Context) error {
list, err := s.PrincipalService.FindPrincipalList(context.Background())
if err != nil {
@@ -60,7 +87,6 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
principalPatch := &api.PrincipalPatch{UpdaterId: c.Get(GetPrincipalIdContextKey()).(int)}
if err := jsonapi.UnmarshalPayload(c.Request().Body, principalPatch); err != nil {
- log.Println(err)
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch principal request.").SetInternal(err)
}
diff --git a/backend/server/server.go b/backend/server/server.go
index 676a2b1e9e..72b5b8d78e 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -65,7 +65,7 @@ func NewServer() *Server {
g.Use(middleware.JWTWithConfig(middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
- return strings.HasPrefix(c.Path(), "/api/auth") || c.Path() == "/api/principal"
+ return strings.HasPrefix(c.Path(), "/api/auth") || (c.Path() == "/api/principal" && c.Request().Method == "GET")
},
Claims: &Claims{},
SigningMethod: middleware.AlgorithmHS256,