2023-08-02 03:43:56 -05:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-06 13:17:34 -05:00
|
|
|
"strconv"
|
2023-08-02 03:43:56 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models/roletype"
|
2023-08-09 02:35:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-08-02 03:43:56 -05:00
|
|
|
)
|
|
|
|
|
2024-02-01 05:37:01 -06:00
|
|
|
const (
|
|
|
|
GlobalOrgID = int64(0)
|
|
|
|
)
|
|
|
|
|
2023-08-02 03:43:56 -05:00
|
|
|
type SignedInUser struct {
|
2024-04-11 03:25:29 -05:00
|
|
|
UserID int64 `xorm:"user_id"`
|
|
|
|
UserUID string `xorm:"user_uid"`
|
|
|
|
OrgID int64 `xorm:"org_id"`
|
|
|
|
OrgName string
|
|
|
|
OrgRole roletype.RoleType
|
|
|
|
Login string
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
EmailVerified bool
|
|
|
|
// AuthID will be set if user signed in using external method
|
|
|
|
AuthID string
|
|
|
|
// AuthenticatedBy be set if user signed in using external method
|
2023-08-02 03:43:56 -05:00
|
|
|
AuthenticatedBy string
|
|
|
|
ApiKeyID int64 `xorm:"api_key_id"`
|
|
|
|
IsServiceAccount bool `xorm:"is_service_account"`
|
|
|
|
IsGrafanaAdmin bool
|
|
|
|
IsAnonymous bool
|
|
|
|
IsDisabled bool
|
|
|
|
HelpFlags1 HelpFlags1
|
|
|
|
LastSeenAt time.Time
|
|
|
|
Teams []int64
|
|
|
|
// Permissions grouped by orgID and actions
|
|
|
|
Permissions map[int64]map[string][]string `json:"-"`
|
2023-09-28 02:22:05 -05:00
|
|
|
// IDToken is a signed token representing the identity that can be forwarded to plugins and external services.
|
|
|
|
// Will only be set when featuremgmt.FlagIdForwarding is enabled.
|
2024-04-02 10:45:15 -05:00
|
|
|
IDToken string `json:"-" xorm:"-"`
|
2024-05-02 07:50:56 -05:00
|
|
|
NamespacedID identity.NamespaceID
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
|
|
|
|
return u.UserID > 0 && time.Since(u.LastSeenAt) > time.Minute*5
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) NameOrFallback() string {
|
|
|
|
if u.Name != "" {
|
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
if u.Login != "" {
|
|
|
|
return u.Login
|
|
|
|
}
|
|
|
|
return u.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) HasRole(role roletype.RoleType) bool {
|
|
|
|
if u.IsGrafanaAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.OrgRole.Includes(role)
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// IsRealUser returns true if the entity is a real user and not a service account
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) IsRealUser() bool {
|
|
|
|
// backwards compatibility
|
|
|
|
// checking if userId the user is a real user
|
|
|
|
// previously we used to check if the UserId was 0 or -1
|
|
|
|
// and not a service account
|
|
|
|
return u.UserID > 0 && !u.IsServiceAccountUser()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) IsApiKeyUser() bool {
|
|
|
|
return u.ApiKeyID > 0
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// IsServiceAccountUser returns true if the entity is a service account
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) IsServiceAccountUser() bool {
|
|
|
|
return u.IsServiceAccount
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// HasUniqueId returns true if the entity has a unique id
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) HasUniqueId() bool {
|
|
|
|
return u.IsRealUser() || u.IsApiKeyUser() || u.IsServiceAccountUser()
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetCacheKey returns a unique key for the entity.
|
|
|
|
// Add an extra prefix to avoid collisions with other caches
|
2023-09-14 02:19:33 -05:00
|
|
|
func (u *SignedInUser) GetCacheKey() string {
|
|
|
|
namespace, id := u.GetNamespacedID()
|
|
|
|
if !u.HasUniqueId() {
|
|
|
|
// Hack use the org role as id for identities that do not have a unique id
|
|
|
|
// e.g. anonymous and render key.
|
2023-11-22 08:51:11 -06:00
|
|
|
orgRole := u.GetOrgRole()
|
|
|
|
if orgRole == "" {
|
|
|
|
orgRole = roletype.RoleNone
|
|
|
|
}
|
|
|
|
|
|
|
|
id = string(orgRole)
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
2023-09-14 02:19:33 -05:00
|
|
|
|
|
|
|
return fmt.Sprintf("%d-%s-%s", u.GetOrgID(), namespace, id)
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetIsGrafanaAdmin returns true if the user is a server admin
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) GetIsGrafanaAdmin() bool {
|
|
|
|
return u.IsGrafanaAdmin
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetLogin returns the login of the active entity
|
|
|
|
// Can be empty if the user is anonymous
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) GetLogin() string {
|
|
|
|
return u.Login
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgID returns the ID of the active organization
|
2023-08-02 03:43:56 -05:00
|
|
|
func (u *SignedInUser) GetOrgID() int64 {
|
|
|
|
return u.OrgID
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// DEPRECATED: GetOrgName returns the name of the active organization
|
|
|
|
// Retrieve the organization name from the organization service instead of using this method.
|
|
|
|
func (u *SignedInUser) GetOrgName() string {
|
|
|
|
return u.OrgName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissions returns the permissions of the active entity
|
2023-08-09 02:35:50 -05:00
|
|
|
func (u *SignedInUser) GetPermissions() map[string][]string {
|
2023-08-02 03:43:56 -05:00
|
|
|
if u.Permissions == nil {
|
|
|
|
return make(map[string][]string)
|
|
|
|
}
|
|
|
|
|
2023-08-09 02:35:50 -05:00
|
|
|
if u.Permissions[u.GetOrgID()] == nil {
|
|
|
|
return make(map[string][]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Permissions[u.GetOrgID()]
|
|
|
|
}
|
|
|
|
|
2024-02-01 05:37:01 -06:00
|
|
|
// GetGlobalPermissions returns the permissions of the active entity that are available across all organizations
|
|
|
|
func (u *SignedInUser) GetGlobalPermissions() map[string][]string {
|
|
|
|
if u.Permissions == nil {
|
|
|
|
return make(map[string][]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Permissions[GlobalOrgID] == nil {
|
|
|
|
return make(map[string][]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Permissions[GlobalOrgID]
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// DEPRECATED: GetTeams returns the teams the entity is a member of
|
|
|
|
// Retrieve the teams from the team service instead of using this method.
|
2023-08-09 02:35:50 -05:00
|
|
|
func (u *SignedInUser) GetTeams() []int64 {
|
|
|
|
return u.Teams
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgRole returns the role of the active entity in the active organization
|
2023-08-09 02:35:50 -05:00
|
|
|
func (u *SignedInUser) GetOrgRole() roletype.RoleType {
|
|
|
|
return u.OrgRole
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:08:15 -05:00
|
|
|
// GetID returns namespaced id for the entity
|
2024-05-02 07:50:56 -05:00
|
|
|
func (u *SignedInUser) GetID() identity.NamespaceID {
|
2024-05-06 13:17:34 -05:00
|
|
|
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() (identity.Namespace, string) {
|
2023-08-09 02:35:50 -05:00
|
|
|
switch {
|
|
|
|
case u.ApiKeyID != 0:
|
2024-05-06 13:17:34 -05:00
|
|
|
return identity.NamespaceAPIKey, strconv.FormatInt(u.ApiKeyID, 10)
|
2023-08-09 02:35:50 -05:00
|
|
|
case u.IsServiceAccount:
|
2024-05-06 13:17:34 -05:00
|
|
|
return identity.NamespaceServiceAccount, strconv.FormatInt(u.UserID, 10)
|
2023-08-09 05:33:35 -05:00
|
|
|
case u.UserID > 0:
|
2024-05-06 13:17:34 -05:00
|
|
|
return identity.NamespaceUser, strconv.FormatInt(u.UserID, 10)
|
2023-08-09 02:35:50 -05:00
|
|
|
case u.IsAnonymous:
|
2024-05-06 13:17:34 -05:00
|
|
|
return identity.NamespaceAnonymous, "0"
|
2024-03-15 09:08:15 -05:00
|
|
|
case u.AuthenticatedBy == "render" && u.UserID == 0:
|
2024-05-06 13:17:34 -05:00
|
|
|
return identity.NamespaceRenderService, "0"
|
2023-08-09 02:35:50 -05:00
|
|
|
}
|
|
|
|
|
2024-05-06 13:17:34 -05:00
|
|
|
return u.NamespacedID.Namespace(), u.NamespacedID.ID()
|
2024-03-15 09:08:15 -05:00
|
|
|
}
|
|
|
|
|
2024-05-06 13:17:34 -05:00
|
|
|
// GetUID returns namespaced uid for the entity
|
|
|
|
func (u *SignedInUser) GetUID() identity.NamespaceID {
|
|
|
|
switch {
|
|
|
|
case u.ApiKeyID != 0:
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceAPIKey, strconv.FormatInt(u.ApiKeyID, 10))
|
2024-05-06 13:17:34 -05:00
|
|
|
case u.IsServiceAccount:
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceServiceAccount, u.UserUID)
|
2024-05-06 13:17:34 -05:00
|
|
|
case u.UserID > 0:
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceUser, u.UserUID)
|
2024-05-06 13:17:34 -05:00
|
|
|
case u.IsAnonymous:
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceAnonymous, "0")
|
2024-05-06 13:17:34 -05:00
|
|
|
case u.AuthenticatedBy == "render" && u.UserID == 0:
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceRenderService, "0")
|
2024-05-06 13:17:34 -05:00
|
|
|
}
|
|
|
|
|
2024-05-14 07:13:50 -05:00
|
|
|
return identity.NewNamespaceIDString(identity.NamespaceEmpty, "0")
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
|
|
|
|
2024-04-11 03:25:29 -05:00
|
|
|
func (u *SignedInUser) GetAuthID() string {
|
|
|
|
return u.AuthID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) GetAuthenticatedBy() string {
|
|
|
|
return u.AuthenticatedBy
|
|
|
|
}
|
|
|
|
|
2024-03-27 09:22:13 -05:00
|
|
|
func (u *SignedInUser) IsAuthenticatedBy(providers ...string) bool {
|
|
|
|
for _, p := range providers {
|
|
|
|
if u.AuthenticatedBy == p {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-02 03:43:56 -05:00
|
|
|
// FIXME: remove this method once all services are using an interface
|
|
|
|
func (u *SignedInUser) IsNil() bool {
|
|
|
|
return u == nil
|
|
|
|
}
|
2023-08-09 05:33:35 -05:00
|
|
|
|
|
|
|
// GetEmail returns the email of the active entity
|
|
|
|
// Can be empty.
|
|
|
|
func (u *SignedInUser) GetEmail() string {
|
|
|
|
return u.Email
|
|
|
|
}
|
|
|
|
|
2024-04-05 05:05:46 -05:00
|
|
|
func (u *SignedInUser) IsEmailVerified() bool {
|
|
|
|
return u.EmailVerified
|
|
|
|
}
|
|
|
|
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetDisplayName returns the display name of the active entity
|
|
|
|
// The display name is the name if it is set, otherwise the login or email
|
|
|
|
func (u *SignedInUser) GetDisplayName() string {
|
|
|
|
return u.NameOrFallback()
|
|
|
|
}
|
2023-09-06 04:16:10 -05:00
|
|
|
|
2023-09-28 02:22:05 -05:00
|
|
|
func (u *SignedInUser) GetIDToken() string {
|
|
|
|
return u.IDToken
|
|
|
|
}
|