Chore: Fix error handling in postDashboard, remove UserDisplayDTO, fix live redis client initialization (#87206)

* clean up error handling in postDashboard and remove UserDisplayDTO

* replace GetUserUID with GetUID and GetNamespacedUID, enforce namespace constant type

* lint fix

* lint fix

* more lint fixes
This commit is contained in:
Dan Cech
2024-05-06 14:17:34 -04:00
committed by GitHub
parent ba8b4bde3a
commit 41bee274fd
27 changed files with 206 additions and 183 deletions
+43 -51
View File
@@ -2,6 +2,7 @@ package user
import (
"fmt"
"strconv"
"time"
"github.com/grafana/grafana/pkg/models/roletype"
@@ -56,36 +57,6 @@ func (u *SignedInUser) NameOrFallback() string {
return u.Email
}
// TODO: There's a need to remove this struct since it creates a circular dependency
// DEPRECATED: This function uses `UserDisplayDTO` model which we want to remove
// In order to retrieve the user URL, we need the dto library. However, adding
// the dto library to the user service creates a circular dependency
func (u *SignedInUser) ToUserDisplayDTO() *UserDisplayDTO {
return &UserDisplayDTO{
ID: u.UserID,
UID: u.UserUID,
Login: u.Login,
Name: u.Name,
// AvatarURL: dtos.GetGravatarUrl(u.GetEmail()),
}
}
// Static function to parse a requester into a UserDisplayDTO
func NewUserDisplayDTOFromRequester(requester identity.Requester) (*UserDisplayDTO, error) {
userID := int64(0)
namespaceID, identifier := requester.GetNamespacedID()
if namespaceID == identity.NamespaceUser || namespaceID == identity.NamespaceServiceAccount {
userID, _ = identity.IntIdentifier(namespaceID, identifier)
}
return &UserDisplayDTO{
ID: userID,
Login: requester.GetLogin(),
Name: requester.GetDisplayName(),
}, nil
}
func (u *SignedInUser) HasRole(role roletype.RoleType) bool {
if u.IsGrafanaAdmin {
return true
@@ -196,27 +167,52 @@ func (u *SignedInUser) GetOrgRole() roletype.RoleType {
// GetID returns namespaced id for the entity
func (u *SignedInUser) GetID() identity.NamespaceID {
switch {
case u.ApiKeyID != 0:
return namespacedID(identity.NamespaceAPIKey, u.ApiKeyID)
case u.IsServiceAccount:
return namespacedID(identity.NamespaceServiceAccount, u.UserID)
case u.UserID > 0:
return namespacedID(identity.NamespaceUser, u.UserID)
case u.IsAnonymous:
return namespacedID(identity.NamespaceAnonymous, 0)
case u.AuthenticatedBy == "render" && u.UserID == 0:
return namespacedID(identity.NamespaceRenderService, 0)
}
return u.NamespacedID
ns, id := u.GetNamespacedID()
return identity.NewNamespaceIDString(ns, id)
}
// GetNamespacedID returns the namespace and ID of the active entity
// The namespace is one of the constants defined in pkg/services/auth/identity
func (u *SignedInUser) GetNamespacedID() (string, string) {
id := u.GetID()
return id.Namespace(), id.ID()
func (u *SignedInUser) GetNamespacedID() (identity.Namespace, string) {
switch {
case u.ApiKeyID != 0:
return identity.NamespaceAPIKey, strconv.FormatInt(u.ApiKeyID, 10)
case u.IsServiceAccount:
return identity.NamespaceServiceAccount, strconv.FormatInt(u.UserID, 10)
case u.UserID > 0:
return identity.NamespaceUser, strconv.FormatInt(u.UserID, 10)
case u.IsAnonymous:
return identity.NamespaceAnonymous, "0"
case u.AuthenticatedBy == "render" && u.UserID == 0:
return identity.NamespaceRenderService, "0"
}
return u.NamespacedID.Namespace(), u.NamespacedID.ID()
}
// GetUID returns namespaced uid for the entity
func (u *SignedInUser) GetUID() identity.NamespaceID {
ns, uid := u.GetNamespacedUID()
return identity.NewNamespaceIDString(ns, uid)
}
// GetNamespacedUID returns the namespace and UID of the active entity
// The namespace is one of the constants defined in pkg/services/auth/identity
func (u *SignedInUser) GetNamespacedUID() (identity.Namespace, string) {
switch {
case u.ApiKeyID != 0:
return identity.NamespaceAPIKey, fmt.Sprint(u.ApiKeyID)
case u.IsServiceAccount:
return identity.NamespaceServiceAccount, u.UserUID
case u.UserID > 0:
return identity.NamespaceUser, u.UserUID
case u.IsAnonymous:
return identity.NamespaceAnonymous, ""
case u.AuthenticatedBy == "render" && u.UserID == 0:
return identity.NamespaceRenderService, ""
}
return identity.NamespaceEmpty, ""
}
func (u *SignedInUser) GetAuthID() string {
@@ -260,7 +256,3 @@ func (u *SignedInUser) GetDisplayName() string {
func (u *SignedInUser) GetIDToken() string {
return u.IDToken
}
func namespacedID(namespace string, id int64) identity.NamespaceID {
return identity.NewNamespaceIDUnchecked(namespace, id)
}
-8
View File
@@ -223,14 +223,6 @@ type ErrCaseInsensitiveLoginConflict struct {
Users []User
}
type UserDisplayDTO struct {
ID int64 `json:"id,omitempty"`
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login,omitempty"`
AvatarURL string `json:"avatarUrl"`
}
func (e *ErrCaseInsensitiveLoginConflict) Unwrap() error {
return ErrCaseInsensitive
}