Files
grafana/pkg/api/dtos/models.go
T

122 lines
4.1 KiB
Go
Raw Normal View History

2014-11-21 16:43:04 +01:00
package dtos
2014-10-05 21:13:01 +02:00
import (
"crypto/md5"
"fmt"
"net/http"
"regexp"
2014-10-05 21:13:01 +02:00
"strings"
2016-03-11 23:28:33 +01:00
"github.com/grafana/grafana/pkg/components/simplejson"
2019-10-08 18:57:53 +02:00
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
2014-10-05 21:13:01 +02:00
)
2020-07-16 14:39:01 +02:00
var regNonAlphaNumeric = regexp.MustCompile("[^a-zA-Z0-9]+")
var mlog = log.New("models")
2020-07-16 14:39:01 +02:00
2016-03-10 01:31:10 -08:00
type AnyId struct {
Id int64 `json:"id"`
}
2015-01-21 09:52:40 +01:00
type LoginCommand struct {
User string `json:"user" binding:"Required"`
Password string `json:"password" binding:"Required"`
Remember bool `json:"remember"`
2014-10-05 21:13:01 +02:00
}
2014-11-21 16:43:04 +01:00
type CurrentUser struct {
IsSignedIn bool `json:"isSignedIn"`
Id int64 `json:"id"`
ExternalUserId string `json:"externalUserId"`
Login string `json:"login"`
Email string `json:"email"`
Name string `json:"name"`
LightTheme bool `json:"lightTheme"`
OrgCount int `json:"orgCount"`
OrgId int64 `json:"orgId"`
OrgName string `json:"orgName"`
OrgRole models.RoleType `json:"orgRole"`
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
GravatarUrl string `json:"gravatarUrl"`
Timezone string `json:"timezone"`
WeekStart string `json:"weekStart"`
Locale string `json:"locale"`
HelpFlags1 models.HelpFlags1 `json:"helpFlags1"`
HasEditPermissionInFolders bool `json:"hasEditPermissionInFolders"`
Permissions UserPermissionsMap `json:"permissions,omitempty"`
2014-10-05 21:13:01 +02:00
}
type UserPermissionsMap map[string]bool
// swagger:model
type MetricRequest struct {
// From Start time in epoch timestamps in milliseconds or relative using Grafana time units.
// required: true
// example: now-1h
From string `json:"from"`
// To End time in epoch timestamps in milliseconds or relative using Grafana time units.
// required: true
// example: now
To string `json:"to"`
// queries.refId Specifies an identifier of the query. Is optional and default to “A”.
// queries.datasourceId Specifies the data source to be queried. Each query in the request must have an unique datasourceId.
// queries.maxDataPoints - Species maximum amount of data points that dashboard panel can render. Is optional and default to 100.
// queries.intervalMs - Specifies the time interval in milliseconds of time series. Is optional and defaults to 1000.
// required: true
// example: [ { "refId": "A", "intervalMs": 86400000, "maxDataPoints": 1092, "datasource":{ "uid":"PD8C576611E62080A" }, "rawSql": "SELECT 1 as valueOne, 2 as valueTwo", "format": "table" } ]
Queries []*simplejson.Json `json:"queries"`
// required: false
Debug bool `json:"debug"`
HTTPRequest *http.Request `json:"-"`
}
func GetGravatarUrl(text string) string {
2018-03-27 22:52:29 +02:00
if setting.DisableGravatar {
return setting.AppSubUrl + "/public/img/user_profile.png"
2018-03-27 22:52:29 +02:00
}
if text == "" {
return ""
2014-10-05 21:13:01 +02:00
}
hash, _ := GetGravatarHash(text)
return fmt.Sprintf(setting.AppSubUrl+"/avatar/%x", hash)
}
func GetGravatarHash(text string) ([]byte, bool) {
if text == "" {
return make([]byte, 0), false
}
2014-10-05 21:13:01 +02:00
hasher := md5.New()
2019-10-08 18:57:53 +02:00
if _, err := hasher.Write([]byte(strings.ToLower(text))); err != nil {
mlog.Warn("Failed to hash text", "err", err)
2019-10-08 18:57:53 +02:00
}
return hasher.Sum(nil), true
2014-10-05 21:13:01 +02:00
}
func GetGravatarUrlWithDefault(text string, defaultText string) string {
if text != "" {
return GetGravatarUrl(text)
}
2020-07-16 14:39:01 +02:00
text = regNonAlphaNumeric.ReplaceAllString(defaultText, "") + "@localhost"
return GetGravatarUrl(text)
}
func IsHiddenUser(userLogin string, signedInUser *models.SignedInUser, cfg *setting.Cfg) bool {
if userLogin == "" || signedInUser.IsGrafanaAdmin || userLogin == signedInUser.Login {
return false
}
if _, hidden := cfg.HiddenUsers[userLogin]; hidden {
return true
}
return false
}