diff --git a/pkg/services/serviceaccounts/manager/service.go b/pkg/services/serviceaccounts/manager/service.go index a7e41de2cb8..c2ec6a0977b 100644 --- a/pkg/services/serviceaccounts/manager/service.go +++ b/pkg/services/serviceaccounts/manager/service.go @@ -42,10 +42,10 @@ func ProvideServiceAccountsService( return s, nil } -func (s *ServiceAccountsService) DeleteServiceAccount(ctx context.Context, orgID, serviceAccountID int64) error { - if !s.cfg.FeatureToggles["service-accounts"] { - s.log.Debug(ServiceAccountFeatureToggleNotFound) +func (sa *ServiceAccountsService) DeleteServiceAccount(ctx context.Context, orgID, serviceAccountID int64) error { + if !sa.cfg.FeatureToggles["service-accounts"] { + sa.log.Debug(ServiceAccountFeatureToggleNotFound) return nil } - return s.store.DeleteServiceAccount(ctx, orgID, serviceAccountID) + return sa.store.DeleteServiceAccount(ctx, orgID, serviceAccountID) } diff --git a/pkg/services/sqlstore/org_test.go b/pkg/services/sqlstore/org_test.go index 376fad7be68..bce5d2352ee 100644 --- a/pkg/services/sqlstore/org_test.go +++ b/pkg/services/sqlstore/org_test.go @@ -141,11 +141,15 @@ func TestAccountDataAccess(t *testing.T) { ac1cmd := models.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"} ac2cmd := models.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true} + serviceaccountcmd := models.CreateUserCommand{Login: "serviceaccount", Email: "service@test.com", Name: "serviceaccount name", IsAdmin: true, IsServiceAccount: true} ac1, err := sqlStore.CreateUser(context.Background(), ac1cmd) require.NoError(t, err) ac2, err := sqlStore.CreateUser(context.Background(), ac2cmd) require.NoError(t, err) + // user only used for making sure we filter out the service accounts + _, err = sqlStore.CreateUser(context.Background(), serviceaccountcmd) + require.NoError(t, err) t.Run("Should be able to read user info projection", func(t *testing.T) { query := models.GetUserProfileQuery{UserId: ac1.Id} diff --git a/pkg/services/sqlstore/org_users.go b/pkg/services/sqlstore/org_users.go index adadc20025c..e825667f5f5 100644 --- a/pkg/services/sqlstore/org_users.go +++ b/pkg/services/sqlstore/org_users.go @@ -107,6 +107,10 @@ func (ss *SQLStore) GetOrgUsers(ctx context.Context, query *models.GetOrgUsersQu whereConditions = append(whereConditions, "org_user.org_id = ?") whereParams = append(whereParams, query.OrgId) + // TODO: add to chore, for cleaning up after we have created + // service accounts table in the modelling + whereConditions = append(whereConditions, fmt.Sprintf("%s.is_service_account = false", x.Dialect().Quote("user"))) + if query.Query != "" { queryWithWildcards := "%" + query.Query + "%" whereConditions = append(whereConditions, "(email "+dialect.LikeStr()+" ? OR name "+dialect.LikeStr()+" ? OR login "+dialect.LikeStr()+" ?)") @@ -157,6 +161,10 @@ func (ss *SQLStore) SearchOrgUsers(ctx context.Context, query *models.SearchOrgU whereConditions = append(whereConditions, "org_user.org_id = ?") whereParams = append(whereParams, query.OrgID) + // TODO: add to chore, for cleaning up after we have created + // service accounts table in the modelling + whereConditions = append(whereConditions, fmt.Sprintf("%s.is_service_account = false", x.Dialect().Quote("user"))) + if query.Query != "" { queryWithWildcards := "%" + query.Query + "%" whereConditions = append(whereConditions, "(email "+dialect.LikeStr()+" ? OR name "+dialect.LikeStr()+" ? OR login "+dialect.LikeStr()+" ?)") @@ -189,7 +197,8 @@ func (ss *SQLStore) SearchOrgUsers(ctx context.Context, query *models.SearchOrgU // get total count orgUser := models.OrgUser{} - countSess := x.Table("org_user") + countSess := x.Table("org_user"). + Join("INNER", x.Dialect().Quote("user"), fmt.Sprintf("org_user.user_id=%s.id", x.Dialect().Quote("user"))) if len(whereConditions) > 0 { countSess.Where(strings.Join(whereConditions, " AND "), whereParams...) diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 0cc36a5746c..f3c5ffea65e 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -609,6 +609,10 @@ func SearchUsers(ctx context.Context, query *models.SearchUsersQuery) error { whereParams := make([]interface{}, 0) sess := x.Table("user").Alias("u") + // TODO: add to chore, for cleaning up after we have created + // service accounts table in the modelling + whereConditions = append(whereConditions, "u.is_service_account = false") + // Join with only most recent auth module joinCondition := `( SELECT id from user_auth