2023-08-02 03:43:56 -05:00
|
|
|
package identity
|
|
|
|
|
2023-08-10 07:20:58 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models/roletype"
|
|
|
|
)
|
2023-08-09 02:35:50 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
NamespaceUser = "user"
|
|
|
|
NamespaceAPIKey = "api-key"
|
|
|
|
NamespaceServiceAccount = "service-account"
|
|
|
|
NamespaceAnonymous = "anonymous"
|
|
|
|
NamespaceRenderService = "render"
|
|
|
|
)
|
|
|
|
|
2023-08-10 07:20:58 -05:00
|
|
|
var ErrNotIntIdentifier = errors.New("identifier is not an int64")
|
|
|
|
var ErrIdentifierNotInitialized = errors.New("identifier is not initialized")
|
|
|
|
|
2023-08-02 03:43:56 -05:00
|
|
|
type Requester interface {
|
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.
|
|
|
|
GetDisplayName() string
|
|
|
|
// GetEmail returns the email of the active entity.
|
|
|
|
// Can be empty.
|
|
|
|
GetEmail() string
|
|
|
|
// GetIsGrafanaAdmin returns true if the user is a server admin
|
2023-08-02 03:43:56 -05:00
|
|
|
GetIsGrafanaAdmin() bool
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetLogin returns the login of the active entity
|
|
|
|
// Can be empty.
|
2023-08-02 03:43:56 -05:00
|
|
|
GetLogin() string
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetNamespacedID returns the namespace and ID of the active entity.
|
|
|
|
// The namespace is one of the constants defined in pkg/services/auth/identity.
|
2023-08-28 03:42:24 -05:00
|
|
|
GetNamespacedID() (namespace string, identifier string)
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgID returns the ID of the active organization
|
2023-08-02 03:43:56 -05:00
|
|
|
GetOrgID() int64
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgRole returns the role of the active entity in the active organization.
|
|
|
|
GetOrgRole() roletype.RoleType
|
|
|
|
// GetPermissions returns the permissions of the active entity.
|
2023-08-09 02:35:50 -05:00
|
|
|
GetPermissions() map[string][]string
|
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
|
|
|
GetTeams() []int64
|
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.
|
|
|
|
GetOrgName() string
|
|
|
|
|
|
|
|
// IsNil returns true if the identity is nil
|
|
|
|
// FIXME: remove this method once all services are using an interface
|
2023-08-02 03:43:56 -05:00
|
|
|
IsNil() bool
|
2023-08-09 02:35:50 -05:00
|
|
|
|
|
|
|
// Legacy
|
2023-08-09 05:33:35 -05:00
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
// HasRole returns true if the active entity has the given role in the active organization.
|
|
|
|
HasRole(role roletype.RoleType) bool
|
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-08-09 02:35:50 -05:00
|
|
|
GetCacheKey() (string, error)
|
2023-08-09 05:33:35 -05:00
|
|
|
// HasUniqueId returns true if the entity has a unique id
|
2023-08-09 02:35:50 -05:00
|
|
|
HasUniqueId() bool
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
2023-08-10 07:20:58 -05:00
|
|
|
|
|
|
|
// IntIdentifier converts a string identifier to an int64.
|
|
|
|
// Applicable for users, service accounts, api keys and renderer service.
|
|
|
|
// Errors if the identifier is not initialized or if namespace is not recognized.
|
|
|
|
func IntIdentifier(namespace, identifier string) (int64, error) {
|
|
|
|
switch namespace {
|
|
|
|
case NamespaceUser, NamespaceAPIKey, NamespaceServiceAccount, NamespaceRenderService:
|
|
|
|
id, err := strconv.ParseInt(identifier, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unrecognized format for valid namespace %s: %w", namespace, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return 0, ErrIdentifierNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, ErrNotIntIdentifier
|
|
|
|
}
|