mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Users: Allow specifying user UIDs in params (#95424)
* add user ID API translation * add uid to user frontend * use users' UIDs in admin pages * fix ldapSync page * use global user search for user by UID * remove active org filtering * remove orgID params
This commit is contained in:
@@ -213,8 +213,7 @@ type GetUserByIDQuery struct {
|
||||
}
|
||||
|
||||
type GetUserByUIDQuery struct {
|
||||
OrgID int64
|
||||
UID string
|
||||
UID string
|
||||
}
|
||||
|
||||
type StartVerifyEmailCommand struct {
|
||||
@@ -264,6 +263,7 @@ const (
|
||||
|
||||
type AdminCreateUserResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
UID string `json:"uid"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
)
|
||||
@@ -13,6 +14,7 @@ type Service interface {
|
||||
CreateServiceAccount(context.Context, *CreateUserCommand) (*User, error)
|
||||
Delete(context.Context, *DeleteUserCommand) error
|
||||
GetByID(context.Context, *GetUserByIDQuery) (*User, error)
|
||||
// GetByUID returns a user by UID. This also includes service accounts (identity use only)
|
||||
GetByUID(context.Context, *GetUserByUIDQuery) (*User, error)
|
||||
GetByLogin(context.Context, *GetUserByLoginQuery) (*User, error)
|
||||
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
|
||||
@@ -28,3 +30,16 @@ type Verifier interface {
|
||||
Start(ctx context.Context, cmd StartVerifyEmailCommand) error
|
||||
Complete(ctx context.Context, cmd CompleteEmailVerifyCommand) error
|
||||
}
|
||||
|
||||
func UIDToIDHandler(userService Service) func(ctx context.Context, userID string) (string, error) {
|
||||
return func(ctx context.Context, userID string) (string, error) {
|
||||
_, err := strconv.ParseInt(userID, 10, 64)
|
||||
if userID == "" || err == nil {
|
||||
return userID, nil
|
||||
}
|
||||
user, err := userService.GetByUID(ctx, &GetUserByUIDQuery{
|
||||
UID: userID,
|
||||
})
|
||||
return strconv.FormatInt(user.ID, 10), err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
type store interface {
|
||||
Insert(context.Context, *user.User) (int64, error)
|
||||
GetByID(context.Context, int64) (*user.User, error)
|
||||
GetByUID(ctx context.Context, orgId int64, uid string) (*user.User, error)
|
||||
GetByUID(ctx context.Context, uid string) (*user.User, error)
|
||||
GetByLogin(context.Context, *user.GetUserByLoginQuery) (*user.User, error)
|
||||
GetByEmail(context.Context, *user.GetUserByEmailQuery) (*user.User, error)
|
||||
Delete(context.Context, int64) error
|
||||
@@ -108,14 +108,11 @@ func (ss *sqlStore) GetByID(ctx context.Context, userID int64) (*user.User, erro
|
||||
return &usr, err
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetByUID(ctx context.Context, orgId int64, uid string) (*user.User, error) {
|
||||
func (ss *sqlStore) GetByUID(ctx context.Context, uid string) (*user.User, error) {
|
||||
var usr user.User
|
||||
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
has, err := sess.Table("user").
|
||||
Where("org_id = ? AND uid = ?", orgId, uid).
|
||||
Get(&usr)
|
||||
|
||||
has, err := sess.Table("user").Where("uid = ?", uid).Get(&usr)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
|
||||
@@ -99,6 +99,12 @@ func TestIntegrationUserDataAccess(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "abcd", siu.UserUID)
|
||||
|
||||
query := user.GetUserByUIDQuery{UID: "abcd"}
|
||||
result, err := userStore.GetByUID(context.Background(), query.UID)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, result.UID, "abcd")
|
||||
require.Equal(t, result.Email, "next-test@email.com")
|
||||
})
|
||||
|
||||
t.Run("Testing DB - creates and loads user", func(t *testing.T) {
|
||||
|
||||
@@ -214,12 +214,11 @@ func (s *Service) GetByID(ctx context.Context, query *user.GetUserByIDQuery) (*u
|
||||
|
||||
func (s *Service) GetByUID(ctx context.Context, query *user.GetUserByUIDQuery) (*user.User, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "user.GetByUID", trace.WithAttributes(
|
||||
attribute.Int64("orgID", query.OrgID),
|
||||
attribute.String("userUID", query.UID),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
return s.store.GetByUID(ctx, query.OrgID, query.UID)
|
||||
return s.store.GetByUID(ctx, query.UID)
|
||||
}
|
||||
|
||||
func (s *Service) GetByLogin(ctx context.Context, query *user.GetUserByLoginQuery) (*user.User, error) {
|
||||
|
||||
@@ -291,7 +291,7 @@ func (f *FakeUserStore) GetByID(context.Context, int64) (*user.User, error) {
|
||||
return f.ExpectedUser, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeUserStore) GetByUID(context.Context, int64, string) (*user.User, error) {
|
||||
func (f *FakeUserStore) GetByUID(context.Context, string) (*user.User, error) {
|
||||
return f.ExpectedUser, f.ExpectedError
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user