Unverified Commit 3158c4b8 authored by Wen Sun's avatar Wen Sun Committed by GitHub

fix: role error in api/v2 when the first user registers (#2875)

Fix role error in api/v2 when the first user registers
parent 30ae4140
...@@ -179,7 +179,7 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques ...@@ -179,7 +179,7 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace setting, err: %s", err)) 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") 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 ...@@ -193,13 +193,17 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques
Nickname: request.Username, Nickname: request.Username,
PasswordHash: string(passwordHash), 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 { if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err)) 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(existedHostUsers) == 0 {
if len(existingUsers) == 0 { // Change the default role to host if there is no host user.
create.Role = store.RoleAdmin create.Role = store.RoleHost
} else { } else {
create.Role = store.RoleUser create.Role = store.RoleUser
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment