From 61568840d3156f748d17d32059b660280c1b0cc6 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 23 Jun 2023 22:25:55 +0800 Subject: [PATCH] feat: add workspace setting api --- api/v1/auth.go | 10 +++ api/v1/system.go | 15 ++++- api/v1/v1.go | 1 + api/v1/workspace_setting.go | 122 ++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 api/v1/workspace_setting.go diff --git a/api/v1/auth.go b/api/v1/auth.go index 56f1e4b..01faac9 100644 --- a/api/v1/auth.go +++ b/api/v1/auth.go @@ -63,6 +63,16 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) { g.POST("/auth/signup", func(c echo.Context) error { ctx := c.Request().Context() + disallowSignUpSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ + Key: WorkspaceDisallowSignUp.String(), + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting").SetInternal(err) + } + if disallowSignUpSetting != nil && disallowSignUpSetting.Value == "true" { + return echo.NewHTTPError(http.StatusForbidden, "Sign up is not allowed") + } + signup := &SignUpRequest{} if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err) diff --git a/api/v1/system.go b/api/v1/system.go index 38ecf76..71b25c0 100644 --- a/api/v1/system.go +++ b/api/v1/system.go @@ -4,11 +4,13 @@ import ( "net/http" "github.com/boojack/shortify/server/profile" + "github.com/boojack/shortify/store" "github.com/labstack/echo/v4" ) type SystemStatus struct { - Profile *profile.Profile `json:"profile"` + Profile *profile.Profile `json:"profile"` + DisallowSignUp bool `json:"disallowSignUp"` } func (s *APIV1Service) registerSystemRoutes(g *echo.Group) { @@ -17,10 +19,21 @@ func (s *APIV1Service) registerSystemRoutes(g *echo.Group) { }) g.GET("/status", func(c echo.Context) error { + ctx := c.Request().Context() systemStatus := SystemStatus{ Profile: s.Profile, } + disallowSignUpSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ + Key: WorkspaceDisallowSignUp.String(), + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting") + } + if disallowSignUpSetting != nil { + systemStatus.DisallowSignUp = disallowSignUpSetting.Value == "true" + } + return c.JSON(http.StatusOK, systemStatus) }) } diff --git a/api/v1/v1.go b/api/v1/v1.go index f1fbd49..150d892 100644 --- a/api/v1/v1.go +++ b/api/v1/v1.go @@ -22,6 +22,7 @@ func NewAPIV1Service(profile *profile.Profile, store *store.Store) *APIV1Service func (s *APIV1Service) Start(apiGroup *echo.Group, secret string) { apiV1Group := apiGroup.Group("/api/v1") s.registerSystemRoutes(apiV1Group) + s.registerWorkspaceSettingRoutes(apiV1Group) s.registerAuthRoutes(apiV1Group, secret) s.registerUserRoutes(apiV1Group) s.registerShortcutRoutes(apiV1Group) diff --git a/api/v1/workspace_setting.go b/api/v1/workspace_setting.go new file mode 100644 index 0000000..cc448f6 --- /dev/null +++ b/api/v1/workspace_setting.go @@ -0,0 +1,122 @@ +package v1 + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/boojack/shortify/store" + "github.com/labstack/echo/v4" +) + +type WorkspaceSettingKey string + +const ( + // WorkspaceDisallowSignUp is the key type for disallow sign up in workspace level. + WorkspaceDisallowSignUp WorkspaceSettingKey = "disallow-signup" +) + +// String returns the string format of WorkspaceSettingKey type. +func (key WorkspaceSettingKey) String() string { + if key == WorkspaceDisallowSignUp { + return "disallow-signup" + } + return "" +} + +type WorkspaceSetting struct { + Key WorkspaceSettingKey `json:"key"` + Value string `json:"value"` +} + +type WorkspaceSettingUpsert struct { + Key WorkspaceSettingKey `json:"key"` + Value string `json:"value"` +} + +func (upsert WorkspaceSettingUpsert) Validate() error { + if upsert.Key == WorkspaceDisallowSignUp { + value := false + err := json.Unmarshal([]byte(upsert.Value), &value) + if err != nil { + return fmt.Errorf("failed to unmarshal workspace setting disallow signup value") + } + } else { + return fmt.Errorf("invalid workspace setting key") + } + + return nil +} + +func (s *APIV1Service) registerWorkspaceSettingRoutes(g *echo.Group) { + g.POST("/workspace/setting", func(c echo.Context) error { + ctx := c.Request().Context() + userID, ok := c.Get(getUserIDContextKey()).(int) + if !ok { + return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + } + + user, err := s.Store.GetUser(ctx, &store.FindUser{ + ID: &userID, + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + } + if user == nil || user.Role != store.RoleAdmin { + return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") + } + + upsert := &WorkspaceSettingUpsert{} + if err := json.NewDecoder(c.Request().Body).Decode(upsert); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post workspace setting request").SetInternal(err) + } + if err := upsert.Validate(); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "system setting invalidate").SetInternal(err) + } + + workspaceSetting, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{ + Key: upsert.Key.String(), + Value: upsert.Value, + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert system setting").SetInternal(err) + } + return c.JSON(http.StatusOK, convertWorkspaceSettingFromStore(workspaceSetting)) + }) + + g.GET("/workspace/setting", func(c echo.Context) error { + ctx := c.Request().Context() + userID, ok := c.Get(getUserIDContextKey()).(int) + if !ok { + return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + } + + user, err := s.Store.GetUser(ctx, &store.FindUser{ + ID: &userID, + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + } + if user == nil || user.Role != store.RoleAdmin { + return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") + } + + list, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{}) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err) + } + + workspaceSettingList := []*WorkspaceSetting{} + for _, workspaceSetting := range list { + workspaceSettingList = append(workspaceSettingList, convertWorkspaceSettingFromStore(workspaceSetting)) + } + return c.JSON(http.StatusOK, workspaceSettingList) + }) +} + +func convertWorkspaceSettingFromStore(workspaceSetting *store.WorkspaceSetting) *WorkspaceSetting { + return &WorkspaceSetting{ + Key: WorkspaceSettingKey(workspaceSetting.Key), + Value: workspaceSetting.Value, + } +}