mirror of
https://github.com/grafana/grafana.git
synced 2024-11-24 09:50:29 -06:00
Chore: get user from appcontext rather than store utility (#59926)
user from appcontext
This commit is contained in:
parent
e8ed79873a
commit
d036225f7b
@ -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
|
||||
})
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user