Chore: get user from appcontext rather than store utility (#59926)

user from appcontext
This commit is contained in:
Ryan McKinley 2022-12-07 07:38:23 -08:00 committed by GitHub
parent e8ed79873a
commit d036225f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 51 deletions

View File

@ -13,8 +13,8 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
jsoniter "github.com/json-iterator/go"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/user"
)
@ -77,7 +77,7 @@ func (ch *commitHelper) initOrg(ctx context.Context, sql db.DB, orgID int64) err
OrgID: orgID, // gets filled in from each row
UserID: 0,
}
ch.ctx = store.ContextWithUser(context.Background(), rowUser)
ch.ctx = appcontext.WithUser(context.Background(), rowUser)
return err
})
}

View File

@ -6,13 +6,13 @@ import (
"sync"
"time"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
"github.com/grafana/grafana/pkg/services/playlist"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/store/entity"
"github.com/grafana/grafana/pkg/services/store/kind/snapshot"
"github.com/grafana/grafana/pkg/services/user"
@ -101,7 +101,7 @@ func (e *entityStoreJob) start(ctx context.Context) {
OrgID: 0, // gets filled in from each row
UserID: 0,
}
ctx = store.ContextWithUser(ctx, rowUser)
ctx = appcontext.WithUser(ctx, rowUser)
what := models.StandardKindDashboard
e.status.Count[what] = 0

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
@ -20,7 +21,6 @@ import (
"github.com/grafana/grafana/pkg/services/live"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/playlist"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/store/entity"
"github.com/grafana/grafana/pkg/setting"
)
@ -225,7 +225,7 @@ func (ex *StandardExport) HandleRequestExport(c *models.ReqContext) response.Res
return response.Error(http.StatusLocked, "export already running", nil)
}
ctx := store.ContextWithUser(context.Background(), c.SignedInUser)
ctx := appcontext.WithUser(context.Background(), c.SignedInUser)
var job Job
broadcast := func(s ExportStatus) {
ex.broadcastStatus(c.OrgID, s)

View File

@ -5,10 +5,10 @@ import (
"encoding/json"
"fmt"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/playlist"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
objectstore "github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/store/entity"
"github.com/grafana/grafana/pkg/services/user"
)
@ -44,7 +44,7 @@ func (s *entityStoreImpl) sync() {
UserID: 0, // Admin user
IsGrafanaAdmin: true,
}
ctx := objectstore.ContextWithUser(context.Background(), rowUser)
ctx := appcontext.WithUser(context.Background(), rowUser)
for _, info := range results {
dto, err := s.sqlimpl.Get(ctx, &playlist.GetPlaylistByUidQuery{
OrgId: info.OrgID,

View File

@ -1,44 +1,11 @@
package store
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context"
"github.com/grafana/grafana/pkg/services/user"
)
type testUserKey struct{}
func ContextWithUser(ctx context.Context, data *user.SignedInUser) context.Context {
return context.WithValue(ctx, testUserKey{}, data)
}
// UserFromContext ** Experimental **
// TODO: move to global infra package / new auth service
func UserFromContext(ctx context.Context) *user.SignedInUser {
grpcCtx := grpccontext.FromContext(ctx)
if grpcCtx != nil {
return grpcCtx.SignedInUser
}
// Explicitly set in context
u, ok := ctx.Value(testUserKey{}).(*user.SignedInUser)
if ok && u != nil {
return u
}
// From the HTTP request
c, ok := ctxkey.Get(ctx).(*models.ReqContext)
if !ok || c == nil || c.SignedInUser == nil {
return nil
}
return c.SignedInUser
}
// Really just spitballing here :) this should hook into a system that can give better display info
func GetUserIDString(user *user.SignedInUser) string {
if user == nil {

View File

@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/slugify"
@ -109,7 +110,10 @@ func (s *sqlEntityServer) validateGRN(ctx context.Context, grn *entity.GRN) (*en
if grn == nil {
return nil, fmt.Errorf("missing GRN")
}
user := store.UserFromContext(ctx)
user, err := appcontext.User(ctx)
if err != nil {
return nil, err
}
if grn.TenantId == 0 {
grn.TenantId = user.OrgID
} else if grn.TenantId != user.OrgID {
@ -281,7 +285,10 @@ func (s *sqlEntityServer) AdminWrite(ctx context.Context, r *entity.AdminWriteEn
updatedAt := r.UpdatedAt
updatedBy := r.UpdatedBy
if updatedBy == "" {
modifier := store.UserFromContext(ctx)
modifier, err := appcontext.User(ctx)
if err != nil {
return nil, err
}
if modifier == nil {
return nil, fmt.Errorf("can not find user in context")
}
@ -618,7 +625,10 @@ func (s *sqlEntityServer) History(ctx context.Context, r *entity.EntityHistoryRe
}
func (s *sqlEntityServer) Search(ctx context.Context, r *entity.EntitySearchRequest) (*entity.EntitySearchResponse, error) {
user := store.UserFromContext(ctx)
user, err := appcontext.User(ctx)
if err != nil {
return nil, err
}
if user == nil {
return nil, fmt.Errorf("missing user in context")
}

View File

@ -5,12 +5,12 @@ import (
"testing"
apikeygenprefix "github.com/grafana/grafana/pkg/components/apikeygenprefixed"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/server"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/org"
saAPI "github.com/grafana/grafana/pkg/services/serviceaccounts/api"
saTests "github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/store/entity"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
@ -86,6 +86,6 @@ func createTestContext(t *testing.T) testContext {
authToken: authToken,
client: client,
user: serviceAccountUser,
ctx: store.ContextWithUser(context.Background(), serviceAccountUser),
ctx: appcontext.WithUser(context.Background(), serviceAccountUser),
}
}

View File

@ -6,9 +6,9 @@ import (
"sync"
"time"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/tsdb/grafanads"
)
@ -122,9 +122,12 @@ func (c *dsCache) getDS(ctx context.Context, uid string) (*dsVal, error) {
}
}
orgID := store.UserFromContext(ctx).OrgID
usr, err := appcontext.User(ctx)
if err != nil {
return nil, nil // no user
}
v, ok := c.cache[orgID]
v, ok := c.cache[usr.OrgID]
if !ok {
return nil, nil // org not found
}

View File

@ -4,17 +4,17 @@ import (
"context"
"testing"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/datasources"
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
)
func TestResolver(t *testing.T) {
ctxOrg1 := store.ContextWithUser(context.Background(), &user.SignedInUser{OrgID: 1})
ctxOrg1 := appcontext.WithUser(context.Background(), &user.SignedInUser{OrgID: 1})
ds := &fakeDatasources.FakeDataSourceService{
DataSources: []*datasources.DataSource{