mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +00:00
feat: add workspace setting api
This commit is contained in:
parent
c2c9b46705
commit
61568840d3
@ -63,6 +63,16 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
|
|||||||
|
|
||||||
g.POST("/auth/signup", func(c echo.Context) error {
|
g.POST("/auth/signup", func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
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{}
|
signup := &SignUpRequest{}
|
||||||
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
|
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
|
||||||
|
@ -4,11 +4,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/boojack/shortify/server/profile"
|
"github.com/boojack/shortify/server/profile"
|
||||||
|
"github.com/boojack/shortify/store"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SystemStatus struct {
|
type SystemStatus struct {
|
||||||
Profile *profile.Profile `json:"profile"`
|
Profile *profile.Profile `json:"profile"`
|
||||||
|
DisallowSignUp bool `json:"disallowSignUp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIV1Service) registerSystemRoutes(g *echo.Group) {
|
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 {
|
g.GET("/status", func(c echo.Context) error {
|
||||||
|
ctx := c.Request().Context()
|
||||||
systemStatus := SystemStatus{
|
systemStatus := SystemStatus{
|
||||||
Profile: s.Profile,
|
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)
|
return c.JSON(http.StatusOK, systemStatus)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ func NewAPIV1Service(profile *profile.Profile, store *store.Store) *APIV1Service
|
|||||||
func (s *APIV1Service) Start(apiGroup *echo.Group, secret string) {
|
func (s *APIV1Service) Start(apiGroup *echo.Group, secret string) {
|
||||||
apiV1Group := apiGroup.Group("/api/v1")
|
apiV1Group := apiGroup.Group("/api/v1")
|
||||||
s.registerSystemRoutes(apiV1Group)
|
s.registerSystemRoutes(apiV1Group)
|
||||||
|
s.registerWorkspaceSettingRoutes(apiV1Group)
|
||||||
s.registerAuthRoutes(apiV1Group, secret)
|
s.registerAuthRoutes(apiV1Group, secret)
|
||||||
s.registerUserRoutes(apiV1Group)
|
s.registerUserRoutes(apiV1Group)
|
||||||
s.registerShortcutRoutes(apiV1Group)
|
s.registerShortcutRoutes(apiV1Group)
|
||||||
|
122
api/v1/workspace_setting.go
Normal file
122
api/v1/workspace_setting.go
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user