Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: remove role in the user message (#12535)
ecmadao8c03ad2b2fac
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.goindex 11d21942bd..8795df309b 100644--- a/backend/api/v1/acl.go+++ b/backend/api/v1/acl.go@@ -15,6 +15,7 @@ import (enterprise "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/store"+ "github.com/bytebase/bytebase/backend/utils")// ACLInterceptor is the v1 ACL interceptor for gRPC server.@@ -45,7 +46,7 @@ func (in *ACLInterceptor) ACLInterceptor(ctx context.Context, request any, serve}if user != nil {// Store workspace role into context.- ctx = context.WithValue(ctx, common.RoleContextKey, user.Role)+ ctx = context.WithValue(ctx, common.RoleContextKey, utils.BackfillRoleFromRoles(user.Roles))ctx = context.WithValue(ctx, common.UserContextKey, user)}@@ -73,7 +74,7 @@ func (in *ACLInterceptor) ACLStreamInterceptor(request any, ss grpc.ServerStream}if user != nil {// Store workspace role into context.- ctx = context.WithValue(ctx, common.RoleContextKey, user.Role)+ ctx = context.WithValue(ctx, common.RoleContextKey, utils.BackfillRoleFromRoles(user.Roles))ctx = context.WithValue(ctx, common.UserContextKey, user)ss = overrideStream{ServerStream: ss, childCtx: ctx}}@@ -127,7 +128,6 @@ func (in *ACLInterceptor) getUser(ctx context.Context) (*store.UserMessage, erro// If RBAC feature is not enabled, all users are treated as OWNER.if in.licenseService.IsFeatureEnabled(api.FeatureRBAC) != nil {- user.Role = api.WorkspaceAdminuser.Roles = uniq(append(user.Roles, api.WorkspaceAdmin))}return user, nildiff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.goindex d802b8a6e7..ff7c949348 100644--- a/backend/api/v1/database_service.go+++ b/backend/api/v1/database_service.go@@ -1623,8 +1623,12 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lisif !ok {return nil, status.Errorf(codes.Internal, "user not found")}+ role, ok := ctx.Value(common.RoleContextKey).(api.Role)+ if !ok {+ return nil, status.Errorf(codes.Internal, "role not found")+ }- switch user.Role {+ switch role {case api.WorkspaceAdmin, api.WorkspaceDBA:canAccessDBs = databasescase api.WorkspaceMember:@@ -1638,7 +1642,7 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis}}default:- return nil, status.Errorf(codes.PermissionDenied, "unknown role %q", user.Role)+ return nil, status.Errorf(codes.PermissionDenied, "unknown role %q", role)}result := &v1pb.ListSlowQueriesResponse{}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex c12aa38957..0e8b2422da 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -330,16 +330,13 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, request *v1pb.BatchRreturn nil, status.Errorf(codes.InvalidArgument, "No tasks to run in the stage")}- principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)+ user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)if !ok {- return nil, status.Errorf(codes.Internal, "principal ID not found")- }- user, err := s.store.GetUserByID(ctx, principalID)- if err != nil {- return nil, status.Errorf(codes.Internal, "failed to find user, error: %v", err)+ return nil, status.Errorf(codes.Internal, "user not found")}- if user == nil {- return nil, status.Errorf(codes.NotFound, "user %v not found", principalID)+ role, ok := ctx.Value(common.RoleContextKey).(api.Role)+ if !ok {+ return nil, status.Errorf(codes.Internal, "role not found")}ok, err = canUserRunStageTasks(ctx, s.store, user, issue, stageToRun.EnvironmentID)@@ -354,7 +351,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, request *v1pb.BatchRif err != nil {return nil, status.Errorf(codes.Internal, "failed to check if the issue is approved, error: %v", err)}- if !approved && !(user.Role == api.WorkspaceAdmin || user.Role == api.WorkspaceDBA) {+ if !approved && !(role == api.WorkspaceAdmin || role == api.WorkspaceDBA) {return nil, status.Errorf(codes.FailedPrecondition, "cannot run the tasks because the issue is not approved")}diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 457a36efdd..4e96603a96 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -939,7 +939,8 @@ func (s *SQLService) accessCheck(isAdmin,isExport bool) error {// Check if the caller is admin for exporting with admin mode.- if isAdmin && isExport && (user.Role != api.WorkspaceAdmin && user.Role != api.WorkspaceDBA) {+ role := utils.BackfillRoleFromRoles(user.Roles)+ if isAdmin && isExport && (role != api.WorkspaceAdmin && role != api.WorkspaceDBA) {return status.Errorf(codes.PermissionDenied, "only workspace owner and DBA can export data using admin mode")}diff --git a/backend/store/principal.go b/backend/store/principal.goindex 618dbca018..04e6708196 100644--- a/backend/store/principal.go+++ b/backend/store/principal.go@@ -22,7 +22,6 @@ var systemBotUser = &UserMessage{Name: "Bytebase",Email: api.SystemBotEmail,Type: api.SystemBot,- Role: api.WorkspaceAdmin,Roles: []api.Role{api.WorkspaceAdmin},}@@ -51,12 +50,10 @@ type UpdateUserMessage struct {type UserMessage struct {ID int// Email must be lower case.- Email string- Name string- Type api.PrincipalType- PasswordHash string- // TODO(p0ny): deprecate Role in favor of Roles.- Role api.Role+ Email string+ Name string+ Type api.PrincipalType+ PasswordHash stringRoles []api.RoleMemberDeleted boolMFAConfig *storepb.MFAConfig@@ -239,8 +236,6 @@ func (*Store) listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) (userMessage.Roles = append(userMessage.Roles, api.WorkspaceAdmin)}- userMessage.Role = backfillRoleFromRoles(userMessage.Roles)-userMessage.MemberDeleted = convertRowStatusToDeleted(rowStatus)mfaConfig := storepb.MFAConfig{}if err := common.ProtojsonUnmarshaler.Unmarshal(mfaConfigBytes, &mfaConfig); err != nil {@@ -331,7 +326,6 @@ func (s *Store) CreateUser(ctx context.Context, create *UserMessage, creatorID iPasswordHash: create.PasswordHash,Phone: create.Phone,Roles: roles,- Role: backfillRoleFromRoles(roles),}s.userIDCache.Add(user.ID, user)s.userEmailCache.Add(user.Email, user)@@ -468,25 +462,6 @@ func (s *Store) updateUserRoles(ctx context.Context, tx *Tx, userUID int, rolesreturn nil}-func backfillRoleFromRoles(roles []api.Role) api.Role {- admin, dba := false, false- for _, r := range roles {- if r == api.WorkspaceAdmin {- admin = true- }- if r == api.WorkspaceDBA {- dba = true- }- }- if admin {- return api.WorkspaceAdmin- }- if dba {- return api.WorkspaceDBA- }- return api.WorkspaceMember-}-func uniq[T comparable](array []T) []T {res := make([]T, 0, len(array))seen := make(map[T]struct{}, len(array))diff --git a/backend/utils/member.go b/backend/utils/member.goindex 31c88d4130..7cad3b1486 100644--- a/backend/utils/member.go+++ b/backend/utils/member.go@@ -203,3 +203,24 @@ func GetUserFormattedRolesMap(ctx context.Context, stores *store.Store, user *st}return rolesMap, nil}++// BackfillRoleFromRoles finds the highest workspace level role from roles.+func BackfillRoleFromRoles(roles []api.Role) api.Role {+ admin, dba := false, false+ for _, r := range roles {+ if r == api.WorkspaceAdmin {+ admin = true+ break+ }+ if r == api.WorkspaceDBA {+ dba = true+ }+ }+ if admin {+ return api.WorkspaceAdmin+ }+ if dba {+ return api.WorkspaceDBA+ }+ return api.WorkspaceMember+}