diff --git a/api/v2/user_service.go b/api/v2/user_service.go index 49bb2d8..21b1f37 100644 --- a/api/v2/user_service.go +++ b/api/v2/user_service.go @@ -15,21 +15,24 @@ import ( "github.com/boojack/slash/api/auth" apiv2pb "github.com/boojack/slash/proto/gen/api/v2" storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/server/service/license" "github.com/boojack/slash/store" ) type UserService struct { apiv2pb.UnimplementedUserServiceServer - Secret string - Store *store.Store + Secret string + Store *store.Store + LicenseService *license.LicenseService } // NewUserService creates a new UserService. -func NewUserService(secret string, store *store.Store) *UserService { +func NewUserService(secret string, store *store.Store, licenseService *license.LicenseService) *UserService { return &UserService{ - Secret: secret, - Store: store, + Secret: secret, + Store: store, + LicenseService: licenseService, } } @@ -73,6 +76,16 @@ func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUse return nil, status.Errorf(codes.Internal, "failed to hash password: %v", err) } + if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) { + userList, err := s.Store.ListUsers(ctx, &store.FindUser{}) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to list users: %v", err) + } + if len(userList) >= 3 { + return nil, status.Errorf(codes.ResourceExhausted, "maximum number of users reached") + } + } + user, err := s.Store.CreateUser(ctx, &store.User{ Email: request.User.Email, Nickname: request.User.Nickname, diff --git a/api/v2/v2.go b/api/v2/v2.go index fb39c90..f13f1c6 100644 --- a/api/v2/v2.go +++ b/api/v2/v2.go @@ -36,7 +36,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store ) apiv2pb.RegisterSubscriptionServiceServer(grpcServer, NewSubscriptionService(profile, store, licenseService)) apiv2pb.RegisterWorkspaceServiceServer(grpcServer, NewWorkspaceService(profile, store, licenseService)) - apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(secret, store)) + apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(secret, store, licenseService)) apiv2pb.RegisterUserSettingServiceServer(grpcServer, NewUserSettingService(store)) apiv2pb.RegisterShortcutServiceServer(grpcServer, NewShortcutService(secret, store)) reflection.Register(grpcServer) diff --git a/api/v2/workspace_service.go b/api/v2/workspace_service.go index 9193ea9..71b59ca 100644 --- a/api/v2/workspace_service.go +++ b/api/v2/workspace_service.go @@ -33,7 +33,16 @@ func NewWorkspaceService(profile *profile.Profile, store *store.Store, licenseSe func (s *WorkspaceService) GetWorkspaceProfile(ctx context.Context, _ *apiv2pb.GetWorkspaceProfileRequest) (*apiv2pb.GetWorkspaceProfileResponse, error) { profile := &apiv2pb.WorkspaceProfile{ Mode: s.Profile.Mode, + Plan: apiv2pb.PlanType_PRO, } + + // Load subscription plan from license service. + subscription, err := s.LicenseService.GetSubscription(ctx) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get subscription: %v", err) + } + profile.Plan = subscription.Plan + workspaceSetting, err := s.GetWorkspaceSetting(ctx, &apiv2pb.GetWorkspaceSettingRequest{}) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err) diff --git a/server/service/license/license.go b/server/service/license/license.go index e27ed06..74f7f0f 100644 --- a/server/service/license/license.go +++ b/server/service/license/license.go @@ -96,6 +96,10 @@ func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey stri return s.LoadSubscription(ctx) } +func (s *LicenseService) GetSubscription(ctx context.Context) (*apiv2pb.Subscription, error) { + return s.LoadSubscription(ctx) +} + func (s *LicenseService) IsFeatureEnabled(feature FeatureType) bool { matrix, ok := FeatureMatrix[feature] if !ok {