mirror of https://github.com/grafana/grafana
Chore: Replace appcontext.User(ctx) with identity.GetRequester(ctx) (#91030)
parent
ddd38afa57
commit
be7b1ce2df
@ -1,79 +0,0 @@ |
||||
package appcontext |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity" |
||||
grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context" |
||||
"github.com/grafana/grafana/pkg/services/user" |
||||
) |
||||
|
||||
type ctxUserKey struct{} |
||||
|
||||
// WithUser adds the supplied SignedInUser to the context.
|
||||
func WithUser(ctx context.Context, usr *user.SignedInUser) context.Context { |
||||
ctx = context.WithValue(ctx, ctxUserKey{}, usr) |
||||
// make sure it is also in the simplified version
|
||||
if usr == nil || usr.IsNil() { |
||||
return identity.WithRequester(ctx, nil) |
||||
} |
||||
return identity.WithRequester(ctx, usr) |
||||
} |
||||
|
||||
// User extracts the SignedInUser from the supplied context.
|
||||
// Supports context set by appcontext.WithUser, gRPC server context, and HTTP ReqContext.
|
||||
// Deprecated: use identity.GetRequester(ctx) when possible
|
||||
func User(ctx context.Context) (*user.SignedInUser, error) { |
||||
// Set by appcontext.WithUser
|
||||
u, ok := ctx.Value(ctxUserKey{}).(*user.SignedInUser) |
||||
if ok && u != nil { |
||||
return u, nil |
||||
} |
||||
|
||||
// Set by incoming gRPC server request
|
||||
grpcCtx := grpccontext.FromContext(ctx) |
||||
if grpcCtx != nil && grpcCtx.SignedInUser != nil { |
||||
return grpcCtx.SignedInUser, nil |
||||
} |
||||
|
||||
// If the identity was set via requester, but not appcontext, we can map values
|
||||
// NOTE: this path
|
||||
requester, _ := identity.GetRequester(ctx) |
||||
if requester != nil { |
||||
id := requester.GetID() |
||||
userId, _ := id.UserID() |
||||
orgId := requester.GetOrgID() |
||||
return &user.SignedInUser{ |
||||
NamespacedID: id, |
||||
UserID: userId, |
||||
UserUID: requester.GetUID().ID(), |
||||
OrgID: orgId, |
||||
OrgName: requester.GetOrgName(), |
||||
OrgRole: requester.GetOrgRole(), |
||||
Login: requester.GetLogin(), |
||||
Email: requester.GetEmail(), |
||||
IsGrafanaAdmin: requester.GetIsGrafanaAdmin(), |
||||
Teams: requester.GetTeams(), |
||||
AuthID: requester.GetAuthID(), |
||||
AuthenticatedBy: requester.GetAuthenticatedBy(), |
||||
IDToken: requester.GetIDToken(), |
||||
Permissions: map[int64]map[string][]string{ |
||||
0: requester.GetGlobalPermissions(), |
||||
orgId: requester.GetPermissions(), |
||||
}, |
||||
}, nil |
||||
} |
||||
|
||||
return nil, fmt.Errorf("a SignedInUser was not found in the context") |
||||
} |
||||
|
||||
// MustUser extracts the SignedInUser from the supplied context, and panics if a user is not found.
|
||||
// Supports context set by appcontext.WithUser, gRPC server context, and HTTP ReqContext.
|
||||
func MustUser(ctx context.Context) *user.SignedInUser { |
||||
usr, err := User(ctx) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return usr |
||||
} |
@ -1,71 +0,0 @@ |
||||
package appcontext_test |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/rand" |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity" |
||||
"github.com/grafana/grafana/pkg/infra/appcontext" |
||||
"github.com/grafana/grafana/pkg/infra/tracing" |
||||
grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context" |
||||
"github.com/grafana/grafana/pkg/services/user" |
||||
) |
||||
|
||||
func TestUserFromContext(t *testing.T) { |
||||
t.Run("User should error when context is missing user", func(t *testing.T) { |
||||
usr, err := appcontext.User(context.Background()) |
||||
require.Nil(t, usr) |
||||
require.Error(t, err) |
||||
}) |
||||
|
||||
t.Run("MustUser should panic when context is missing user", func(t *testing.T) { |
||||
require.Panics(t, func() { |
||||
_ = appcontext.MustUser(context.Background()) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("should return user set by ContextWithUser", func(t *testing.T) { |
||||
expected := testUser() |
||||
ctx := appcontext.WithUser(context.Background(), expected) |
||||
actual, err := appcontext.User(ctx) |
||||
require.NoError(t, err) |
||||
require.Equal(t, expected.UserID, actual.UserID) |
||||
|
||||
// The requester is also in context
|
||||
requester, err := identity.GetRequester(ctx) |
||||
require.NoError(t, err) |
||||
require.Equal(t, expected.GetUID(), requester.GetUID()) |
||||
}) |
||||
|
||||
t.Run("should return user set by gRPC context", func(t *testing.T) { |
||||
expected := testUser() |
||||
handler := grpccontext.ProvideContextHandler(tracing.InitializeTracerForTest()) |
||||
ctx := handler.SetUser(context.Background(), expected) |
||||
actual, err := appcontext.User(ctx) |
||||
require.NoError(t, err) |
||||
require.Equal(t, expected.UserID, actual.UserID) |
||||
}) |
||||
|
||||
t.Run("should return user set as a requester", func(t *testing.T) { |
||||
expected := testUser() |
||||
ctx := identity.WithRequester(context.Background(), expected) |
||||
actual, err := appcontext.User(ctx) |
||||
require.NoError(t, err) |
||||
require.Equal(t, expected.UserID, actual.UserID) |
||||
}) |
||||
} |
||||
|
||||
func testUser() *user.SignedInUser { |
||||
i, err := rand.Int(rand.Reader, big.NewInt(100000)) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return &user.SignedInUser{ |
||||
UserID: i.Int64(), |
||||
OrgID: 1, |
||||
} |
||||
} |
Loading…
Reference in new issue