chore: update user checks

This commit is contained in:
Steven 2023-09-24 10:17:05 +08:00
parent 8436d86661
commit 96f6fa4257
4 changed files with 26 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/boojack/slash/api/auth"
storepb "github.com/boojack/slash/proto/gen/store"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store"
)
@ -77,6 +78,16 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
return echo.NewHTTPError(http.StatusForbidden, "sign up has been disabled")
}
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list users").SetInternal(err)
}
if len(userList) >= 5 {
return echo.NewHTTPError(http.StatusBadRequest, "Maximum number of users reached")
}
}
signup := &SignUpRequest{}
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted signup request, err: %s", err)).SetInternal(err)

View File

@ -11,6 +11,7 @@ import (
"golang.org/x/crypto/bcrypt"
"github.com/boojack/slash/internal/util"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store"
)
@ -102,6 +103,16 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized to create user")
}
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list users").SetInternal(err)
}
if len(userList) >= 5 {
return echo.NewHTTPError(http.StatusBadRequest, "Maximum number of users reached")
}
}
userCreate := &CreateUserRequest{}
if err := json.NewDecoder(c.Request().Body).Decode(userCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user request").SetInternal(err)

View File

@ -81,7 +81,7 @@ func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUse
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list users: %v", err)
}
if len(userList) >= 3 {
if len(userList) >= 5 {
return nil, status.Errorf(codes.ResourceExhausted, "maximum number of users reached")
}
}

View File

@ -985,8 +985,9 @@
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| mode | [string](#string) | | |
| enable_signup | [bool](#bool) | | |
| mode | [string](#string) | | Current workspace mode: dev, prod. |
| plan | [PlanType](#slash-api-v2-PlanType) | | The workspace plan. |
| enable_signup | [bool](#bool) | | Whether to enable other users to sign up. |
| custom_style | [string](#string) | | The custom style. |
| custom_script | [string](#string) | | The custom script. |