From 3158c4b8b58b60f7512093edc8aadfc212e46c01 Mon Sep 17 00:00:00 2001 From: Wen Sun Date: Wed, 31 Jan 2024 20:55:52 +0900 Subject: [PATCH] fix: role error in api/v2 when the first user registers (#2875) Fix role error in api/v2 when the first user registers --- api/v2/auth_service.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/api/v2/auth_service.go b/api/v2/auth_service.go index 35cd01c9..25a43a4a 100644 --- a/api/v2/auth_service.go +++ b/api/v2/auth_service.go @@ -179,7 +179,7 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques if err != nil { return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace setting, err: %s", err)) } - if workspaceGeneralSetting.DisallowSignup { + if workspaceGeneralSetting.DisallowSignup || workspaceGeneralSetting.DisallowPasswordLogin { return nil, status.Errorf(codes.PermissionDenied, "sign up is not allowed") } @@ -193,13 +193,17 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques Nickname: request.Username, PasswordHash: string(passwordHash), } - existingUsers, err := s.Store.ListUsers(ctx, &store.FindUser{}) + + hostUserType := store.RoleHost + existedHostUsers, err := s.Store.ListUsers(ctx, &store.FindUser{ + Role: &hostUserType, + }) if err != nil { return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err)) } - // The first user to sign up is an admin by default. - if len(existingUsers) == 0 { - create.Role = store.RoleAdmin + if len(existedHostUsers) == 0 { + // Change the default role to host if there is no host user. + create.Role = store.RoleHost } else { create.Role = store.RoleUser }