mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/grafana/grafana/pkg/models"
|
||
|
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||
|
)
|
||
|
|
||
|
type ServiceAccountsStoreImpl struct {
|
||
|
sqlStore *sqlstore.SQLStore
|
||
|
}
|
||
|
|
||
|
func NewServiceAccountsStore(store *sqlstore.SQLStore) *ServiceAccountsStoreImpl {
|
||
|
return &ServiceAccountsStoreImpl{
|
||
|
sqlStore: store,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *ServiceAccountsStoreImpl) DeleteServiceAccount(ctx context.Context, orgID, serviceaccountID int64) error {
|
||
|
return s.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||
|
return deleteServiceAccountInTransaction(sess, orgID, serviceaccountID)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func deleteServiceAccountInTransaction(sess *sqlstore.DBSession, orgID, serviceAccountID int64) error {
|
||
|
user := models.User{}
|
||
|
has, err := sess.Where(`org_id = ? and id = ? and is_service_account = true`, orgID, serviceAccountID).Get(&user)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if !has {
|
||
|
return serviceaccounts.ErrServiceAccountNotFound
|
||
|
}
|
||
|
for _, sql := range sqlstore.ServiceAccountDeletions() {
|
||
|
_, err := sess.Exec(sql, user.Id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|